9#include <r1/solvers/Polymer.h>
10#include <r1/solvers/Solvent.h>
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>
20#include <pscf/interaction/Interaction.h>
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>
44 void checkCommandInput(std::istream& in, std::string
const & command)
47 Log::file() <<
"\nError reading the arguments of command "
48 << command << std::endl;
65 iteratorFactoryPtr_(0),
84 fieldIo_.associate(domain_, fileMaster_);
97 if (interactionPtr_) {
98 delete interactionPtr_;
103 if (iteratorFactoryPtr_) {
104 delete iteratorFactoryPtr_;
109 if (sweepFactoryPtr_) {
110 delete sweepFactoryPtr_;
132 while ((c = getopt(argc, argv,
"er:p:c:i:o:f")) != -1) {
154 Log::file() <<
"Unknown option -" << optopt << std::endl;
204 iteratorPtr_ = iteratorFactoryPtr_->readObject(in, *
this,
207 std::string msg =
"Unrecognized Iterator subclass name ";
214 sweepFactoryPtr_->readObjectOptional(in, *
this,
className, isEnd);
217 sweepPtr_->setSystem(*
this);
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;
255 void System::allocateFields()
266 wFields_.allocate(nMonomer);
267 cFields_.allocate(nMonomer);
269 for (
int i = 0; i < nMonomer; ++i) {
285 std::string filename;
287 std::istream& inBuffer = in;
292 bool readNext =
true;
299 if (inBuffer.fail()) {
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")) {
313 UTIL_THROW(
"No solution for the current w fields: "
314 "use COMPUTE or ITERATE before this command");
317 if (command ==
"FINISH") {
321 if (command ==
"READ_W") {
322 readEcho(inBuffer, filename);
324 fieldIo_.readFields(
wFields(), filename);
326 if (command ==
"COMPUTE") {
331 if (command ==
"ITERATE") {
333 bool isContinuation =
false;
334 int fail =
iterate(isContinuation);
340 if (command ==
"SWEEP") {
345 if (command ==
"WRITE_PARAM") {
346 readEcho(inBuffer, filename);
352 if (command ==
"WRITE_THERMO") {
353 readEcho(inBuffer, filename);
359 if (command ==
"COMPARE_HOMOGENEOUS") {
362 checkCommandInput(inBuffer, command);
364 readEcho(inBuffer, filename);
366 Log::file() <<
"mode = " << mode << std::endl;
373 comparison.
output(mode, file);
376 if (command ==
"WRITE_W") {
377 readEcho(inBuffer, filename);
378 fieldIo_.writeFields(
wFields(), filename);
380 if (command ==
"WRITE_C") {
381 readEcho(inBuffer, filename);
382 fieldIo_.writeFields(
cFields(), filename);
384 if (command ==
"WRITE_BLOCK_C") {
385 readEcho(inBuffer, filename);
386 fieldIo_.writeBlockCFields(mixture_, filename);
388 if (command ==
"WRITE_Q_SLICE") {
389 int polymerId, blockId, directionId, segmentId;
390 readEcho(inBuffer, filename);
391 inBuffer >> polymerId;
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,
403 if (command ==
"WRITE_Q_TAIL") {
404 readEcho(inBuffer, filename);
405 int polymerId, blockId, directionId;
406 inBuffer >> polymerId;
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);
415 if (command ==
"WRITE_Q_VERTEX") {
416 int polymerId, vertexId;
417 inBuffer >> polymerId;
418 checkCommandInput(inBuffer, command);
421 <<
Int(polymerId, 5) << std::endl;
422 inBuffer >> vertexId;
423 checkCommandInput(inBuffer, command);
425 <<
Int(vertexId, 5) << std::endl;
426 inBuffer >> filename;
427 checkCommandInput(inBuffer, command);
429 <<
Str(filename, 20) << std::endl;
430 fieldIo_.writeVertexQ(mixture_, polymerId, vertexId, filename);
432 if (command ==
"WRITE_Q") {
433 readEcho(inBuffer, filename);
434 int polymerId, blockId, directionId;
435 inBuffer >> polymerId;
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);
444 if (command ==
"WRITE_Q_ALL") {
445 readEcho(inBuffer, filename);
448 if (command ==
"REMESH_W") {
451 checkCommandInput(inBuffer, command);
454 inBuffer >> filename;
455 checkCommandInput(inBuffer, command);
456 Log::file() <<
"outfile = " <<
Str(filename, 20) << std::endl;
457 fieldIo_.remesh(
wFields(), nx, filename);
459 if (command ==
"EXTEND_W") {
462 checkCommandInput(inBuffer, command);
465 inBuffer >> filename;
466 checkCommandInput(inBuffer, command);
467 Log::file() <<
"outfile = " <<
Str(filename, 20) << std::endl;
468 fieldIo_.extend(
wFields(), m, filename);
470 Log::file() <<
" Error: Unknown command "
471 << command << std::endl;
494 void System::readEcho(std::istream& in, std::string&
string)
const
498 UTIL_THROW(
"Unable to read a string parameter.");
513 mixture_.compute(
wFields(), cFields_);
575 for (
int i = 0; i < np; ++i) {
577 phi = polymerPtr->
phi();
578 mu = polymerPtr->
mu();
580 length = polymerPtr->
length();
582 fHelmholtz_ += phi*( mu - 1.0 )/length;
591 for (
int i = 0; i < ns; ++i) {
593 phi = solventPtr->
phi();
594 mu = solventPtr->
mu();
596 size = solventPtr->
size();
598 fHelmholtz_ += phi*( mu - 1.0 )/size;
604 for (
int i = 0; i < nm; ++i) {
608 fIdeal_ = fHelmholtz_;
612 if (!f_.isAllocated()) f_.allocate(nx);
613 if (!c_.isAllocated()) c_.allocate(nm);
617 for (
int i = 0; i < nx; ++i) {
619 for (j = 0; j < nm; ++j) {
620 c_[j] = cFields_[j][i];
625 for (j = 0; j < nm; ++j) {
626 for (k = 0; k < nm; ++k) {
627 f_[i] += c_[j]*chi(j, k)*c_[k];
633 fInter_ = fHelmholtz_ - fIdeal_;
636 pressure_ = -fHelmholtz_;
640 for (
int i = 0; i < np; ++i) {
642 phi = polymerPtr->
phi();
643 mu = polymerPtr->
mu();
644 length = polymerPtr->
length();
646 pressure_ += phi*mu/length;
653 for (
int i = 0; i < ns; ++i) {
655 phi = solventPtr->
phi();
656 mu = solventPtr->
mu();
657 size = solventPtr->
size();
659 pressure_ += phi*mu/size;
669 out <<
"fHelmholtz " <<
Dbl(
fHelmholtz(), 18, 11) << std::endl;
670 out <<
"pressure " <<
Dbl(
pressure(), 18, 11) << std::endl;
672 out <<
"fIdeal " <<
Dbl(fIdeal_, 18, 11) << std::endl;
673 out <<
"fInter " <<
Dbl(fInter_, 18, 11) << std::endl;
679 out <<
"polymers:" << std::endl;
684 for (
int i = 0; i < np; ++i) {
686 <<
" " <<
Dbl(
mixture().polymer(i).phi(),18, 11)
687 <<
" " <<
Dbl(
mixture().polymer(i).mu(), 18, 11)
696 out <<
"solvents:" << std::endl;
701 for (
int i = 0; i < ns; ++i) {
703 <<
" " <<
Dbl(
mixture().solvent(i).phi(),18, 11)
704 <<
" " <<
Dbl(
mixture().solvent(i).mu(), 18, 11)
720 fieldIo_.writeFields(
wFields(), filename);
729 fieldIo_.writeFields(
cFields(), filename);
739 fieldIo_.writeBlockCFields(mixture_, filename);
746 int polymerId,
int blockId,
747 int directionId,
int segmentId)
752 Polymer const& polymer = mixture_.polymer(polymerId);
758 propagator = polymer.
propagator(blockId, directionId);
759 Field const& field = propagator.
q(segmentId);
760 fieldIo_.writeField(field, filename);
767 int polymerId,
int blockId,
int directionId)
772 Polymer const& polymer = mixture_.polymer(polymerId);
778 field = polymer.
propagator(blockId, directionId).tail();
779 fieldIo_.writeField(field, filename);
786 int polymerId,
int blockId,
int directionId)
791 Polymer const& polymer = mixture_.polymer(polymerId);
798 int nslice = propagator.
ns();
802 fileMaster_.openOutputFile(filename, file);
805 file <<
"ngrid" << std::endl
806 <<
" " << domain_.nx() << std::endl
807 <<
"nslice" << std::endl
808 <<
" " << nslice << std::endl;
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);
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) {
840 writeQ(filename, ip, ib,
id);
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).
WT phi() const
Get the overall volume fraction for this species.
void allocate(int capacity)
Allocate the underlying C array.
Dbl()
Default constructor.
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 ¶mFileName)
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.
static std::ostream & file()
Get log ostream by reference.
Two-dimensional array container template (interface).
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.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
std::string toString(int n)
Return string representation of an integer.
SCFT with real 1D fields.
PSCF package top-level namespace.
void set(BracketPolicy::Type policy)
Set policy regarding use of bracket delimiters on arrays.