PSCF v1.5.0
r1/field/FieldIo.cpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "FieldIo.h"
9
10#include <r1/field/Domain.h>
11#include <r1/solvers/Mixture.h>
12#include <r1/solvers/Polymer.h>
13#include <r1/solvers/Solvent.h>
14#include <r1/solvers/Block.h>
15
16#include <pscf/chem/Vertex.h>
17
18#include <util/misc/FileMaster.h>
19#include <util/format/Str.h>
20#include <util/format/Int.h>
21#include <util/format/Dbl.h>
22
23#include <string>
24
25namespace Pscf {
26namespace R1
27{
28
29 using namespace Util;
30
31 /*
32 * Constructor.
33 */
36
37 /*
38 * Destructor.
39 */
42
43
44 void FieldIo::associate(Domain const & domain,
45 FileMaster const & fileMaster)
46 {
47 domainPtr_ = &domain;
48 fileMasterPtr_ = &fileMaster;
49 }
50
52 std::string const & filename)
53 {
54 std::ifstream in;
55 fileMaster().openInputFile(filename, in);
56 readFields(fields, in);
57 in.close();
58 }
59
60 void FieldIo::readFields(DArray<Field>& fields, std::istream& in)
61 {
62 // Read grid dimensions
63 std::string label;
64 in >> label;
65 UTIL_CHECK(label == "nx");
66 int nx = 0;
67 in >> nx;
68 UTIL_CHECK(nx > 0);
69 UTIL_CHECK(nx == domain().nx());
70 in >> label;
71 UTIL_CHECK (label == "nm");
72 int nm = 0;
73 in >> nm;
74 UTIL_CHECK(nm > 0);
75
76 // Check dimensions of fields array
77 UTIL_CHECK(nm == fields.capacity());
78 for (int i = 0; i < nm; ++i) {
79 UTIL_CHECK(nx == fields[i].capacity());
80 }
81
82 // Read fields
83 int i, j, idum;
84 for (i = 0; i < nx; ++i) {
85 in >> idum;
86 UTIL_CHECK(idum == i);
87 for (j = 0; j < nm; ++j) {
88 in >> fields[j][i];
89 }
90 }
91
92 // A missing or invalid value leaves the stream in a failed state
93 if (in.fail()) {
94 UTIL_THROW("Missing or invalid value in field file");
95 }
96 }
97
98 void FieldIo::writeField(Field const & field,
99 std::string const & filename, bool writeHeader) const
100 {
101 std::ofstream out;
102 fileMaster().openOutputFile(filename, out);
103 writeField(field, out, writeHeader);
104 out.close();
105 }
106
107 void FieldIo::writeField(Field const & field, std::ostream& out, bool writeHeader) const
108 {
109 int nx = field.capacity();
110 UTIL_CHECK(nx == domain().nx());
111
112 if (writeHeader){
113 out << "nx " << nx << std::endl;
114 }
115 // Write fields
116 int i;
117 for (i = 0; i < nx; ++i) {
118 out << " " << Dbl(field[i], 18, 11);
119 out << std::endl;
120 }
121 }
122
124 std::string const & filename, bool writeHeader)
125 const
126 {
127 std::ofstream out;
128 fileMaster().openOutputFile(filename, out);
129 writeFields(fields, out, writeHeader);
130 out.close();
131 }
132
134 std::ostream& out, bool writeHeader)
135 const
136 {
137 int nm = fields.capacity();
138 UTIL_CHECK(nm > 0);
139 int nx = fields[0].capacity();
140 UTIL_CHECK(nx == domain().nx());
141 if (nm > 1) {
142 for (int i = 0; i < nm; ++i) {
143 UTIL_CHECK(nx == fields[i].capacity());
144 }
145 }
146 if (writeHeader){
147 out << "nx " << nx << std::endl;
148 out << "nm " << nm << std::endl;
149 }
150 // Write fields
151 int i, j;
152 for (i = 0; i < nx; ++i) {
153 out << Int(i, 5);
154 for (j = 0; j < nm; ++j) {
155 out << " " << Dbl(fields[j][i], 18, 11);
156 }
157 out << std::endl;
158 }
159 }
160
161 void FieldIo::writeBlockCFields(Mixture const & mixture,
162 std::string const & filename) const
163 {
164 std::ofstream out;
165 fileMaster().openOutputFile(filename, out);
166 writeBlockCFields(mixture, out);
167 out.close();
168 }
169
170 /*
171 * Write the concentrations associated with all blocks.
172 */
173 void FieldIo::writeBlockCFields(Mixture const & mixture, std::ostream& out)
174 const
175 {
176 int nx = domain().nx(); // number grid points
177 int np = mixture.nPolymer(); // number of polymer species
178 int nb_tot = mixture.nBlock(); // number of blocks in all polymers
179 int ns = mixture.nSolvent(); // number of solvents
180
181 out << "nx " << nx << std::endl;
182 out << "n_block " << nb_tot << std::endl;
183 out << "n_solvent " << ns << std::endl;
184
185 int nb; // number of blocks per polymer
186 int i, j, k, l; // loop indices
187 double c;
188 for (i = 0; i < nx; ++i) {
189 out << Int(i, 5);
190 for (j = 0; j < np; ++j) {
191 nb = mixture.polymer(j).nBlock();
192 for (k = 0; k < nb; ++k) {
193 c = mixture.polymer(j).block(k).cField()[i];
194 out << " " << Dbl(c, 15, 8);
195 }
196 }
197 for (l = 0; l < ns; ++l) {
198 c = mixture.solvent(l).cField()[i];
199 out << " " << Dbl(c, 15, 8);
200 }
201 out << std::endl;
202 }
203 }
204
205 /*
206 * Write incoming q fields for a specified vertex.
207 */
208 void FieldIo::writeVertexQ(Mixture const & mixture,
209 int polymerId, int vertexId,
210 std::string const & filename) const
211 {
212 std::ofstream out;
213 fileMaster().openOutputFile(filename, out);
214 writeVertexQ(mixture, polymerId, vertexId, out);
215 out.close();
216 }
217
218 /*
219 * Write incoming q fields for a specified vertex.
220 */
221 void FieldIo::writeVertexQ(Mixture const & mixture,
222 int polymerId, int vertexId,
223 std::ostream& out) const
224 {
225 UTIL_CHECK(polymerId >= 0);
226 UTIL_CHECK(polymerId < mixture.nPolymer());
227 Polymer const & polymer = mixture.polymer(polymerId);
228 UTIL_CHECK(vertexId >= 0);
229 UTIL_CHECK(vertexId <= polymer.nBlock());
230 Vertex const & vertex = polymer.vertex(vertexId);
231 int nb = vertex.size(); // number of attached blocks
232 int nx = domain().nx(); // number grid points
233
234 Pair<int> pId;
235 int bId;
236 int dId;
237 int i, j;
238 double c, product;
239 for (i = 0; i < nx; ++i) {
240 out << Int(i, 5);
241 product = 1.0;
242 for (j = 0; j < nb; ++j) {
243 pId = vertex.inPropagatorId(j);
244 bId = pId[0]; // blockId
245 UTIL_CHECK(bId >= 0);
246 dId = pId[1]; // directionId
247 UTIL_CHECK(dId >= 0);
248 UTIL_CHECK(dId <= 1);
249 c = polymer.propagator(bId, dId).tail()[i];
250 out << " " << Dbl(c, 15, 8);
251 product *= c;
252 }
253 out << " " << Dbl(product, 15, 8) << std::endl;
254 }
255
256 }
257
258 /*
259 * Interpolate fields onto new mesh, write result to output file
260 */
261 void FieldIo::remesh(DArray<Field> const & fields, int nx,
262 std::string const & filename)
263 {
264 // Check nx before an output file is opened
265 if (nx < 2) {
266 UTIL_THROW("Number of grid points nx < 2 in remesh");
267 }
268 std::ofstream out;
269 fileMaster().openOutputFile(filename, out);
270 remesh(fields, nx, out);
271 out.close();
272 }
273
274 /*
275 * Interpolate fields onto new mesh, write result to outputstream
276 */
277 void
278 FieldIo::remesh(DArray<Field> const & fields, int nx, std::ostream& out)
279 {
280 if (nx < 2) {
281 UTIL_THROW("Number of grid points nx < 2 in remesh");
282 }
283
284 // Query and check dimensions of fields array
285 int nm = fields.capacity();
286 UTIL_CHECK(nm > 0);
287 for (int i = 0; i < nm; ++i) {
288 UTIL_CHECK(fields[i].capacity() == domain().nx());
289 }
290
291 // Output new grid dimensions
292 out << "nx " << nx << std::endl;
293 out << "nm " << nm << std::endl;
294
295 // Output first grid point
296 int i, j;
297 i = 0;
298 out << Int(i, 5);
299 for (j = 0; j < nm; ++j) {
300 out << " " << Dbl(fields[j][i]);
301 }
302 out << std::endl;
303
304 // Spacing for new grid
305 double dx = (domain().xMax() - domain().xMin())/double(nx-1);
306
307 // Variables used for interpolation
308 double y; // Grid coordinate in old grid
309 double fu; // fractional part of y
310 double fl; // 1.0 - fl
311 double w; // interpolated field value
312 int yi; // Truncated integer part of y
313
314 // Loop over intermediate points
315 for (i = 1; i < nx -1; ++i) {
316 y = dx*double(i)/domain().dx();
317 yi = y;
318 UTIL_CHECK(yi >= 0);
319 UTIL_CHECK(yi + 1 < domain().nx());
320 fu = y - double(yi);
321 fl = 1.0 - fu;
322
323 out << Int(i, 5);
324 for (j = 0; j < nm; ++j) {
325 w = fl*fields[j][yi] + fu*fields[j][yi+1];
326 out << " " << Dbl(w);
327 }
328 out << std::endl;
329 }
330
331 // Output last grid point
332 out << Int(nx - 1, 5);
333 i = domain().nx() - 1;
334 for (j = 0; j < nm; ++j) {
335 out << " " << Dbl(fields[j][i]);
336 }
337 out << std::endl;
338
339 }
340
341 void
342 FieldIo::extend(DArray<Field> const & fields, int m,
343 std::string const & filename)
344 {
345 // Check m before an output file is opened
346 if (m < 0) {
347 UTIL_THROW("Number of added grid points m < 0 in extend");
348 }
349 std::ofstream out;
350 fileMaster().openOutputFile(filename, out);
351 extend(fields, m, out);
352 out.close();
353 }
354
355 /*
356 * Interpolate fields onto new mesh.
357 */
358 void
359 FieldIo::extend(DArray<Field> const & fields, int m, std::ostream& out)
360 {
361 if (m < 0) {
362 UTIL_THROW("Number of added grid points m < 0 in extend");
363 }
364
365 // Query and check dimensions of fields array
366 int nm = fields.capacity();
367 UTIL_CHECK(nm > 0);
368 int nx = fields[0].capacity();
369 UTIL_CHECK(nx == domain().nx());
370 if (nm > 1) {
371 for (int i = 0; i < nm; ++i) {
372 UTIL_CHECK(nx == fields[i].capacity());
373 }
374 }
375
376 // Output new grid dimensions
377 out << "nx " << nx + m << std::endl;
378 out << "nm " << nm << std::endl;
379
380 // Loop over existing points;
381 int i, j;
382 for (i = 0; i < nx; ++i) {
383 out << Int(i, 5);
384 for (j = 0; j < nm; ++j) {
385 out << " " << Dbl(fields[j][i]);
386 }
387 out << std::endl;
388 }
389
390 // Add m new points, assign each the last value
391 for (i = nx; i < nx + m; ++i) {
392 out << Int(i, 5);
393 for (j = 0; j < nm; ++j) {
394 out << " " << Dbl(fields[j][nx-1]);
395 }
396 out << std::endl;
397 }
398
399 }
400
401} // namespace R1
402} // namespace Pscf
int nPolymer() const
Get number of polymer species.
int nSolvent() const
Get number of solvent (point particle) species.
int nBlock() const
Get total number blocks among all polymer species.
PolymerT & polymer(int id)
Get a polymer solver object by non-const reference.
SolventT & solvent(int id)
Get a solvent solver object.
const Vertex & vertex(int id) const
Get a specified Vertex by const reference.
int nBlock() const
Number of blocks.
PT & propagator(int blockId, int directionId)
Get the propagator for a specific block and direction (non-const).
One-dimensional spatial domain and discretization grid.
void writeVertexQ(Mixture const &mixture, int polymerId, int vertexId, std::ostream &out) const
Write product of incoming q fields for one vertex to stream.
void writeFields(DArray< Field > const &fields, std::ostream &out, bool writeHeader=true) const
Write a set of fields, one per monomer type, to an output stream.
void writeBlockCFields(Mixture const &mixture, std::ostream &out) const
Write block concentration fields for all blocks to an output stream.
void extend(DArray< Field > const &fields, int m, std::ostream &out)
Add points to the end of a field mesh and write to stream.
void remesh(DArray< Field > const &fields, int nx, std::ostream &out)
Interpolate an array of fields onto a new mesh and write to stream.
void writeField(Field const &field, std::ostream &out, bool writeHeader=true) const
Write a single field to an output stream.
void readFields(DArray< Field > &fields, std::istream &in)
Read a set of fields, one per monomer type.
void associate(Domain const &domain, FileMaster const &fileMaster)
Get and store addresses of associated objects.
Solver and descriptor for a mixture of polymers and solvents.
Descriptor and solver for a block polymer species.
A junction or chain end in a block polymer.
Definition Vertex.h:31
int size() const
Get the number of attached blocks.
Definition Vertex.h:110
Pair< int > const & inPropagatorId(int i) const
Get the block and direction of an incoming propagator.
Definition Vertex.h:114
int capacity() const
Return allocated size.
Definition Array.h:250
Dynamically allocatable contiguous array template.
Definition DArray.h:32
Dbl()
Default constructor.
Definition Dbl.cpp:17
A FileMaster manages input and output files for a simulation.
Definition FileMaster.h:143
Int()
Default constructor.
Definition Int.cpp:19
An array of exactly 2 objects.
Definition Pair.h:24
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
SCFT with real 1D fields.
PSCF package top-level namespace.
float product(float a, float b)
Product for float Data.
Definition product.h:22