8#include "FhMixture.tpp"
9#include <pscf/floryHuggins/FhInteraction.h>
10#include <pscf/math/LuSolver.h>
14#define PSCF_HAS_INFINITY
26 bool isMuAbsent(
double mu)
28 if (mu >= 0.0)
return false;
29 if (std::numeric_limits<double>::has_infinity) {
30 return std::isinf(mu);
32 return (std::abs(mu) > 1.0E6);
41 if (std::numeric_limits<double>::has_infinity) {
42 return -std::numeric_limits<double>::infinity();
72 hasComposition_(false)
94 c_.allocate(nMonomer_);
95 w_.allocate(nMonomer_);
98 molecules_.allocate(nMolecule_);
99 mu_.allocate(nMolecule_);
100 phi_.allocate(nMolecule_);
101 lnPhi_.allocate(nMolecule_);
102 for (
int i = 0; i < nMolecule_; ++i) {
114 molecules_.allocate(nMolecule_);
115 mu_.allocate(nMolecule_);
116 phi_.allocate(nMolecule_);
117 lnPhi_.allocate(nMolecule_);
125 c_.allocate(nMonomer_);
126 w_.allocate(nMonomer_);
141 for (
int i = 1; i < nMolecule_; ++i) {
142 if (
phi[i] >
phi[iMax]) iMax = i;
145 for (
int i = 0; i < nMolecule_; ++i) {
146 if (i == iMax)
continue;
153 phi_[iMax] = 1.0 - sum;
154 for (
int i = 0; i < nMolecule_; ++i) {
155 lnPhi_[i] = log(phi_[i]);
159 hasComposition_ =
true;
162 void FhMixture::computeC()
166 for (k = 0; k < nMonomer_; ++k) {
171 double concentration;
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();
184 for (k = 0; k < nMonomer_; ++k) {
191 if (std::abs(sum - 1.0) > 1.0E-14) {
192 for (k = 0; k < nMonomer_; ++k) {
215 for (m = 0; m < nMolecule_; ++m) {
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_);
248 solverPtr_->allocate(nMolecule_);
255 for (m1 = 0; m1 < nMolecule_; ++m1) {
256 if (isMuAbsent(
mu[m1])) {
258 lnPhi_[m1] = muAbsent();
264 lnPhi_[m1] = log(phi_[m1]);
268 hasComposition_ =
true;
271 double epsilon = 1.0E-10;
280 for (
int it = 0; it < 50; ++it) {
288 for (m1 = 1; m1 < nMolecule_; ++m1) {
289 if (phi_[m1] > phi_[mMax]) mMax = m1;
297 for (m1 = 0; m1 < nMolecule_; ++m1) {
298 if (m1 != mDep) sum += phi_[m1];
301 phi_[mDep] = 1.0 - sum;
302 lnPhi_[mDep] = std::log1p(-sum);
306 computeResidual(
mu, error);
307 if (error < epsilon)
return;
314 for (t1 = 0; t1 < nMonomer_; ++t1) {
315 for (m1 = 0; m1 < nMolecule_; ++m1) {
316 dWdPhi_(t1, m1) = 0.0;
319 for (m1 = 0; m1 < nMolecule_; ++m1) {
325 for (t2 = 0; t2 < nMonomer_; ++t2) {
326 dWdPhi_(t2, m1) += dWdC_(t2, t1)*f1;
337 for (m1 = 0; m1 < nMolecule_; ++m1) {
338 for (m2 = 0; m2 < nMolecule_; ++m2) {
339 jacobian_(m1, m2) = 0.0;
344 for (m1 = 0; m1 < nMolecule_; ++m1) {
348 for (m2 = 0; m2 < nMolecule_; ++m2) {
349 jacobian_(m1, m2) += s1*dWdPhi_(t1, m2);
357 for (m1 = 0; m1 < nMolecule_; ++m1) {
358 for (m2 = 0; m2 < nMolecule_; ++m2) {
360 jacobian_(m1, m2) -= jacobian_(m1, mDep);
366 for (m1 = 0; m1 < nMolecule_; ++m1) {
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];
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];
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;
392 jacobian_(m1, m1) = 1.0;
399 solverPtr_->computeLU(jacobian_);
400 if (solverPtr_->isSingular()) {
401 UTIL_THROW(
"Singular Jacobian (composition at a spinodal?)");
403 solverPtr_->solve(residual_, dX_);
406 for (m1 = 0; m1 < nMolecule_; ++m1) {
407 lnPhiOld_[m1] = lnPhi_[m1];
412 bool inRange =
false;
413 for (
int j = 0; j < 20; ++j) {
419 while (m1 < nMolecule_ && inRange) {
421 lnPhi_[m1] = lnPhiOld_[m1] - dX_[m1];
422 if (lnPhi_[m1] <= 0) {
423 phi_[m1] = std::exp(lnPhi_[m1]);
432 phi_[mDep] = 1.0 - sum;
433 if (phi_[mDep] < 0) inRange =
false;
438 lnPhi_[mDep] = std::log1p(-sum);
440 for (m1 = 0; m1 < nMolecule_; ++m1) {
449 UTIL_THROW(
"Volume fractions remain out of range");
456 computeResidual(
mu, error);
458 if (error < epsilon)
return;
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();
479 double dxi = -err/jac;
480 for (
int i=0; i < nMolecule_; ++i) {
481 mu_[i] += dxi*molecules_[i].size();
489 void FhMixture::computeResidual(
DArray<double> const & mu,
double& error)
492 for (
int i = 0; i < nMolecule_; ++i) {
493 if (isMuAbsent(
mu[i])) {
497 residual_[i] = mu_[i] -
mu[i];
498 if (std::abs(residual_[i]) > error) {
499 error = std::abs(residual_[i]);
513 for (
int i = 0; i < nMolecule_; ++i) {
514 size = molecules_[i].size();
517 fHelmholtz_ += phi_[i]*( log(phi_[i]) - 1.0 )/size;
525 pressure_ = -fHelmholtz_;
526 for (
int i = 0; i < nMolecule_; ++i) {
527 size = molecules_[i].size();
530 pressure_ += phi_[i]*mu_[i]/size;
543 for (
int i = 0; i < nMolecule_; ++i) {
546 for (
int j = 0; j < mol.
nClump(); ++j) {
Descriptor for a mixture of polymer and solvent species.
int monomerId() const
Get the monomer type id.
double size() const
Get the size (number of monomers) in this block.
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.
void setComposition(DArray< double > const &phi)
Set system composition.
FhMolecule & molecule(int id)
Get a molecule object (non-const reference).
int nMolecule() const
Get number of molecule species (polymer + solvent).
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.
int nMonomer() const
Get number of monomer types.
void initialize(Composition< WT > const &mixture)
Initialize to properties of a Composition descriptor.
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.
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.
double phi(int id) const
Return molecular volume fraction for one species.
Molecular species in a homogeneous Flory-Huggins mixture.
double size() const
Total molecule size = volume / reference volume.
FhClump & clump(int id)
Get a specified FhClump.
int nClump() const
Number of monomer clumps (monomer types).
int capacity() const
Return allocated size.
int size() const
Return logical size of array.
Dynamically allocatable contiguous array template.
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.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
#define UTIL_ASSERT(condition)
Assertion macro suitable for debugging serial or parallel code.
double sum(Array< double > const &in)
Compute sum of array elements (real).
PSCF package top-level namespace.