PSCF v1.4.0
cFieldIo.tpp
1#ifndef PRDC_CFIELD_IO_TPP
2#define PRDC_CFIELD_IO_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2025, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <pscf/mesh/MeshIterator.h>
12#include <pscf/math/IntVec.h>
13#include <pscf/math/arithmetic.h>
14
15#include <util/containers/DArray.h>
16#include <util/format/Dbl.h>
17
18namespace Pscf {
19namespace Prdc {
20
21 template <int D, class AT>
22 void readCFieldsData(std::istream& in,
23 DArray< AT > & fields,
24 IntVec<D> const& dimensions)
25 {
26 double x, y;
27 MeshIterator<D> iter(dimensions);
28 int nMonomer = fields.capacity();
29 UTIL_CHECK(nMonomer > 0);
30 int rank, j;
31 for (j = 0; j < nMonomer; ++j) {
32 for (iter.begin(); !iter.atEnd(); ++iter) {
33 rank = iter.rank();
34 in >> x;
35 UTIL_ASSERT(in.good());
36 in >> y;
37 UTIL_ASSERT(in.good());
38 assign(fields[j][rank], x, y);
39 }
40 }
41 UTIL_CHECK(in.good());
42 }
43
44 template <int D, class AT>
45 void readCFieldData(std::istream& in,
46 AT& field,
47 IntVec<D> const& dimensions)
48 {
49 double x, y;
50 MeshIterator<D> iter(dimensions);
51 int rank;
52 for (iter.begin(); !iter.atEnd(); ++iter) {
53 rank = iter.rank();
54 in >> x;
55 UTIL_ASSERT(in.good());
56 in >> y;
57 UTIL_ASSERT(in.good());
58 assign(field[rank], x, y);
59 }
60 UTIL_CHECK(in.good());
61 }
62
63 template <int D, class AT, typename CT, typename RT>
64 void writeCFieldsData(std::ostream& out,
65 DArray< AT > const& fields,
66 IntVec<D> const& dimensions)
67 {
68 RT x, y;
69 MeshIterator<D> iter(dimensions);
70 int nMonomer = fields.capacity();
71 UTIL_CHECK(nMonomer > 0);
72 int rank, j;
73 for (j = 0; j < nMonomer; ++j) {
74 for (iter.begin(); !iter.atEnd(); ++iter) {
75 rank = iter.rank();
76 x = real( fields[j][rank] );
77 y = imag( fields[j][rank] );
78 out << " " << Dbl(x, 21, 13)
79 << " " << Dbl(y, 21, 13);
80 }
81 out << std::endl;
82 }
83 }
84
85 template <int D, class AT, typename CT, typename RT>
86 void writeCFieldData(std::ostream& out,
87 AT const& field,
88 IntVec<D> const& dimensions)
89 {
90 RT x, y;
91 MeshIterator<D> iter(dimensions);
92 int rank;
93 for (iter.begin(); !iter.atEnd(); ++iter) {
94 rank = iter.rank();
95 x = real( field[rank] );
96 y = imag( field[rank] );
97 out << " " << Dbl(x, 21, 13)
98 << " " << Dbl(y, 21, 13)
99 << std::endl;
100 }
101 }
102
103} // namespace Prdc
104} // namespace Pscf
105#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
Iterator over points in a Mesh<D>.
int rank() const
Get the rank of current element.
void begin()
Set iterator to the first point in the mesh.
bool atEnd() const
Is this the end (i.e., one past the last point)?
int capacity() const
Return allocated size.
Definition Array.h:144
Dynamically allocatable contiguous array template.
Definition DArray.h:32
Wrapper for a double precision number, for formatted ostream output.
Definition Dbl.h:40
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_ASSERT(condition)
Assertion macro suitable for debugging serial or parallel code.
Definition global.h:75
void writeCFieldsData(std::ostream &out, DArray< AT > const &fields, IntVec< D > const &dimensions)
Write data for an array of complex fields, with no header section.
Definition cFieldIo.tpp:64
void readCFieldData(std::istream &in, AT &field, IntVec< D > const &dimensions)
Read data for a single complex field, with no header section.
Definition cFieldIo.tpp:45
void writeCFieldData(std::ostream &out, AT const &field, IntVec< D > const &dimensions)
Write data for a single complex field, with no header section.
Definition cFieldIo.tpp:86
void readCFieldsData(std::istream &in, DArray< AT > &fields, IntVec< D > const &dimensions)
Read data for an array of complex fields, with no header section.
Definition cFieldIo.tpp:22
double imag(fftw_complex const &a)
Return the imaginary part of an fftw_complex number.
Definition cpu/complex.h:49
void assign(fftw_complex &z, double const &a, double const &b)
Create an fftw_complex from real and imaginary parts, z = a + ib.
double real(fftw_complex const &a)
Return the real part of an fftw_complex number.
Definition cpu/complex.h:38
Periodic fields and crystallography.
Definition complex.cpp:11
PSCF package top-level namespace.