PSCF v1.5.0
FhMixture.cpp
1/*
2* PSCF - Molecule Self-Consistent Field Theory
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 "FhMixture.tpp"
9#include <pscf/floryHuggins/FhInteraction.h>
10#include <pscf/math/LuSolver.h>
11#include <limits>
12#include <cmath>
13
14#define PSCF_HAS_INFINITY
15
16namespace Pscf {
17
18 using namespace Util;
19
20 // Anonymous namespace (only accessible in this file)
21 namespace {
22
23 /*
24 * Is a species with chemical potential mu absent (mu = -infinity)?
25 */
26 bool isMuAbsent(double mu)
27 {
28 if (mu >= 0.0) return false;
29 if (std::numeric_limits<double>::has_infinity) {
30 return std::isinf(mu);
31 } else {
32 return (std::abs(mu) > 1.0E6);
33 }
34 }
35
36 /*
37 * Return the value of mu used to represent an absent species.
38 */
39 double muAbsent()
40 {
41 if (std::numeric_limits<double>::has_infinity) {
42 return -std::numeric_limits<double>::infinity();
43 } else {
44 return -1.0E8;
45 }
46 }
47
48 }
49
50 /*
51 * Constructor.
52 */
55 molecules_(),
56 mu_(),
57 phi_(),
58 c_(),
59 w_(),
60 residual_(),
61 dX_(),
62 dWdC_(),
63 dWdPhi_(),
64 jacobian_(),
65 lnPhi_(),
66 lnPhiOld_(),
67 fHelmholtz_(0.0),
68 pressure_(0.0),
69 solverPtr_(0),
70 nMolecule_(0),
71 nMonomer_(0),
72 hasComposition_(false)
73 { setClassName("FhMixture"); }
74
75 /*
76 * Destructor.
77 */
79 {
80 if (solverPtr_) {
81 delete solverPtr_;
82 }
83 }
84
85 /*
86 * Read all parameters and initialize.
87 */
88 void FhMixture::readParameters(std::istream& in)
89 {
90 // Precondition
91 UTIL_ASSERT(molecules_.capacity() == 0);
92
93 read<int>(in, "nMonomer", nMonomer_);
94 c_.allocate(nMonomer_);
95 w_.allocate(nMonomer_);
96
97 read<int>(in, "nMolecule", nMolecule_);
98 molecules_.allocate(nMolecule_);
99 mu_.allocate(nMolecule_);
100 phi_.allocate(nMolecule_);
101 lnPhi_.allocate(nMolecule_);
102 for (int i = 0; i < nMolecule_; ++i) {
103 readParamComposite(in, molecules_[i]);
104 }
105
106 validate();
107 }
108
110 {
111 UTIL_CHECK(molecules_.capacity() == 0);
113 nMolecule_ = nMolecule;
114 molecules_.allocate(nMolecule_);
115 mu_.allocate(nMolecule_);
116 phi_.allocate(nMolecule_);
117 lnPhi_.allocate(nMolecule_);
118 }
119
121 {
122 UTIL_CHECK(nMonomer_ == 0);
123 UTIL_CHECK(nMonomer > 0);
124 nMonomer_ = nMonomer;
125 c_.allocate(nMonomer_);
126 w_.allocate(nMonomer_);
127 }
128
129 /*
130 * Set molecular and monomer volume fractions.
131 */
133 {
134 validate();
135 UTIL_ASSERT(phi.capacity() == nMolecule_);
136
137 // Set molecular volume fractions. The phi of the species with
138 // the largest phi is 1 minus the sum of the others: that of any
139 // other species would lose its precision if it is dilute.
140 int iMax = 0;
141 for (int i = 1; i < nMolecule_; ++i) {
142 if (phi[i] > phi[iMax]) iMax = i;
143 }
144 double sum = 0;
145 for (int i = 0; i < nMolecule_; ++i) {
146 if (i == iMax) continue;
147 UTIL_CHECK(phi[i] >= 0.0);
148 UTIL_CHECK(phi[i] <= 1.0);
149 phi_[i] = phi[i];
150 sum += phi[i];
151 }
152 UTIL_CHECK(sum <= 1.0);
153 phi_[iMax] = 1.0 - sum;
154 for (int i = 0; i < nMolecule_; ++i) {
155 lnPhi_[i] = log(phi_[i]);
156 }
157
158 computeC();
159 hasComposition_ = true;
160 }
161
162 void FhMixture::computeC()
163 {
164 // Initialize monomer fractions to zero
165 int k;
166 for (k = 0; k < nMonomer_; ++k) {
167 c_[k] = 0.0;
168 }
169
170 // Compute monomer volume fractions
171 double concentration;
172 int i, j;
173 for (i = 0; i < nMolecule_; ++i) {
174 FhMolecule& mol = molecules_[i];
175 concentration = phi_[i]/mol.size();
176 for (j = 0; j < molecules_[i].nClump(); ++j) {
177 k = mol.clump(j).monomerId();
178 c_[k] += concentration*mol.clump(j).size();
179 }
180 }
181
182 // Check and normalize monomer volume fractions
183 double sum = 0.0;
184 for (k = 0; k < nMonomer_; ++k) {
185 UTIL_CHECK(c_[k] >= 0.0);
186 UTIL_CHECK(c_[k] <= 1.0);
187 sum += c_[k];
188 }
189 UTIL_CHECK(sum > 0.9999);
190 UTIL_CHECK(sum < 1.0001);
191 if (std::abs(sum - 1.0) > 1.0E-14) {
192 for (k = 0; k < nMonomer_; ++k) {
193 c_[k] /= sum;
194 }
195 }
196
197 }
198
199 /*
200 * Compute thermodynamic properties.
201 */
202 void FhMixture::computeMu(FhInteraction const & interaction,
203 double xi)
204 {
205 UTIL_CHECK(interaction.nMonomer() == nMonomer_);
206 UTIL_CHECK(hasComposition_);
207
208 // Compute monomer excess chemical potentials
209 interaction.computeW(c_, w_);
210
211 int m; // molecule index
212 int c; // clump index
213 int t; // monomer type index
214 double mu, size;
215 for (m = 0; m < nMolecule_; ++m) {
216 FhMolecule& mol = molecules_[m];
217 mu = lnPhi_[m];
218 mu += xi*mol.size();
219 for (c = 0; c < mol.nClump(); ++c) {
220 t = mol.clump(c).monomerId();
221 size = mol.clump(c).size();
222 mu += size*w_[t];
223 }
224 mu_[m] = mu;
225 }
226 }
227
228 /*
229 * Compute composition from chemical potentials.
230 */
231 void FhMixture::computePhi(FhInteraction const & interaction,
232 DArray<double> const & mu,
233 DArray<double> const & phi,
234 double& xi)
235 {
236 UTIL_CHECK(interaction.nMonomer() == nMonomer_);
237 validate();
238
239 // Allocate resources used by solver on first use
240 if (residual_.capacity() == 0) {
241 residual_.allocate(nMolecule_);
242 dX_.allocate(nMolecule_);
243 dWdC_.allocate(nMonomer_, nMonomer_);
244 dWdPhi_.allocate(nMonomer_, nMolecule_);
245 jacobian_.allocate(nMolecule_, nMolecule_);
246 lnPhiOld_.allocate(nMolecule_);
247 solverPtr_ = new LuSolver();
248 solverPtr_->allocate(nMolecule_);
249 }
250
251 // Set initial guess for the composition
252 // Set phi = 0 for any species with mu = -infinity.
253 int m1, m2; // molecule type indices
254 int mAbsent = 0;
255 for (m1 = 0; m1 < nMolecule_; ++m1) {
256 if (isMuAbsent(mu[m1])) {
257 phi_[m1] = 0.0;
258 lnPhi_[m1] = muAbsent();
259 ++mAbsent;
260 } else {
261 UTIL_CHECK(phi[m1] > 0.0);
262 UTIL_CHECK(phi[m1] <= 1.0);
263 phi_[m1] = phi[m1];
264 lnPhi_[m1] = log(phi_[m1]);
265 }
266 }
267 UTIL_CHECK(mAbsent < nMolecule_);
268 hasComposition_ = true;
269
270 double error;
271 double epsilon = 1.0E-10;
272 double s1; // clump size
273 double v1; // molecule size
274 double f1; // clump fraction
275 int c1; // clump index
276 int t1, t2; // monomer type indices
277 int mDep = -1; // index of the dependent species
278 double sum;
279
280 for (int it = 0; it < 50; ++it) {
281
282 // The dependent species is the species with the largest phi:
283 // Its phi is 1 minus the sum of the others, and xi replaces it
284 // as a Newton variable.
285
286 // Find the index mMax of the species of greatest phi
287 int mMax = 0;
288 for (m1 = 1; m1 < nMolecule_; ++m1) {
289 if (phi_[m1] > phi_[mMax]) mMax = m1;
290 }
291
292 // Check if the index of the dependent species has changed.
293 // This always occurs on the first iteration, when mDep = -1.
294 if (mMax != mDep) {
295 mDep = mMax;
296 sum = 0.0;
297 for (m1 = 0; m1 < nMolecule_; ++m1) {
298 if (m1 != mDep) sum += phi_[m1];
299 }
300 UTIL_CHECK(sum < 1.0);
301 phi_[mDep] = 1.0 - sum;
302 lnPhi_[mDep] = std::log1p(-sum);
303 computeC();
304 computeMu(interaction, xi);
305 adjustXi(mu, xi);
306 computeResidual(mu, error);
307 if (error < epsilon) return;
308 }
309
310 // Compute matrix of derivative dWdC (C = monomer fraction)
311 interaction.computeDwDc(c_, dWdC_);
312
313 // Compute matrix derivative dWdPhi (Phi = molecule fraction)
314 for (t1 = 0; t1 < nMonomer_; ++t1) {
315 for (m1 = 0; m1 < nMolecule_; ++m1) {
316 dWdPhi_(t1, m1) = 0.0;
317 }
318 }
319 for (m1 = 0; m1 < nMolecule_; ++m1) {
320 v1 = molecule(m1).size();
321 for (c1 = 0; c1 < molecule(m1).nClump(); ++c1) {
322 t1 = molecule(m1).clump(c1).monomerId();
323 s1 = molecule(m1).clump(c1).size();
324 f1 = s1/v1;
325 for (t2 = 0; t2 < nMonomer_; ++t2) {
326 dWdPhi_(t2, m1) += dWdC_(t2, t1)*f1;
327 }
328 }
329 }
330
331 // The Newton variables are xi and ln(phi) of every species other
332 // than the dependent species, with index mDep. The residual is
333 // a difference between mu and its target value, for all species.
334 // The column for xi is d( mu[m])/d xi = size[m], for all m.
335
336 // Initialize jacobian to zero
337 for (m1 = 0; m1 < nMolecule_; ++m1) {
338 for (m2 = 0; m2 < nMolecule_; ++m2) {
339 jacobian_(m1, m2) = 0.0;
340 }
341 }
342
343 // Set jacobian_ to interaction part of matrix d(mu)/d(Phi)
344 for (m1 = 0; m1 < nMolecule_; ++m1) {
345 for (c1 = 0; c1 < molecule(m1).nClump(); ++c1) {
346 t1 = molecule(m1).clump(c1).monomerId();
347 s1 = molecule(m1).clump(c1).size();
348 for (m2 = 0; m2 < nMolecule_; ++m2) {
349 jacobian_(m1, m2) += s1*dWdPhi_(t1, m2);
350 }
351 }
352 }
353 // This is now a full matrix that still contains the column with
354 // index mDep, and is d(mu)/d(phi) rather than d(mu)/d(ln phi).
355
356 // Impose incompressibility on interaction part of d(mu)/d(phi).
357 for (m1 = 0; m1 < nMolecule_; ++m1) {
358 for (m2 = 0; m2 < nMolecule_; ++m2) {
359 if (m2 != mDep) {
360 jacobian_(m1, m2) -= jacobian_(m1, mDep);
361 }
362 }
363 }
364
365 // Construct the Jacobian column d(mu)/d(xi) with index mDep
366 for (m1 = 0; m1 < nMolecule_; ++m1) {
367 jacobian_(m1, mDep) = molecule(m1).size();
368 }
369
370 // Convert d(mu)/d(phi) to d(mu)/d(ln phi), for m2 != mDep
371 for (m2 = 0; m2 < nMolecule_; ++m2) {
372 if (m2 == mDep) continue;
373 for (m1 = 0; m1 < nMolecule_; ++m1) {
374 jacobian_(m1, m2) *= phi_[m2];
375 }
376 }
377
378 // Add the ideal parts, for columns with m2 != mDep
379 for (m2 = 0; m2 < nMolecule_; ++m2) {
380 if (m2 == mDep) continue;
381 jacobian_(m2, m2) += 1.0;
382 jacobian_(mDep, m2) -= phi_[m2]/phi_[mDep];
383 }
384
385 // An absent species has a zero residual, and is assigned a unit
386 // row so that its value ln(phi) = -infinity will not be changed.
387 for (m1 = 0; m1 < nMolecule_; ++m1) {
388 if (isMuAbsent(mu[m1])) {
389 for (m2 = 0; m2 < nMolecule_; ++m2) {
390 jacobian_(m1, m2) = 0.0;
391 }
392 jacobian_(m1, m1) = 1.0;
393 }
394 }
395
396 // Newton Raphson update of phi and xi fields. The Jacobian is
397 // singular if the composition is at a spinodal of the mixture,
398 // e.g., at the critical point of a symmetric blend.
399 solverPtr_->computeLU(jacobian_);
400 if (solverPtr_->isSingular()) {
401 UTIL_THROW("Singular Jacobian (composition at a spinodal?)");
402 }
403 solverPtr_->solve(residual_, dX_);
404
405 // Store old values of ln(phi)
406 for (m1 = 0; m1 < nMolecule_; ++m1) {
407 lnPhiOld_[m1] = lnPhi_[m1];
408 }
409
410 // Apply update.
411 // Rescale if necessary to preserve incompressibility.
412 bool inRange = false;
413 for (int j = 0; j < 20; ++j) {
414
415 // Update and check volume fractions
416 sum = 0.0;
417 inRange = true;
418 m1 = 0;
419 while (m1 < nMolecule_ && inRange) {
420 if (m1 != mDep) {
421 lnPhi_[m1] = lnPhiOld_[m1] - dX_[m1];
422 if (lnPhi_[m1] <= 0) {
423 phi_[m1] = std::exp(lnPhi_[m1]);
424 sum += phi_[m1];
425 } else {
426 inRange = false;
427 }
428 }
429 ++m1;
430 }
431 if (inRange) {
432 phi_[mDep] = 1.0 - sum;
433 if (phi_[mDep] < 0) inRange = false;
434 }
435
436 // Exit loop or reduce increment
437 if (inRange) {
438 lnPhi_[mDep] = std::log1p(-sum);
439 } else {
440 for (m1 = 0; m1 < nMolecule_; ++m1) {
441 dX_[m1] *= 0.5;
442 }
443 }
444
445 }
446 if (inRange) {
447 xi = xi - dX_[mDep];
448 } else {
449 UTIL_THROW("Volume fractions remain out of range");
450 }
451
452 // Compute residual
453 computeC();
454 computeMu(interaction, xi);
455 adjustXi(mu, xi);
456 computeResidual(mu, error);
457
458 if (error < epsilon) return;
459 }
460
461 UTIL_THROW("Failed to converge");
462 }
463
464 /*
465 * Adjust Xi such that the sum of residuals is equal to zero.
466 *
467 * Apply Newton's method to a one-dimensional problem. The problem is
468 * linear, so the solution is obtained in one step.
469 */
470 void FhMixture::adjustXi(DArray<double> const & mu, double& xi)
471 {
472 double err = 0.0;
473 double jac = 0.0;
474 for (int i=0; i < nMolecule_; ++i) {
475 if (isMuAbsent(mu[i])) continue;
476 err += mu_[i] - mu[i];
477 jac += molecules_[i].size();
478 }
479 double dxi = -err/jac;
480 for (int i=0; i < nMolecule_; ++i) {
481 mu_[i] += dxi*molecules_[i].size();
482 }
483 xi += dxi;
484 }
485
486 /*
487 * Compute and store residual_ vector.
488 */
489 void FhMixture::computeResidual(DArray<double> const & mu, double& error)
490 {
491 error = 0.0;
492 for (int i = 0; i < nMolecule_; ++i) {
493 if (isMuAbsent(mu[i])) {
494 residual_[i] = 0.0;
495 continue;
496 }
497 residual_[i] = mu_[i] - mu[i];
498 if (std::abs(residual_[i]) > error) {
499 error = std::abs(residual_[i]);
500 }
501 }
502 }
503
504 /*
505 * Compute Helmoltz free energy and pressure
506 */
508 {
509 fHelmholtz_ = 0.0;
510
511 // Compute ideal gas contributions to fHelhmoltz_
512 double size;
513 for (int i = 0; i < nMolecule_; ++i) {
514 size = molecules_[i].size();
515 // phi*(log(phi) - 1) tends to 0 as phi tends to 0
516 if (phi_[i] > 0.0) {
517 fHelmholtz_ += phi_[i]*( log(phi_[i]) - 1.0 )/size;
518 }
519 }
520
521 // Add average interaction free energy density per monomer
522 fHelmholtz_ += interaction.fHelmholtz(c_);
523
524 // Compute pressure
525 pressure_ = -fHelmholtz_;
526 for (int i = 0; i < nMolecule_; ++i) {
527 size = molecules_[i].size();
528 // phi*mu tends to 0 as phi tends to 0 (mu ~ log(phi))
529 if (phi_[i] > 0.0) {
530 pressure_ += phi_[i]*mu_[i]/size;
531 }
532 }
533
534 }
535
536 /*
537 * Check validity after completing initialization.
538 */
540 {
541 UTIL_ASSERT(nMolecule_ > 0);
542 UTIL_ASSERT(nMonomer_ > 0);
543 for (int i = 0; i < nMolecule_; ++i) {
544 FhMolecule const & mol = molecules_[i];
545 UTIL_ASSERT(mol.nClump() > 0);
546 for (int j = 0; j < mol.nClump(); ++j) {
547 UTIL_ASSERT(mol.clump(j).monomerId() < nMonomer_);
548 }
549 }
550 }
551
552 // Explicit instantiation definition for member function template.
553 template
555
556} // namespace Pscf
Descriptor for a mixture of polymer and solvent species.
Definition Composition.h:68
int monomerId() const
Get the monomer type id.
Definition FhClump.h:127
double size() const
Get the size (number of monomers) in this block.
Definition FhClump.h:133
Flory-Huggins interaction model.
virtual void computeW(Array< double > const &c, Array< double > &w) const
Compute chemical potential from concentration.
int nMonomer() const
Get number of monomer types.
virtual void computeDwDc(Array< double > const &c, Matrix< double > &dWdC) const
Compute second derivatives of free energy.
virtual double fHelmholtz(Array< double > const &c) const
Compute excess Helmholtz free energy per monomer.
~FhMixture()
Destructor.
Definition FhMixture.cpp:78
void setComposition(DArray< double > const &phi)
Set system composition.
FhMolecule & molecule(int id)
Get a molecule object (non-const reference).
Definition FhMixture.h:340
FhMixture()
Constructor.
Definition FhMixture.cpp:53
int nMolecule() const
Get number of molecule species (polymer + solvent).
Definition FhMixture.h:374
void validate() const
Validate all data structures.
void computePhi(FhInteraction const &interaction, DArray< double > const &mu, DArray< double > const &phi, double &xi)
Compute composition from chemical potentials.
double mu(int id) const
Return chemical potential for one species.
Definition FhMixture.h:347
int nMonomer() const
Get number of monomer types.
Definition FhMixture.h:377
void initialize(Composition< WT > const &mixture)
Initialize to properties of a Composition descriptor.
Definition FhMixture.tpp:30
void computeMu(FhInteraction const &interaction, double xi=0.0)
Compute chemical potential from preset composition.
void computeFreeEnergy(FhInteraction const &interaction)
Compute Helmholtz free energy and pressure.
double c(int id) const
Return monomer volume fraction for one monomer type.
Definition FhMixture.h:361
void setNMolecule(int nMolecule)
Set the number of molecular species and allocate memory.
void setNMonomer(int nMonomer)
Set the number of monomer types and allocate memory.
virtual void readParameters(std::istream &in)
Read parameters from file and initialize.
Definition FhMixture.cpp:88
double phi(int id) const
Return molecular volume fraction for one species.
Definition FhMixture.h:354
Molecular species in a homogeneous Flory-Huggins mixture.
Definition FhMolecule.h:36
double size() const
Total molecule size = volume / reference volume.
Definition FhMolecule.h:129
FhClump & clump(int id)
Get a specified FhClump.
Definition FhMolecule.h:139
int nClump() const
Number of monomer clumps (monomer types).
Definition FhMolecule.h:122
LuSolver()
Constructor.
Definition LuSolver.cpp:114
int capacity() const
Return allocated size.
Definition Array.h:250
int size() const
Return logical size of array.
Definition Array.h:243
Dynamically allocatable contiguous array template.
Definition DArray.h:32
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.
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
#define UTIL_ASSERT(condition)
Assertion macro suitable for debugging serial or parallel code.
Definition global.h:75
double sum(Array< double > const &in)
Compute sum of array elements (real).
Definition Reduce.cpp:19
PSCF package top-level namespace.