PSCF v1.5.0
ScftThermo.tpp
1#ifndef RP_SCFT_THERMO_TPP
2#define RP_SCFT_THERMO_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 "ScftThermo.h"
12
13#include <rp/solvers/Mixture.h>
14#include <rp/solvers/Polymer.h>
15#include <rp/solvers/Solvent.h>
16#include <rp/field/Domain.h>
17#include <rp/field/CFields.h>
18#include <rp/field/WFields.h>
19#include <rp/field/Mask.h>
20
21#include <pscf/crystal/Basis.h>
22#include <pscf/crystal/UnitCell.h>
23
24#include <pscf/backend/Reduce.h>
25#include <pscf/chem/PolymerSpecies.h>
26#include <pscf/chem/SolventSpecies.h>
27#include <pscf/interaction/Interaction.h>
28#include <pscf/mesh/Mesh.h>
29
30#include <util/format/Int.h>
31#include <util/format/Dbl.h>
32
33namespace Pscf {
34namespace Rp {
35
36 using namespace Util;
37
38 /*
39 * Constructor.
40 */
41 template <int D, class T>
44 fHelmholtz_(0.0),
45 fIdeal_(0.0),
46 fInter_(0.0),
47 fExt_(0.0),
48 pressure_(0.0),
49 hasData_(false)
50 {}
52 /*
53 * Compute Helmholtz free energy and pressure.
54 */
55 template <int D, class T>
57 {
58 if (hasData_) return;
59
60 UTIL_CHECK(w().hasData());
61 UTIL_CHECK(c().hasData());
62
63 int nm = mixture().nMonomer(); // number of monomer types
64 int np = mixture().nPolymer(); // number of polymer species
65 int ns = mixture().nSolvent(); // number of solvent species
66
67 // Initialize all free energy contributions to zero
68 fHelmholtz_ = 0.0;
69 fIdeal_ = 0.0;
70 fInter_ = 0.0;
71 fExt_ = 0.0;
72
73 double phi, mu;
74
75 // Compute polymer ideal gas contributions to fIdeal_
76 if (np > 0) {
77 PolymerSpecies<double> const * polymerPtr;
78 double length;
79 for (int i = 0; i < np; ++i) {
80 polymerPtr = &mixture().polymerSpecies(i);
81 phi = polymerPtr->phi();
82 mu = polymerPtr->mu();
84 length = polymerPtr->length();
85 } else {
86 length = (double) polymerPtr->nBead();
87 }
88 if (phi > 0.0) {
89 fIdeal_ += phi*( mu - 1.0 ) / length;
90 // Recall: mu = ln(phi/q)
91 }
92 }
93 }
94
95 // Compute solvent ideal gas contributions to fIdeal_
96 if (ns > 0) {
97 SolventSpecies<double> const * solventPtr;
98 double size;
99 for (int i = 0; i < ns; ++i) {
100 solventPtr = &mixture().solventSpecies(i);
101 phi = solventPtr->phi();
102 mu = solventPtr->mu();
103 size = solventPtr->size();
104 if (phi > 0.0) {
105 fIdeal_ += phi*( mu - 1.0 )/size;
106 }
107 }
108 }
109
110 // Volume integrals with a mask: If the system has a mask, then the
111 // volume that should be used in calculating free energy/pressure
112 // is the volume available to the material, not the total unit cell
113 // volume. We thus divide all terms that involve integrating over
114 // the unit cell volume by quantity mask().phiTot(), which is the
115 // volume fraction of the unit cell that is occupied by material.
116 // This properly scales them to the correct value. fExt_, fInter_,
117 // and the Legendre transform component of fIdeal_ all require
118 // this scaling. If no mask is present, mask().phiTot() = 1 and no
119 // scaling occurs.
120 const double phiTot = mask().phiTot();
121
122 // Compute Legendre transform subtraction from fIdeal_
123 double temp = 0.0;
124 if (w().isSymmetric()) {
125 // Use expansion in symmetry-adapted orthonormal basis
126 UTIL_CHECK(w().isAllocatedBasis());
127 UTIL_CHECK(c().isAllocatedBasis());
128 const int nBasis = domain().basis().nBasis();
129 for (int i = 0; i < nm; ++i) {
130 for (int k = 0; k < nBasis; ++k) {
131 temp -= w().basis(i)[k] * c().basis(i)[k];
132 }
133 }
134 } else {
135 // Use summation over grid points
136 const int meshSize = domain().mesh().size();
137 for (int i = 0; i < nm; ++i) {
138 temp -= Reduce::innerProduct(w().rgrid(i), c().rgrid(i));
139 }
140 temp /= double(meshSize);
141 }
142 temp /= phiTot;
143 fIdeal_ += temp;
144 fHelmholtz_ += fIdeal_;
145
146 // Compute contribution from external fields, if they exist
147 if (h().hasData()) {
148 if (w().isSymmetric() && h().isSymmetric()) {
149 // Use expansion in symmetry-adapted orthonormal basis
150 UTIL_CHECK(h().isAllocatedBasis());
151 UTIL_CHECK(c().isAllocatedBasis());
152 const int nBasis = domain().basis().nBasis();
153 for (int i = 0; i < nm; ++i) {
154 for (int k = 0; k < nBasis; ++k) {
155 fExt_ += h().basis(i)[k] * c().basis(i)[k];
156 }
157 }
158 } else {
159 // Use summation over grid points
160 const int meshSize = domain().mesh().size();
161 for (int i = 0; i < nm; ++i) {
162 fExt_ += Reduce::innerProduct(h().rgrid(i), c().rgrid(i));
163 }
164 fExt_ /= double(meshSize);
165 }
166 fExt_ /= phiTot;
167 fHelmholtz_ += fExt_;
168 }
169
170 // Compute excess interaction free energy [ phi^{T}*chi*phi/2 ]
171 if (w().isSymmetric()) {
172 // Use basis expansion
173 UTIL_CHECK(c().isAllocatedBasis());
174 const int nBasis = domain().basis().nBasis();
175 double temp;
176 for (int i = 0; i < nm; ++i) {
177 for (int j = i; j < nm; ++j) {
178 const double chi = interaction().chi(i,j);
179 if (chi != 0.0) {
180 temp = 0.0;
181 for (int k = 0; k < nBasis; ++k) {
182 temp += c().basis(i)[k] * c().basis(j)[k];
183 }
184 if (i == j) {
185 fInter_ += 0.5*chi*temp;
186 } else {
187 fInter_ += chi*temp;
188 }
189 }
190 }
191 }
192 } else {
193 // Use r-grid data
194 const int meshSize = domain().mesh().size();
195 double temp;
196 for (int i = 0; i < nm; ++i) {
197 for (int j = i; j < nm; ++j) {
198 const double chi = interaction().chi(i,j);
199 if (chi != 0.0) {
200 temp = Reduce::innerProduct(c().rgrid(i), c().rgrid(j));
201 if (i == j) {
202 fInter_ += 0.5*chi*temp;
203 } else {
204 fInter_ += chi*temp;
205 }
206 }
207 }
208 }
209 fInter_ /= double(meshSize);
210 }
211 fInter_ /= phiTot;
212 fHelmholtz_ += fInter_;
213
214 // Initialize pressure (-1 x grand-canonical free energy / monomer)
215 pressure_ = -fHelmholtz_;
216
217 // Polymer chemical potential corrections to pressure
218 if (np > 0) {
219 PolymerSpecies<double> const * polymerPtr;
220 double length;
221 for (int i = 0; i < np; ++i) {
222 polymerPtr = &mixture().polymerSpecies(i);
223 phi = polymerPtr->phi();
224 mu = polymerPtr->mu();
226 length = polymerPtr->length();
227 } else {
228 length = (double) polymerPtr->nBead();
229 }
230 if (phi > 0.0) {
231 pressure_ += mu * phi / length;
232 }
233 }
234 }
235
236 // Solvent corrections to pressure
237 if (ns > 0) {
238 SolventSpecies<double> const * solventPtr;
239 double size;
240 for (int i = 0; i < ns; ++i) {
241 solventPtr = &mixture().solventSpecies(i);
242 phi = solventPtr->phi();
243 mu = solventPtr->mu();
244 size = solventPtr->size();
245 if (phi > 0.0) {
246 pressure_ += mu * phi / size;
247 }
248 }
249 }
250
251 hasData_ = true;
252 }
253
254 /*
255 * Clear stored values of thermodynamic properties.
256 */
257 template <int D, class T>
259 { hasData_ = false; }
260
261 /*
262 * Write thermodynamic properties to file.
263 */
264 template <int D, class T>
265 void ScftThermo<D,T>::write(std::ostream& out)
266 {
267 if (!hasData_) {
268 compute();
269 }
270
271 out << std::endl;
272 out << "fHelmholtz " << Dbl(fHelmholtz(), 18, 11) << std::endl;
273 out << "pressure " << Dbl(pressure(), 18, 11) << std::endl;
274 out << std::endl;
275 out << "fIdeal " << Dbl(fIdeal_, 18, 11) << std::endl;
276 out << "fInter " << Dbl(fInter_, 18, 11) << std::endl;
277 if (h().hasData()) {
278 out << "fExt " << Dbl(fExt_, 18, 11) << std::endl;
279 }
280 out << std::endl;
281
282 int np = mixture().nPolymer();
283 int ns = mixture().nSolvent();
284
285 if (np > 0) {
286 out << "polymers:" << std::endl;
287 out << " "
288 << " phi "
289 << " mu "
290 << std::endl;
291 for (int i = 0; i < np; ++i) {
292 out << Int(i, 5)
293 << " " << Dbl(mixture().polymer(i).phi(),18, 11)
294 << " " << Dbl(mixture().polymer(i).mu(), 18, 11)
295 << std::endl;
296 }
297 out << std::endl;
298 }
299
300 if (ns > 0) {
301 out << "solvents:" << std::endl;
302 out << " "
303 << " phi "
304 << " mu "
305 << std::endl;
306 for (int i = 0; i < ns; ++i) {
307 out << Int(i, 5)
308 << " " << Dbl(mixture().solvent(i).phi(),18, 11)
309 << " " << Dbl(mixture().solvent(i).mu(), 18, 11)
310 << std::endl;
311 }
312 out << std::endl;
313 }
314
315 out << "cellParams:" << std::endl;
316 for (int i = 0; i < domain().unitCell().nParameter(); ++i) {
317 out << Int(i, 5)
318 << " "
319 << Dbl(domain().unitCell().parameter(i), 18, 11)
320 << std::endl;
321 }
322 out << std::endl;
323 }
324
325} // namespace Rp
326} // namespace Pscf
327#endif
Descriptor for a linear or acyclic branched block polymer.
int nBead() const
Total number of beads in the polymer (bead model).
double length() const
Sum of the lengths of all blocks in the polymer (thread model).
ScftThermo(System< D, T > const &system)
Constructor.
bool hasData() const
Have free energies and pressure been computed?
Definition ScftThermo.h:249
void clear()
Clear all thermodynamic data.
void write(std::ostream &out)
Write SCFT thermodynamic properties to a file.
SystemConstRef< D, T > SystemConstRefT
Base class type name alias.
Definition ScftThermo.h:149
void compute()
Compute SCFT free energy density and pressure for current fields.
System< D, T > const & system() const
Get the associated System.
CFields< D, T > const & c() const
Get the concentration (c) field container.
WFields< D, CPT > const & h() const
double fHelmholtz() const
Get total Helmholtz free energy per monomer / kT.
Definition ScftThermo.h:209
Mixture< D, T > const & mixture() const
Get the Mixture.
double pressure() const
Get the precomputed pressure times monomer volume / kT.
Definition ScftThermo.h:241
WFields< D, T > const & w() const
Get the chemical potential (w) field container.
Domain< D, CPT > const & domain() const
Base class template for classes that represent a complete system.
Descriptor for a solvent species.
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
Dbl()
Default constructor.
Definition Dbl.cpp:17
Int()
Default constructor.
Definition Int.cpp:19
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
double innerProduct(Array< double > const &a, Array< double > const &b)
Compute Euclidean inner product of two real arrays .
Definition Reduce.cpp:176
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.