PSCF v1.5.0
r1/system/System.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 "System.h"
9#include <r1/solvers/Polymer.h>
10#include <r1/solvers/Solvent.h>
11
12#include <r1/iterator/Iterator.h>
13#include <r1/iterator/IteratorFactory.h>
14#include <r1/sweep/Sweep.h>
15#include <r1/sweep/SweepFactory.h>
16#include <r1/iterator/NrIterator.h>
17#include <r1/system/HomogeneousComparison.h>
18#include <r1/field/FieldIo.h>
19
20#include <pscf/interaction/Interaction.h>
21
22#include <util/format/Str.h>
23#include <util/format/Int.h>
24#include <util/format/Dbl.h>
25#include <util/param/BracketPolicy.h>
26#include <util/misc/ioUtil.h>
27
28#include <string>
29#include <unistd.h>
30
31namespace Pscf {
32namespace R1
33{
34
35 using namespace Util;
36
37 namespace {
38
39 /*
40 * Stop if the arguments of a command could not be read (used in
41 * readCommands). Without this, the command would be executed with
42 * unset or old argument values.
43 */
44 void checkCommandInput(std::istream& in, std::string const & command)
45 {
46 if (in.fail()) {
47 Log::file() << "\nError reading the arguments of command "
48 << command << std::endl;
49 UTIL_THROW("Error reading command arguments");
50 }
51 }
52
53 }
54
55 /*
56 * Constructor.
57 */
59 : mixture_(),
60 domain_(),
61 fileMaster_(),
62 fieldIo_(),
63 interactionPtr_(0),
64 iteratorPtr_(0),
65 iteratorFactoryPtr_(0),
66 sweepPtr_(0),
67 sweepFactoryPtr_(0),
68 wFields_(),
69 cFields_(),
70 f_(),
71 c_(),
72 fHelmholtz_(0.0),
73 fIdeal_(0.0),
74 fInter_(0.0),
75 pressure_(0.0),
76 hasMixture_(0),
77 hasDomain_(0),
78 hasFields_(0),
79 hasCFields_(false),
80 hasSweep_(0)
81 {
82 setClassName("System");
83
84 fieldIo_.associate(domain_, fileMaster_);
85 interactionPtr_ = new Interaction();
86 iteratorFactoryPtr_ = new IteratorFactory(*this);
87 sweepFactoryPtr_ = new SweepFactory(*this);
88
89 BracketPolicy::set(BracketPolicy::Optional);
90 }
91
92 /*
93 * Destructor.
94 */
96 {
97 if (interactionPtr_) {
98 delete interactionPtr_;
99 }
100 if (iteratorPtr_) {
101 delete iteratorPtr_;
102 }
103 if (iteratorFactoryPtr_) {
104 delete iteratorFactoryPtr_;
105 }
106 if (sweepPtr_) {
107 delete sweepPtr_;
108 }
109 if (sweepFactoryPtr_) {
110 delete sweepFactoryPtr_;
111 }
112 }
113
114 /*
115 * Process command line options.
116 */
117 void System::setOptions(int argc, char **argv)
118 {
119 bool eflag = false; // echo
120 bool pFlag = false; // param file
121 bool cFlag = false; // command file
122 bool iFlag = false; // input prefix
123 bool oFlag = false; // output prefix
124 char* pArg = 0;
125 char* cArg = 0;
126 char* iArg = 0;
127 char* oArg = 0;
128
129 // Read program arguments
130 int c;
131 opterr = 0;
132 while ((c = getopt(argc, argv, "er:p:c:i:o:f")) != -1) {
133 switch (c) {
134 case 'e':
135 eflag = true;
136 break;
137 case 'p': // parameter file
138 pFlag = true;
139 pArg = optarg;
140 break;
141 case 'c': // command file
142 cFlag = true;
143 cArg = optarg;
144 break;
145 case 'i': // input prefix
146 iFlag = true;
147 iArg = optarg;
148 break;
149 case 'o': // output prefix
150 oFlag = true;
151 oArg = optarg;
152 break;
153 case '?':
154 Log::file() << "Unknown option -" << optopt << std::endl;
155 UTIL_THROW("Invalid command line option");
156 }
157 }
158
159 // Set flag to echo parameters as they are read.
160 if (eflag) {
162 }
163
164 // If option -p, set parameter file name
165 if (pFlag) {
166 fileMaster().setParamFileName(std::string(pArg));
167 }
168
169 // If option -c, set command file name
170 if (cFlag) {
171 fileMaster().setCommandFileName(std::string(cArg));
172 }
173
174 // If option -i, set path prefix for input files
175 if (iFlag) {
176 fileMaster().setInputPrefix(std::string(iArg));
177 }
178
179 // If option -o, set path prefix for output files
180 if (oFlag) {
181 fileMaster().setOutputPrefix(std::string(oArg));
182 }
183
184 }
185
186 /*
187 * Read parameters and initialize.
188 */
189 void System::readParameters(std::istream& in)
190 {
192 hasMixture_ = true;
193
194 interaction().setNMonomer(mixture().nMonomer());
196
198 hasDomain_ = true;
199 allocateFields();
200
201 // Instantiate and initialize an Iterator
202 std::string className;
203 bool isEnd;
204 iteratorPtr_ = iteratorFactoryPtr_->readObject(in, *this,
205 className, isEnd);
206 if (!iteratorPtr_) {
207 std::string msg = "Unrecognized Iterator subclass name ";
208 msg += className;
209 UTIL_THROW(msg.c_str());
210 }
211
212 // Optionally instantiate and initialize a Sweep object
213 sweepPtr_ =
214 sweepFactoryPtr_->readObjectOptional(in, *this, className, isEnd);
215 if (sweepPtr_) {
216 hasSweep_ = true;
217 sweepPtr_->setSystem(*this);
218 } else {
219 hasSweep_ = false;
220 }
221 }
222
223 /*
224 * Read default parameter file.
225 */
226 void System::readParam(std::istream& in)
227 {
228 readBegin(in, className().c_str());
229 readParameters(in);
230 readEnd(in);
231 }
232
233 /*
234 * Read default parameter file.
235 */
237 { readParam(fileMaster().paramFile()); }
238
239 /*
240 * Write parameter file, omitting the sweep block.
241 */
242 void System::writeParamNoSweep(std::ostream& out) const
243 {
244 out << "System{" << std::endl;
245 mixture_.writeParam(out);
246 interactionPtr_->writeParam(out);
247 domain_.writeParam(out);
248 iteratorPtr_->writeParam(out);
249 out << "}" << std::endl;
250 }
251
252 /*
253 * Allocate memory for fields.
254 */
255 void System::allocateFields()
256 {
257 // Preconditions
258 UTIL_CHECK(hasMixture_);
259 UTIL_CHECK(hasDomain_);
260
261 // Allocate memory in mixture
263
264 // Allocate wFields and cFields
265 int nMonomer = mixture().nMonomer();
266 wFields_.allocate(nMonomer);
267 cFields_.allocate(nMonomer);
268 int nx = domain().nx();
269 for (int i = 0; i < nMonomer; ++i) {
270 wField(i).allocate(nx);
271 cField(i).allocate(nx);
272 }
273 hasFields_ = true;
274 }
275
276 /*
277 * Read and execute commands from a specified command file.
278 */
279 int System::readCommands(std::istream &in)
280 {
281 UTIL_CHECK(hasMixture_);
282 UTIL_CHECK(hasDomain_);
283
284 std::string command;
285 std::string filename;
286
287 std::istream& inBuffer = in;
288
289 // Set to 1 if a command fails, which also stops execution
290 int status = 0;
291
292 bool readNext = true;
293 while (readNext) {
294
295 // Stop when no further command can be read. (A test of eof()
296 // here would skip a last command that is not followed by a
297 // newline.)
298 inBuffer >> command;
299 if (inBuffer.fail()) {
300 break;
301 }
302 Log::file() << command;
303
304 // Output of c fields, propagators or thermodynamic properties
305 // requires a solution for the current w fields
306 if (!hasCFields_ && (command == "WRITE_C"
307 || command == "WRITE_BLOCK_C" || command == "WRITE_THERMO"
308 || command == "COMPARE_HOMOGENEOUS"
309 || command == "WRITE_Q_SLICE" || command == "WRITE_Q_TAIL"
310 || command == "WRITE_Q_VERTEX" || command == "WRITE_Q"
311 || command == "WRITE_Q_ALL")) {
312 Log::file() << std::endl;
313 UTIL_THROW("No solution for the current w fields: "
314 "use COMPUTE or ITERATE before this command");
315 }
316
317 if (command == "FINISH") {
318 Log::file() << std::endl;
319 readNext = false;
320 } else
321 if (command == "READ_W") {
322 readEcho(inBuffer, filename);
323 hasCFields_ = false;
324 fieldIo_.readFields(wFields(), filename);
325 } else
326 if (command == "COMPUTE") {
327 // Solve the modified diffusion equation, without iteration
328 Log::file() << std::endl;
329 compute();
330 } else
331 if (command == "ITERATE") {
332 // Attempt iteratively solve a single SCFT problem
333 bool isContinuation = false;
334 int fail = iterate(isContinuation);
335 if (fail) {
336 status = 1;
337 readNext = false;
338 }
339 } else
340 if (command == "SWEEP") {
341 // Attempt to solve a sequence of SCFT problems along a line
342 // through parameter space.
343 sweep();
344 } else
345 if (command == "WRITE_PARAM") {
346 readEcho(inBuffer, filename);
347 std::ofstream file;
348 fileMaster().openOutputFile(filename, file);
349 writeParam(file);
350 file.close();
351 } else
352 if (command == "WRITE_THERMO") {
353 readEcho(inBuffer, filename);
354 std::ofstream file;
355 fileMaster().openOutputFile(filename, file, std::ios_base::app);
356 writeThermo(file);
357 file.close();
358 } else
359 if (command == "COMPARE_HOMOGENEOUS") {
360 int mode;
361 inBuffer >> mode;
362 checkCommandInput(inBuffer, command);
363
364 readEcho(inBuffer, filename);
365 Log::file() << std::endl;
366 Log::file() << "mode = " << mode << std::endl;
367
368 std::ofstream file;
369 fileMaster().openOutputFile(filename, file, std::ios_base::app);
370
371 HomogeneousComparison comparison(*this);
372 comparison.compute(mode);
373 comparison.output(mode, file);
374 file.close();
375 } else
376 if (command == "WRITE_W") {
377 readEcho(inBuffer, filename);
378 fieldIo_.writeFields(wFields(), filename);
379 } else
380 if (command == "WRITE_C") {
381 readEcho(inBuffer, filename);
382 fieldIo_.writeFields(cFields(), filename);
383 } else
384 if (command == "WRITE_BLOCK_C") {
385 readEcho(inBuffer, filename);
386 fieldIo_.writeBlockCFields(mixture_, filename);
387 } else
388 if (command == "WRITE_Q_SLICE") {
389 int polymerId, blockId, directionId, segmentId;
390 readEcho(inBuffer, filename);
391 inBuffer >> polymerId;
392 inBuffer >> blockId;
393 inBuffer >> directionId;
394 inBuffer >> segmentId;
395 checkCommandInput(inBuffer, command);
396 Log::file() << Str("polymer ID ", 21) << polymerId << "\n"
397 << Str("block ID ", 21) << blockId << "\n"
398 << Str("direction ID ", 21) << directionId << "\n"
399 << Str("segment ID ", 21) << segmentId << std::endl;
400 writeQSlice(filename, polymerId, blockId, directionId,
401 segmentId);
402 } else
403 if (command == "WRITE_Q_TAIL") {
404 readEcho(inBuffer, filename);
405 int polymerId, blockId, directionId;
406 inBuffer >> polymerId;
407 inBuffer >> blockId;
408 inBuffer >> directionId;
409 checkCommandInput(inBuffer, command);
410 Log::file() << Str("polymer ID ", 21) << polymerId << "\n"
411 << Str("block ID ", 21) << blockId << "\n"
412 << Str("direction ID ", 21) << directionId << "\n";
413 writeQTail(filename, polymerId, blockId, directionId);
414 } else
415 if (command == "WRITE_Q_VERTEX") {
416 int polymerId, vertexId;
417 inBuffer >> polymerId;
418 checkCommandInput(inBuffer, command);
419 Log::file() << std::endl;
420 Log::file() << "polymerId = "
421 << Int(polymerId, 5) << std::endl;
422 inBuffer >> vertexId;
423 checkCommandInput(inBuffer, command);
424 Log::file() << "vertexId = "
425 << Int(vertexId, 5) << std::endl;
426 inBuffer >> filename;
427 checkCommandInput(inBuffer, command);
428 Log::file() << "outfile = "
429 << Str(filename, 20) << std::endl;
430 fieldIo_.writeVertexQ(mixture_, polymerId, vertexId, filename);
431 } else
432 if (command == "WRITE_Q") {
433 readEcho(inBuffer, filename);
434 int polymerId, blockId, directionId;
435 inBuffer >> polymerId;
436 inBuffer >> blockId;
437 inBuffer >> directionId;
438 checkCommandInput(inBuffer, command);
439 Log::file() << Str("polymer ID ", 21) << polymerId << "\n"
440 << Str("block ID ", 21) << blockId << "\n"
441 << Str("direction ID ", 21) << directionId << "\n";
442 writeQ(filename, polymerId, blockId, directionId);
443 } else
444 if (command == "WRITE_Q_ALL") {
445 readEcho(inBuffer, filename);
446 writeQAll(filename);
447 } else
448 if (command == "REMESH_W") {
449 int nx;
450 inBuffer >> nx;
451 checkCommandInput(inBuffer, command);
452 Log::file() << std::endl;
453 Log::file() << "nx = " << Int(nx, 20) << std::endl;
454 inBuffer >> filename;
455 checkCommandInput(inBuffer, command);
456 Log::file() << "outfile = " << Str(filename, 20) << std::endl;
457 fieldIo_.remesh(wFields(), nx, filename);
458 } else
459 if (command == "EXTEND_W") {
460 int m;
461 inBuffer >> m;
462 checkCommandInput(inBuffer, command);
463 Log::file() << std::endl;
464 Log::file() << "m = " << Int(m, 20) << std::endl;
465 inBuffer >> filename;
466 checkCommandInput(inBuffer, command);
467 Log::file() << "outfile = " << Str(filename, 20) << std::endl;
468 fieldIo_.extend(wFields(), m, filename);
469 } else {
470 Log::file() << " Error: Unknown command "
471 << command << std::endl;
472 status = 1;
473 readNext = false;
474 }
475
476 }
477 return status;
478 }
479
480 /*
481 * Read and execute commands from the default command file.
482 */
484 {
485 if (fileMaster().commandFileName().empty()) {
486 UTIL_THROW("Empty command file name");
487 }
488 return readCommands(fileMaster().commandFile());
489 }
490
491 /*
492 * Read a string (e.g., a filename) and echo to the log file.
493 */
494 void System::readEcho(std::istream& in, std::string& string) const
495 {
496 in >> string;
497 if (in.fail()) {
498 UTIL_THROW("Unable to read a string parameter.");
499 }
500 Log::file() << " " << Str(string, 20) << std::endl;
501 }
502
503 // Primary SCFT Computations
504
505 /*
506 * Solve MDE for current w-fields, without iteration.
507 */
509 {
510 //UTIL_CHECK(hasWFields_);
511
512 // Solve the modified diffusion equation (without iteration)
513 mixture_.compute(wFields(), cFields_);
514 hasCFields_ = true;
515
516 // Free energy and pressure of these fields, so that WRITE_THERMO
517 // and COMPARE_HOMOGENEOUS after COMPUTE report the current state
519
520 //hasCFields_ = true;
521 }
522
523 /*
524 * Iteratively solve a SCFT problem for specified parameters.
525 */
526 int System::iterate(bool isContinuation)
527 {
528 // UTIL_CHECK(hasWFields_);
529 hasCFields_ = false;
530
531 Log::file() << std::endl;
532
533 // Call iterator (return 0 for convergence, 1 for failure)
534 int error = iterator().solve(isContinuation);
535
536 // If converged, compute related properties
537 if (!error) {
539 hasCFields_ = true;
541 }
542 return error;
543 }
544
545 /*
546 * Perform sweep along a line in parameter space.
547 */
549 {
550 //UTIL_CHECK(hasWFields_);
551 UTIL_CHECK(sweepPtr_);
552 Log::file() << std::endl;
553 Log::file() << std::endl;
554
555 sweepPtr_->sweep();
556 }
557
558 // Thermodynamic properties
559
560 /*
561 * Compute Helmholtz free energy and pressure
562 */
564 {
565 fHelmholtz_ = 0.0;
566 int np = mixture().nPolymer();
567 int ns = mixture().nSolvent();
568 int nm = mixture().nMonomer();
569 double phi, mu;
570
571 // Polymeric ideal gas contributions to fHelhmoltz_
572 if (np > 0) {
573 Polymer* polymerPtr;
574 double length;
575 for (int i = 0; i < np; ++i) {
576 polymerPtr = &mixture().polymer(i);
577 phi = polymerPtr->phi();
578 mu = polymerPtr->mu();
579 // Recall: mu = ln(phi/q)
580 length = polymerPtr->length();
581 if (phi > 0.0) {
582 fHelmholtz_ += phi*( mu - 1.0 )/length;
583 }
584 }
585 }
586
587 // Solvent ideal gas contributions to fHelhmoltz_
588 if (ns > 0) {
589 Solvent* solventPtr;
590 double size;
591 for (int i = 0; i < ns; ++i) {
592 solventPtr = &mixture().solvent(i);
593 phi = solventPtr->phi();
594 mu = solventPtr->mu();
595 // Recall: mu = ln(phi/q)
596 size = solventPtr->size();
597 if (phi > 0.0) {
598 fHelmholtz_ += phi*( mu - 1.0 )/size;
599 }
600 }
601 }
602
603 // Apply Legendre transform subtraction
604 for (int i = 0; i < nm; ++i) {
605 fHelmholtz_ -=
606 domain().innerProduct(wFields_[i], cFields_[i]);
607 }
608 fIdeal_ = fHelmholtz_;
609
610 // Add average interaction free energy density per monomer
611 int nx = domain().nx();
612 if (!f_.isAllocated()) f_.allocate(nx);
613 if (!c_.isAllocated()) c_.allocate(nm);
614 int j, k;
615 // FhInteraction fhInteraction(interaction());
616 Matrix<double> const & chi = interaction().chi();
617 for (int i = 0; i < nx; ++i) {
618 // Store c_[j] = local concentration of species j
619 for (j = 0; j < nm; ++j) {
620 c_[j] = cFields_[j][i];
621 }
622 // Compute f_[i] = excess free eenrgy at grid point i
623 // f_[i] = fhInteraction.fHelmholtz(c_);
624 f_[i] = 0.0;
625 for (j = 0; j < nm; ++j) {
626 for (k = 0; k < nm; ++k) {
627 f_[i] += c_[j]*chi(j, k)*c_[k];
628 }
629 }
630 f_[i] *= 0.5;
631 }
632 fHelmholtz_ += domain().spatialAverage(f_);
633 fInter_ = fHelmholtz_ - fIdeal_;
634
635 // Compute pressure
636 pressure_ = -fHelmholtz_;
637 if (np > 0) {
638 double length;
639 Polymer* polymerPtr;
640 for (int i = 0; i < np; ++i) {
641 polymerPtr = & mixture().polymer(i);
642 phi = polymerPtr->phi();
643 mu = polymerPtr->mu();
644 length = polymerPtr->length();
645 if (phi > 0.0) {
646 pressure_ += phi*mu/length;
647 }
648 }
649 }
650 if (ns > 0) {
651 double size;
652 Solvent* solventPtr;
653 for (int i = 0; i < ns; ++i) {
654 solventPtr = & mixture().solvent(i);
655 phi = solventPtr->phi();
656 mu = solventPtr->mu();
657 size = solventPtr->size();
658 if (phi > 0.0) {
659 pressure_ += phi*mu/size;
660 }
661 }
662 }
663
664 }
665
666 void System::writeThermo(std::ostream& out) const
667 {
668 out << std::endl;
669 out << "fHelmholtz " << Dbl(fHelmholtz(), 18, 11) << std::endl;
670 out << "pressure " << Dbl(pressure(), 18, 11) << std::endl;
671 out << std::endl;
672 out << "fIdeal " << Dbl(fIdeal_, 18, 11) << std::endl;
673 out << "fInter " << Dbl(fInter_, 18, 11) << std::endl;
674 out << std::endl;
675
676 // Polymers
677 int np = mixture().nPolymer();
678 if (np > 0) {
679 out << "polymers:" << std::endl;
680 out << " "
681 << " phi "
682 << " mu "
683 << std::endl;
684 for (int i = 0; i < np; ++i) {
685 out << Int(i, 5)
686 << " " << Dbl(mixture().polymer(i).phi(),18, 11)
687 << " " << Dbl(mixture().polymer(i).mu(), 18, 11)
688 << std::endl;
689 }
690 out << std::endl;
691 }
692
693 // Solvents
694 int ns = mixture().nSolvent();
695 if (ns > 0) {
696 out << "solvents:" << std::endl;
697 out << " "
698 << " phi "
699 << " mu "
700 << std::endl;
701 for (int i = 0; i < ns; ++i) {
702 out << Int(i, 5)
703 << " " << Dbl(mixture().solvent(i).phi(),18, 11)
704 << " " << Dbl(mixture().solvent(i).mu(), 18, 11)
705 << std::endl;
706 }
707 out << std::endl;
708 }
709
710 }
711
712 // Output operations (correspond to command file commands)
713
714 /*
715 * Write w-fields in symmetry-adapted basis format.
716 */
717 void System::writeW(std::string const & filename) const
718 {
719 //UTIL_CHECK(hasWFields_);
720 fieldIo_.writeFields(wFields(), filename);
721 }
722
723 /*
724 * Write all concentration fields in symmetry-adapted basis format.
725 */
726 void System::writeC(std::string const & filename) const
727 {
728 //UTIL_CHECK(hasCFields_);
729 fieldIo_.writeFields(cFields(), filename);
730 }
731
732 /*
733 * Write all concentration fields in real space (r-grid) format, for
734 * each block (or solvent) individually rather than for each species.
735 */
736 void System::writeBlockC(std::string const & filename) const
737 {
738 //UTIL_CHECK(hasCFields_);
739 fieldIo_.writeBlockCFields(mixture_, filename);
740 }
741
742 /*
743 * Write the last time slice of the propagator in r-grid format.
744 */
745 void System::writeQSlice(const std::string & filename,
746 int polymerId, int blockId,
747 int directionId, int segmentId)
748 const
749 {
750 UTIL_CHECK(polymerId >= 0);
751 UTIL_CHECK(polymerId < mixture_.nPolymer());
752 Polymer const& polymer = mixture_.polymer(polymerId);
753 UTIL_CHECK(blockId >= 0);
754 UTIL_CHECK(blockId < polymer.nBlock());
755 UTIL_CHECK(directionId >= 0);
756 UTIL_CHECK(directionId <= 1);
757 Propagator const &
758 propagator = polymer.propagator(blockId, directionId);
759 Field const& field = propagator.q(segmentId);
760 fieldIo_.writeField(field, filename);
761 }
762
763 /*
764 * Write the last time slice of the propagator in r-grid format.
765 */
766 void System::writeQTail(const std::string & filename,
767 int polymerId, int blockId, int directionId)
768 const
769 {
770 UTIL_CHECK(polymerId >= 0);
771 UTIL_CHECK(polymerId < mixture_.nPolymer());
772 Polymer const& polymer = mixture_.polymer(polymerId);
773 UTIL_CHECK(blockId >= 0);
774 UTIL_CHECK(blockId < polymer.nBlock());
775 UTIL_CHECK(directionId >= 0);
776 UTIL_CHECK(directionId <= 1);
777 Field const&
778 field = polymer.propagator(blockId, directionId).tail();
779 fieldIo_.writeField(field, filename);
780 }
781
782 /*
783 * Write the propagator for a block and direction.
784 */
785 void System::writeQ(const std::string & filename,
786 int polymerId, int blockId, int directionId)
787 const
788 {
789 UTIL_CHECK(polymerId >= 0);
790 UTIL_CHECK(polymerId < mixture_.nPolymer());
791 Polymer const& polymer = mixture_.polymer(polymerId);
792 UTIL_CHECK(blockId >= 0);
793 UTIL_CHECK(blockId < polymer.nBlock());
794 UTIL_CHECK(directionId >= 0);
795 UTIL_CHECK(directionId <= 1);
796 Propagator const& propagator
797 = polymer.propagator(blockId, directionId);
798 int nslice = propagator.ns();
799
800 // Open file
801 std::ofstream file;
802 fileMaster_.openOutputFile(filename, file);
803
804 // Write header
805 file << "ngrid" << std::endl
806 << " " << domain_.nx() << std::endl
807 << "nslice" << std::endl
808 << " " << nslice << std::endl;
809
810 // Write data
811 bool hasHeader = false;
812 for (int i = 0; i < nslice; ++i) {
813 file << "slice " << i << std::endl;
814 Field const& field = propagator.q(i);
815 fieldIo_.writeField(field, file, hasHeader);
816 }
817 file.close();
818 }
819
820 /*
821 * Write propagators for all blocks of all polymers to files.
822 */
823 void System::writeQAll(std::string const & basename) const
824 {
825 std::string filename;
826 int np, nb, ip, ib, id;
827 np = mixture_.nPolymer();
828 for (ip = 0; ip < np; ++ip) {
829 nb = mixture_.polymer(ip).nBlock();
830 for (ib = 0; ib < nb; ++ib) {
831 for (id = 0; id < 2; ++id) {
832 filename = basename;
833 filename += "_";
834 filename += toString(ip);
835 filename += "_";
836 filename += toString(ib);
837 filename += "_";
838 filename += toString(id);
839 //filename += ".q";
840 writeQ(filename, ip, ib, id);
841 }
842 }
843 }
844 }
845
846} // namespace R1
847} // namespace Pscf
int nMonomer() const
Get number of monomer types.
int nPolymer() const
Get number of polymer species.
int nSolvent() const
Get number of solvent (point particle) species.
Interaction(bool isCompressible=false)
Constructor.
DMatrix< double > const & chi() const
Return the chi matrix by const reference.
void setNMonomer(int nMonomer)
Set the number of monomer types.
PolymerT & polymer(int id)
Get a polymer solver object by non-const reference.
SolventT & solvent(int id)
Get a solvent solver object.
double length() const
Sum of the lengths of all blocks in the polymer (thread model).
int nBlock() const
Number of blocks.
PT & propagator(int blockId, int directionId)
Get the propagator for a specific block and direction (non-const).
int nx() const
Get number of spatial grid points, including both endpoints.
double innerProduct(Field const &f, Field const &g) const
Compute inner product of two real fields.
double spatialAverage(Field const &f) const
Compute spatial average of a field.
Command to compute properties of homogeneous reference system.
void output(int mode, std::ostream &out) const
Output comparison to a homogeneous reference system.
void compute(int mode)
Compute properties of a homogeneous reference system.
IteratorFactory(System &system)
Constructor.
virtual int solve(bool isContinuation=false)=0
Iterate to solution.
void setDomain(Domain const &domain)
Create an association with the domain and allocate memory.
Descriptor and solver for a block polymer species.
MDE solver for one-direction of one block.
FieldT const & q(int i) const
Return q-field at specified step.
int ns() const
Number of values of s (or slices), including head and tail.
Solver and descriptor for a solvent species in a mixture.
SweepFactory(System &system)
Constructor.
Field & cField(int monomerId)
Get chemical potential field for a specific monomer type.
FileMaster & fileMaster()
Get FileMaster by reference.
void writeQ(std::string const &filename, int polymerId, int blockId, int directionId) const
Write one propagator for one block, in r-grid format.
void compute()
Solve the modified diffusion equation once, without iteration.
void sweep()
Sweep in parameter space, solving an SCF problem at each point.
void readParam()
Read input parameters from default param file.
void writeThermo(std::ostream &out) const
Write thermodynamic properties to a file.
void writeQTail(std::string const &filename, int polymerId, int blockId, int directionId) const
Write the final slice of a propagator in r-grid format.
virtual void readParameters(std::istream &in)
Read input parameters (without opening and closing lines).
void setOptions(int argc, char **argv)
Process command line options.
void writeW(std::string const &filename) const
Write chemical potential fields in symmetrized basis format.
Mixture & mixture()
Get Mixture by reference.
void writeParamNoSweep(std::ostream &out) const
Write parameter file to an ostream, omitting any Sweep block.
Iterator & iterator()
Get the Iterator by reference.
DArray< Field > & cFields()
Get array of all chemical potential fields.
int readCommands()
Read commands from default command file.
int iterate(bool isContinuation=false)
Iteratively solve a SCFT problem.
double pressure() const
Get precomputed pressure x monomer volume kT.
void computeFreeEnergy()
Compute free energy density and pressure for current fields.
int readCommands(std::istream &in)
Read command script.
WField & wField(int monomerId)
Get chemical potential field for a specific monomer type.
DArray< WField > & wFields()
Get array of all chemical potential fields.
virtual void readParam(std::istream &in)
Read input parameters (with opening and closing lines).
void writeQAll(std::string const &basename) const
Write all propagators of all blocks, each to a separate file.
Interaction & interaction()
Get interaction (i.e., excess free energy) by reference.
void writeBlockC(std::string const &filename) const
Write c-fields for all blocks and solvents in r-grid format.
Domain & domain()
Get spatial domain (including grid info) by reference.
void writeQSlice(std::string const &filename, int polymerId, int blockId, int directionId, int segmentId) const
Write slice of a propagator at fixed s in r-grid format.
DArray< double > Field
Generic Field type.
double fHelmholtz() const
Get precomputed Helmholtz free energy per monomer / kT.
void writeC(std::string const &filename) const
Write concentration fields in symmetrized basis format.
double size() const
Get the size (number of monomers) in this solvent.
WT mu() const
Get the chemical potential for this species (units kT=1).
Definition Species.h:174
WT phi() const
Get the overall volume fraction for this species.
Definition Species.h:167
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:262
Dbl()
Default constructor.
Definition Dbl.cpp:17
void openOutputFile(const std::string &filename, std::ofstream &out, std::ios_base::openmode mode=std::ios_base::out) const
Open an output file.
void setParamFileName(const std::string &paramFileName)
Set the parameter file name.
void setOutputPrefix(const std::string &outputPrefix)
Set the output file prefix string.
void setCommandFileName(const std::string &commandFileName)
Set the command file name.
void setInputPrefix(const std::string &inputPrefix)
Set the input file prefix string.
Int()
Default constructor.
Definition Int.cpp:19
static std::ostream & file()
Get log ostream by reference.
Definition Log.cpp:59
Two-dimensional array container template (interface).
Definition Matrix.h:32
static void setEcho(bool echo=true)
Enable or disable echoing for all subclasses of ParamComponent.
Begin & readBegin(std::istream &in, const char *label, bool isRequired=true)
Add and read a class label and opening bracket.
void setClassName(const char *className)
Set class name string.
virtual void writeParam(std::ostream &out) const
Write all parameters to an output stream.
std::string className() const
Get class name string.
void readParamComposite(std::istream &in, ParamComposite &child, bool next=true)
Add and read a required child ParamComposite.
End & readEnd(std::istream &in)
Add and read the closing bracket.
Str()
Default constructor.
Definition Str.cpp:17
#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
std::string toString(int n)
Return string representation of an integer.
Definition ioUtil.cpp:52
SCFT with real 1D fields.
PSCF package top-level namespace.
void set(BracketPolicy::Type policy)
Set policy regarding use of bracket delimiters on arrays.