PSCF v1.5.0
MixtureTmpl.tpp
1#ifndef PSCF_MIXTURE_TMPL_TPP
2#define PSCF_MIXTURE_TMPL_TPP
3
4/*
5* PSCF - Mixture 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 "MixtureTmpl.h"
12#include <util/misc/Log.h>
13#include <cmath>
14#include <iomanip>
15#include <sstream>
16
17namespace Pscf
18{
19
20 /*
21 * Constructor.
22 */
23 template <class PT, class ST, typename WT>
25 : Composition<WT>(),
27 polymers_(),
28 solvents_()
29 {}
30
31 /*
32 * Get a PolymerSpecies descriptor by non-const reference.
33 */
34 template <class PT, class ST, typename WT>
35 PolymerSpecies<WT> const &
37 {
39 return polymers_[id];
40 }
41
42 /*
43 * Get a SolventSpecies descriptor by const reference.
44 */
45 template <class PT, class ST, typename WT>
46 SolventSpecies<WT> const &
48 {
50 return solvents_[id];
51 }
52
53 /*
54 * Read all parameters and initialize.
55 */
56 template <class PT, class ST, typename WT>
58 {
59 // Read nMonomer and monomers array
60 read<int>(in, "nMonomer", nMonomer_);
61 monomers_.allocate(nMonomer_);
62 for (int i = 0; i < nMonomer_; ++i) {
63 monomers_[i].setId(i);
64 }
65 readDArray< Monomer >(in, "monomers", monomers_, nMonomer_);
66
67 /*
68 * The input format for a single monomer is defined in the istream
69 * extraction operation (operator >>) for a Pscf::Monomer, in file
70 * pscf/chem/Monomer.cpp. The text representation contains only the
71 * value for the monomer statistical segment Monomer::kuhn.
72 */
73
74 // Read nPolymer (required parameter, must be > 0)
75 read<int>(in, "nPolymer", nPolymer_);
76 UTIL_CHECK(nPolymer_ > 0);
77
78 // Optionally read nSolvent, with nSolvent=0 by default
79 nSolvent_ = 0;
80 readOptional<int>(in, "nSolvent", nSolvent_);
81
82 // Read polymers and compute nBlock
83 nBlock_ = 0;
84 if (nPolymer_ > 0) {
85
86 polymers_.allocate(nPolymer_);
87 for (int i = 0; i < nPolymer_; ++i) {
89 nBlock_ = nBlock_ + polymer(i).nBlock();
90 }
91
92 // Set statistical segment lengths for all blocks
93 double kuhn;
94 int monomerId;
95 for (int i = 0; i < nPolymer_; ++i) {
96 for (int j = 0; j < polymer(i).nBlock(); ++j) {
97 monomerId = polymer(i).block(j).monomerId();
98 if (monomerId < 0 || monomerId >= nMonomer_) {
99 Log::file() << "Polymer " << i << ", block " << j
100 << ": monomerId = " << monomerId
101 << ", nMonomer = " << nMonomer_ << "\n";
102 UTIL_THROW("Block monomerId out of range");
103 }
104 kuhn = Composition<WT>::monomer(monomerId).kuhn();
105 polymer(i).block(j).setKuhn(kuhn);
106 }
107 }
108
109 }
110
111 // Read solvents
112 if (nSolvent_ > 0) {
113
114 solvents_.allocate(nSolvent_);
115 for (int i = 0; i < nSolvent_; ++i) {
117 int monomerId = solvent(i).monomerId();
118 if (monomerId < 0 || monomerId >= nMonomer_) {
119 Log::file() << "Solvent " << i << ": monomerId = "
120 << monomerId << ", nMonomer = " << nMonomer_
121 << "\n";
122 UTIL_THROW("Solvent monomerId out of range");
123 }
124 }
125
126 }
127
128 // Optionally read monomer reference value
129 vMonomer_ = 1.0; // Default value
130 readOptional(in, "vMonomer", vMonomer_);
131 if (!(vMonomer_ > 0.0)) {
132 Log::file() << "vMonomer = " << vMonomer_ << "\n";
133 UTIL_THROW("vMonomer must be positive");
134 }
135
136 // If every species is closed, the volume fractions must add up
137 // to 1, within rounding errors. (The AM iterators of pscf_rpc and
138 // pscf_rpg would converge to a total concentration equal to the
139 // sum of the volume fractions.)
141 WT sum = 0.0;
142 for (int i = 0; i < nPolymer_; ++i) {
143 sum += polymer(i).phi();
144 }
145 for (int i = 0; i < nSolvent_; ++i) {
146 sum += solvent(i).phi();
147 }
148 if (std::abs(sum - 1.0) > 1.0E-10) {
149 std::ostringstream message;
150 message << std::setprecision(16)
151 << "Volume fractions of the closed species add up "
152 << "to " << sum << ", but must add up to 1 within "
153 << "1e-10";
154 UTIL_THROW(message.str().c_str());
155 }
156 }
157
158 }
159
160}
161#endif
bool isCanonical() const
Is this mixture being treated in a canonical (closed) ensemble?
Monomer const & monomer(int id) const
Get a Monomer type descriptor by const reference.
DArray< Monomer > monomers_
SolventSpecies< WT > const & solventSpecies(int id) const final
Set a SolventSpecies descriptor object by const reference.
MixtureTmpl()
Constructor.
void readParameters(std::istream &in) override
Read parameters from file and initialize.
PolymerSpecies< WT > const & polymerSpecies(int id) const final
Get a PolymerSpecies descriptor by const reference.
double kuhn() const
Statistical segment length (random walk step size).
Definition Monomer.h:131
Descriptor for a linear or acyclic branched block polymer.
Descriptor for a solvent species.
static std::ostream & file()
Get log ostream by reference.
Definition Log.cpp:59
ScalarParam< Type > & read(std::istream &in, const char *label, Type &value)
Add and read a new required ScalarParam < Type > object.
ParamComposite()
Constructor.
void readParamComposite(std::istream &in, ParamComposite &child, bool next=true)
Add and read a required child ParamComposite.
#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
double sum(Array< double > const &in)
Compute sum of array elements (real).
Definition Reduce.cpp:19
PSCF package top-level namespace.