PSCF v1.5.0
rp/solvers/Mixture.tpp
1#ifndef RP_MIXTURE_TPP
2#define RP_MIXTURE_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "Mixture.h"
12
13#include <rp/solvers/Mixture.h>
14#include <rp/solvers/Polymer.h>
15#include <rp/solvers/Solvent.h>
16#include <rp/solvers/Block.h>
17#include <rp/solvers/Propagator.h>
18#include <rp/field/FieldIo.h>
19
20#include <pscf/field/FFT.h>
21#include <pscf/field/RField.h>
22#include <pscf/crystal/UnitCell.h>
23
24#include <pscf/backend/VecOp.h>
25#include <pscf/mesh/Mesh.h>
26#include <pscf/chem/Monomer.h>
27#include <pscf/chem/PolymerModel.h>
28
29#include <util/containers/DArray.h>
30#include <util/misc/ioUtil.h>
31#include <util/misc/FileMaster.h>
32
33#include <pscf/solvers/MixtureTmpl.tpp>
34
35namespace Pscf {
36namespace Rp {
37
38 using namespace Util;
39
40 // Construction, destruction, and initialization
41
42 /*
43 * Constructor
44 */
45 template <int D, class T>
47 : stress_(),
48 ds_(-1.0),
49 meshPtr_(nullptr),
50 unitCellPtr_(nullptr),
51 fieldIoPtr_(nullptr),
52 nParam_(0),
53 hasStress_(false),
54 isSymmetric_(false),
55 useBatchedFFT_(false)
56 { ParamComposite::setClassName("Mixture"); }
57
58 /*
59 * Destructor
60 */
61 template <int D, class T>
64
65 /*
66 * Read all parameters and initialize.
67 */
68 template <int D, class T>
69 void Mixture<D,T>::readParameters(std::istream& in)
70 {
71 // Read standard data for a mixture
73 UTIL_CHECK(nMonomer() > 0);
75
76 // Set ds_ parameter (only used in thread model)
78 // Thread model (ds_ is a required parameter)
79 ParamComposite::read(in, "ds", ds_);
80 UTIL_CHECK(ds_ > 0);
81 } else {
82 // Bead model (set to 1.0, but unused)
83 ds_ = 1.0;
84 }
85
86 // Optionally read useBatchedFFT boolean
87 useBatchedFFT_ = true;
88 ParamComposite::readOptional(in, "useBatchedFFT", useBatchedFFT_);
89
90 }
91
92 /*
93 * Create associations with Mesh, FFT, UnitCell, and WaveList objects.
94 */
95 template <int D, class T>
97 FFT<D,T> const & fft,
98 UnitCell<D> const & cell,
99 WaveList<D,T> & waveList)
100 {
101 UTIL_CHECK(nMonomer() > 0);
102 UTIL_CHECK(nPolymer() + nSolvent() > 0);
103 UTIL_CHECK(mesh.size() > 0);
104 UTIL_CHECK(fft.isSetup());
105 UTIL_CHECK(mesh.dimensions() == fft.meshDimensions());
106 UTIL_CHECK(cell.nParameter() > 0);
107
108 // Assign member variables
109 meshPtr_ = &mesh;
110 unitCellPtr_ = &cell;
111 nParam_ = cell.nParameter();
112
113 // Create associations for all blocks, set nParams in Polymer objects
114 if (nPolymer() > 0) {
115 int i, j;
116 for (i = 0; i < nPolymer(); ++i) {
117 polymer(i).setNParams(nParam_);
118 for (j = 0; j < polymer(i).nBlock(); ++j) {
119 polymer(i).block(j).associate(mesh, fft, cell, waveList);
121 }
122 }
123
124 // Create associations for all solvents (if any)
125 if (nSolvent() > 0) {
126 for (int i = 0; i < nSolvent(); ++i) {
127 solvent(i).associate(mesh);
128 }
129 }
130
131 }
132
133 /*
134 * Create an association with a FieldIo object, for file output.
135 */
136 template <int D, class T>
138 { fieldIoPtr_ = &fieldIo; }
139
140 /*
141 * Allocate internal data containers in all solvers.
142 */
143 template <int D, class T>
145 {
146 UTIL_CHECK(nMonomer() > 0);
147 UTIL_CHECK(nPolymer()+ nSolvent() > 0);
148 UTIL_CHECK(meshPtr_);
149 UTIL_CHECK(meshPtr_->size() > 0);
150 UTIL_CHECK(ds_ > 0);
151
152 // Allocate memory for all Block objects
153 if (nPolymer() > 0) {
154 allocateBlocks();
156
157 // Allocate memory for all Solvent objects
158 if (nSolvent() > 0) {
159 for (int i = 0; i < nSolvent(); ++i) {
160 solvent(i).allocate();
161 }
162 }
165 }
166
167 // Primary Computations
168
169 /*
170 * Compute concentrations (but not total free energy).
171 */
172 template <int D, class T>
174 DArray<FieldT> & cFields,
175 double phiTot)
176 {
177 UTIL_CHECK(meshPtr_);
178 UTIL_CHECK(mesh().size() > 0);
179 UTIL_CHECK(nMonomer() > 0);
180 UTIL_CHECK(nPolymer() + nSolvent() > 0);
181 UTIL_CHECK(wFields.capacity() == nMonomer());
182 UTIL_CHECK(cFields.capacity() == nMonomer());
183
184 int nm = nMonomer();
185 int monomerId;
186 int i, j;
187
188 // Clear all monomer concentration fields, check capacities
189 for (i = 0; i < nm; ++i) {
190 VecOp::eqS(cFields[i], 0.0);
191 }
192
193 // Process polymer species
194 if (nPolymer() > 0) {
195
196 // Solve MDE for all polymers
197 for (i = 0; i < nPolymer(); ++i) {
198 polymer(i).compute(wFields, phiTot);
199 }
200
201 // Add block contributions to monomer concentrations
202 for (i = 0; i < nPolymer(); ++i) {
203 for (j = 0; j < polymer(i).nBlock(); ++j) {
204 monomerId = polymer(i).block(j).monomerId();
205 UTIL_CHECK(monomerId >= 0);
206 UTIL_CHECK(monomerId < nm);
207 FieldT& monomerField = cFields[monomerId];
208 Block<D,T> const & block = polymer(i).block(j);
209 VecOp::addEqV(monomerField, block.cField());
210 }
211 }
212
213 }
214
215 // Process solvent species
216 if (nSolvent() > 0) {
217
218 // Compute solvent concentrations
219 for (i = 0; i < nSolvent(); ++i) {
220 monomerId = solvent(i).monomerId();
221 solvent(i).compute(wFields[monomerId], phiTot);
222 }
223
224 // Add solvent contributions to monomer concentrations
225 for (i = 0; i < nSolvent(); ++i) {
226 monomerId = solvent(i).monomerId();
227 UTIL_CHECK(monomerId >= 0);
228 UTIL_CHECK(monomerId < nm);
229 FieldT& monomerField = cFields[monomerId];
230 FieldT const & solventField = solvent(i).cField();
231 VecOp::addEqV(monomerField, solventField);
232 }
233
234 }
235
236 isSymmetric_ = false;
237 hasStress_ = false;
238 }
239
240 /*
241 * Set the isSymmetric boolean variable true or false.
242 */
243 template <int D, class T>
244 void Mixture<D,T>::setIsSymmetric(bool isSymmetric)
245 { isSymmetric_ = isSymmetric; }
246
247 /*
248 * Compute total SCFT stress for this mixture.
249 */
250 template <int D, class T>
251 void Mixture<D,T>::computeStress(double phiTot)
252 {
253 UTIL_CHECK(nParam_ > 0);
254
255 int i, j;
256
257 // Initialize all stress components to zero
258 stress_.clear();
259 for (i = 0; i < nParam_; ++i) {
260 stress_.append(0.0);
261 }
262
263 if (nPolymer() > 0) {
264
265 // Compute stress for each polymer
266 for (i = 0; i < nPolymer(); ++i) {
267 polymer(i).computeStress();
268 }
270 // Accumulate total stress
271 for (i = 0; i < nParam_; ++i) {
272 for (j = 0; j < nPolymer(); ++j) {
273 stress_[i] += polymer(j).stress(i);
274 }
275 }
276
277 }
278
279 // Correct for possible partial occupation of the unit cell.
280 // Used in problems that contain a Mask, e.g., thin films.
281 for (i = 0; i < nParam_; ++i) {
282 stress_[i] /= phiTot;
283 }
284
285 // Note: Solvent does not contribute to derivatives of f_Helmholtz
286 // with respect to unit cell parameters at fixed volume fractions.
287
288 hasStress_ = true;
289 }
290
291 // Parameter Modification
292
293 /*
294 * Reset statistical segment length for one monomer type.
295 */
296 template <int D, class T>
297 void Mixture<D,T>::setKuhn(int monomerId, double kuhn)
298 {
299 // Set new Kuhn length for relevant Monomer object
300 monomer(monomerId).setKuhn(kuhn);
301
302 // Update kuhn length for all blocks of this monomer type
303 for (int i = 0; i < nPolymer(); ++i) {
304 for (int j = 0; j < polymer(i).nBlock(); ++j) {
305 BlockT& block = polymer(i).block(j);
306 if (monomerId == block.monomerId()) {
307 block.setKuhn(kuhn);
308 }
309 }
311 hasStress_ = false;
312 }
313
314 /*
315 * Clear all data that depends on the unit cell parameters.
316 */
317 template <int D, class T>
319 {
320 if (nPolymer() > 0) {
321 for (int i = 0; i < nPolymer(); ++i) {
322 polymer(i).clearUnitCellData();
323 }
324 }
325 stress_.clear();
326 hasStress_ = false;
327 }
328
329 // Concentration Field Output
330
331 /*
332 * Combine cFields for all blocks and solvents into one DArray
333 */
334 template <int D, class T> void
336 const
338 int np = nSolvent() + nBlock();
339 UTIL_CHECK(np > 0);
340 UTIL_CHECK(nMonomer() > 0);
341 int nx = mesh().size();
342 UTIL_CHECK(nx > 0);
343 int i, j;
344
345 // Check allocation of blockCFields, allocate if necessary
346 // Initialize all concentration values to zero
347 if (!blockCFields.isAllocated()) {
348 blockCFields.allocate(np);
349 }
350 UTIL_CHECK(blockCFields.capacity() == np);
351 for (i = 0; i < np; ++i) {
352 if (!blockCFields[i].isAllocated()) {
353 blockCFields[i].allocate(mesh().dimensions());
354 }
355 VecOp::eqS(blockCFields[i], 0.0);
356 }
357
358 // Initialize section (block or solvent) index
359 int sectionId = 0;
360
361 // Process polymer species
362 if (nPolymer() > 0) {
363 for (i = 0; i < nPolymer(); ++i) {
364 for (j = 0; j < polymer(i).nBlock(); ++j) {
365 UTIL_CHECK(sectionId >= 0);
366 UTIL_CHECK(sectionId < np);
367 // Copy block r-grid c-field to blockCFields
368 blockCFields[sectionId] = polymer(i).block(j).cField();
369 sectionId++;
370 }
371 }
372 }
373 UTIL_CHECK(sectionId == nBlock());
374
375 // Process solvent species
376 if (nSolvent() > 0) {
377 for (i = 0; i < nSolvent(); ++i) {
378 UTIL_CHECK(sectionId >= 0);
379 UTIL_CHECK(sectionId < np);
380 // Copy solvent r-grid c-field to blockCFields
381 blockCFields[sectionId] = solvent(i).cField();
382 sectionId++;
383 }
384 }
385 UTIL_CHECK(sectionId == np);
386
387 }
388
389 /*
390 * Output all concentration fields in real space (r-grid) format for
391 * each block and solvent species to specified file.
392 */
393 template <int D, class T>
394 void
395 Mixture<D,T>::writeBlockCRGrid(std::string const & filename)
396 const
397 {
398 UTIL_CHECK(fieldIoPtr_);
399
400 // Create and allocate array to hold c field data
401 DArray<FieldT> blockCFields;
402 int np = nSolvent() + nBlock();
403 blockCFields.allocate(np);
404 for (int i = 0; i < np; i++) {
405 blockCFields[i].allocate(mesh().dimensions());
406 }
407
408 // Get c field data from the Mixture
409 createBlockCRGrid(blockCFields);
410
411 // Write block and solvent c field data to file
412 fieldIo().writeFieldsRGrid(filename, blockCFields,
413 unitCell(), isSymmetric_);
414 }
415
416 // Propagator field output
417
418 /*
419 * Write a specified slice of the propagator in r-grid format.
420 */
421 template <int D, class T>
423 std::string const & filename,
424 int polymerId,
425 int blockId,
426 int directionId,
427 int sliceId) const
428 {
429 UTIL_CHECK(fieldIoPtr_);
430 UTIL_CHECK(polymerId >= 0);
431 UTIL_CHECK(polymerId < nPolymer());
432 PolymerT const& polymerRef = polymer(polymerId);
433 UTIL_CHECK(blockId >= 0);
434 UTIL_CHECK(blockId < polymerRef.nBlock());
435 UTIL_CHECK(directionId >= 0);
436 UTIL_CHECK(directionId <= 1);
437 UTIL_CHECK(unitCell().isInitialized());
438 PropagatorT const &
439 propagator = polymerRef.propagator(blockId, directionId);
440 FieldT const & field = propagator.q(sliceId);
441 fieldIo().writeFieldRGrid(filename, field, unitCell(), isSymmetric_);
442 }
443
444 /*
445 * Write the last (tail) slice of the propagator in r-grid format.
446 */
447 template <int D, class T>
449 std::string const & filename,
450 int polymerId,
451 int blockId,
452 int directionId) const
453 {
454 UTIL_CHECK(fieldIoPtr_);
455 UTIL_CHECK(polymerId >= 0);
456 UTIL_CHECK(polymerId < nPolymer());
457 PolymerT const& polymerRef = polymer(polymerId);
458 UTIL_CHECK(blockId >= 0);
459 UTIL_CHECK(blockId < polymerRef.nBlock());
460 UTIL_CHECK(directionId >= 0);
461 UTIL_CHECK(directionId <= 1);
462 UTIL_CHECK(unitCell().isInitialized());
463 FieldT const &
464 field = polymerRef.propagator(blockId, directionId).tail();
465 fieldIo().writeFieldRGrid(filename, field, unitCell(), isSymmetric_);
466 }
467
468 /*
469 * Write the entire propagator for a specified block and direction.
470 */
471 template <int D, class T>
473 std::string const & filename,
474 int polymerId,
475 int blockId,
476 int directionId) const
477 {
478 UTIL_CHECK(fieldIoPtr_);
479 UTIL_CHECK(polymerId >= 0);
480 UTIL_CHECK(polymerId < nPolymer());
481 PolymerT const& polymerRef = polymer(polymerId);
482 UTIL_CHECK(blockId >= 0);
483 UTIL_CHECK(blockId < polymerRef.nBlock());
484 UTIL_CHECK(directionId >= 0);
485 UTIL_CHECK(directionId <= 1);
486 UTIL_CHECK(unitCell().isInitialized());
487 PropagatorT const&
488 propagator = polymerRef.propagator(blockId, directionId);
489 int ns = propagator.ns();
490
491 // Open file
492 std::ofstream file;
493 fieldIo().fileMaster().openOutputFile(filename, file);
494
495 // Write header
496 fieldIo().writeFieldHeader(file, 1, unitCell(), isSymmetric_);
497 file << "mesh" << std::endl
498 << " " << mesh().dimensions() << std::endl
499 << "nslice" << std::endl
500 << " " << ns << std::endl;
501
502 // Write data
503 bool hasHeader = false;
504 for (int i = 0; i < ns; ++i) {
505 file << "slice " << i << std::endl;
506 fieldIo().writeFieldRGrid(file, propagator.q(i), unitCell(),
507 hasHeader);
508 }
509 file.close();
510 }
511
512 /*
513 * Write propagators for all blocks of all polymers to files.
514 */
515 template <int D, class T>
516 void Mixture<D,T>::writeQAll(std::string const & basename)
517 {
518 UTIL_CHECK(fieldIoPtr_);
519 std::string filename;
520 int np, nb, ip, ib, id;
521 np = nPolymer();
522 for (ip = 0; ip < np; ++ip) {
523 nb = polymer(ip).nBlock();
524 for (ib = 0; ib < nb; ++ib) {
525 for (id = 0; id < 2; ++id) {
526 filename = basename;
527 filename += "_";
528 filename += toString(ip);
529 filename += "_";
530 filename += toString(ib);
531 filename += "_";
532 filename += toString(id);
533 filename += ".rq";
534 writeQ(filename, ip, ib, id);
535 }
536 }
537 }
538 }
539
540 // Stress Output
541
542 /*
543 * Write stress to output stream.
544 */
545 template <int D, class T>
546 void Mixture<D,T>::writeStress(std::ostream& out) const
547 {
548 UTIL_CHECK(hasStress_);
549
550 out << "stress:" << std::endl;
551
552 for (int i = 0; i < nParam_; i++) {
553 out << Int(i, 5)
554 << " "
555 << Dbl(stress_[i], 18, 11)
556 << std::endl;
557 }
558 out << std::endl;
559 }
560
561 // Private allocation function
562
563 /*
564 * Allocate memory for all blocks.
565 */
566 template <int D, class T>
567 void Mixture<D,T>::allocateBlocks()
568 {
569 const int np = CompositionT::nPolymer();
570 int i, j;
571 for (i = 0; i < np; ++i) {
572 for (j = 0; j < polymer(i).nBlock(); ++j) {
573 polymer(i).block(j).allocate(ds_, useBatchedFFT_);
574 }
575 }
576 }
577
578} // namespace Rp
579} // namespace Pscf
580#endif
Monomer const & monomer(int id) const
Description of a regular grid of points in a periodic domain.
Definition Mesh.h:61
IntVec< D > dimensions() const
Get an IntVec<D> of the grid dimensions.
Definition Mesh.h:217
int size() const
Get total number of grid points.
Definition Mesh.h:229
void readParameters(std::istream &in) override
Read parameters from file and initialize.
int nBlock() const
Number of blocks.
PT & propagator(int blockId, int directionId)
Get the propagator for a specific block and direction (non-const).
File input/output, format conversions and other utilities for fields.
virtual void readParameters(std::istream &in)
Read all parameters and initialize.
void writeQ(std::string const &filename, int polymerId, int blockId, int directionId) const
Write the complete propagator for one block, in r-grid format.
void writeQSlice(std::string const &filename, int polymerId, int blockId, int directionId, int segmentId) const
Write one slice of a propagator at fixed s in r-grid format.
Polymer< D, T > PolymerT
Polymer object type.
void writeStress(std::ostream &out) const
Write stress values to output stream.
void computeStress(double phiTot=1.0)
Compute derivatives of free energy w/ respect to cell parameters.
SolventT & solvent(int id)
void setIsSymmetric(bool isSymmetric)
Set the isSymmetric flag true or false.
void allocate()
Allocate required internal memory for all solvers.
void associate(Mesh< D > const &mesh, FFT< D, T > const &fft, UnitCell< D > const &cell, WaveList< D, T > &waveList)
Create associations with Mesh, FFT, UnitCell, and WaveList objects.
Block< D, T > BlockT
Block type, for a block in a block polymer.
void writeQTail(std::string const &filename, int polymerId, int blockId, int directionId) const
Write the final slice of a propagator in r-grid format.
FieldIo< D, T > const & fieldIo() const
Return associated FieldIo by const reference.
Mesh< D > const & mesh() const
Return associated Mesh<D> by const reference.
void writeBlockCRGrid(std::string const &filename) const
Write c fields for all blocks and solvents in r-grid format.
void createBlockCRGrid(DArray< FieldT > &blockCFields) const
Get c-fields for all blocks and solvents as array of r-grid fields.
Propagator< D, T > PropagatorT
Propagator type, for one direction within a block.
void setFieldIo(FieldIo< D, T > const &fieldIo)
Create an association with a FieldIo object.
void compute(DArray< FieldT > const &wFields, DArray< FieldT > &cFields, double phiTot=1.0)
Compute partition functions and concentrations.
UnitCell< D > const & unitCell() const
Return associated UnitCell<D> by const reference.
void setKuhn(int monomerId, double kuhn)
Reset statistical segment length for one monomer type.
void writeQAll(std::string const &basename)
Write all propagators of all blocks, each to a separate file.
PolymerT & polymer(int id)
Mixture()
Default constructor.
int nParameter() const
Get the number of parameters in the unit cell.
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:55
int capacity() const
Return allocated size.
Definition Array.h:250
bool isAllocated() const
Does this array have associated data?
Definition Array.h:236
Dynamically allocatable contiguous array template.
Definition DArray.h:32
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:262
Dbl()
Default constructor.
Definition Dbl.cpp:17
Int()
Default constructor.
Definition Int.cpp:19
ScalarParam< Type > & read(std::istream &in, const char *label, Type &value)
Add and read a new required ScalarParam < Type > object.
void setClassName(const char *className)
Set class name string.
ScalarParam< Type > & readOptional(std::istream &in, const char *label, Type &value)
Add and read a new optional ScalarParam < Type > object.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
std::string toString(int n)
Return string representation of an integer.
Definition ioUtil.cpp:52
void addEqV(Array< double > &a, Array< double > const &b)
Vector-vector in-place addition, a[i] += b[i] (real).
Definition VecOp.cpp:203
void eqS(Array< double > &a, double b)
Vector assignment, a[i] = b (real).
Definition VecOp.cpp:55
bool isThread()
Is the thread model in use ?
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.