PSCF v1.5.0
Species.tpp
1/*
2* PSCF - Polymer Self-Consistent Field
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 "Species.h"
9#include <pscf/math/arithmetic.h>
10#include <util/misc/Log.h>
11#include <cmath>
12#include <complex>
13
14namespace Pscf {
15
16 using namespace Util;
17
18 /*
19 * Default constructor.
20 */
21 template <typename WT>
23 : phiMu_(0.0),
24 ensemble_(Ensemble::Unknown)
25 {
26 assign(phi_, 0.0);
27 assign(mu_, 0.0);
28 assign(q_, 1.0);
29 setClassName("Species");
30 }
31
32 /*
33 * Read phi or mu (but not both) and set the ensemble accordingly
34 */
35 template <typename WT>
36 void Species<WT>::readParameters(std::istream& in)
37 {
38 // Precondition
39 UTIL_CHECK(ensemble_ == Ensemble::Unknown);
40
41 // Read phi or mu (but not both)
42 bool hasPhi = readOptional(in, "phi", phiMu_).isActive();
43 if (hasPhi) {
44 if (!(std::real(phiMu_) >= 0.0 && std::real(phiMu_) <= 1.0)) {
45 Log::file() << "phi = " << phiMu_ << "\n";
46 UTIL_THROW("Volume fraction phi must lie in [0, 1]");
47 }
48 ensemble_ = Ensemble::Closed;
49 assign(phi_, phiMu_);
50 } else {
51 read(in, "mu", phiMu_);
52 // Note: This throws an Exception if phi and mu are both absent
53 ensemble_ = Ensemble::Open;
54 assign(mu_, phiMu_);
55 }
56
57 // Postcondition
58 UTIL_CHECK(ensemble_ != Ensemble::Unknown);
59 }
60
61 /*
62 * Set volume fraction (if ensemble is closed).
63 */
64 template <typename WT>
66 {
67 UTIL_CHECK(ensemble() == Ensemble::Closed);
68 UTIL_CHECK(phi >= 0.0);
69 UTIL_CHECK(phi <= 1.0);
70 assign(phiMu_, phi);
71 assign(phi_, phi);
72
73 // Note: phiMu_ stores the most recently read or set value of
74 // phi or mu (depending on the ensemble), which is the value written
75 // by ParamComposite::writeParameters.
76 }
77
78 /*
79 * Set chemical potential (if ensemble is open).
80 */
81 template <typename WT>
83 {
84 UTIL_CHECK(ensemble() == Ensemble::Open);
85 assign(phiMu_, mu);
86 assign(mu_, mu);
87
88 // Note: phiMu_ stores the most recently read or set value of
89 // phi or mu (depending on the ensemble), which is the value written
90 // by ParamComposite::writeParameters.
91 }
92
93 /*
94 * Set q and compute mu or phi (depending on ensemble).
95 */
96 template <typename WT>
98 {
99 assign(q_, q);
100 if (ensemble() == Ensemble::Closed) {
101 WT ratio;
102 div(ratio, phi_, q_);
103 assignLog(mu_, ratio);
104 // Equivalent to mu_ = std::log(phi_/q_);
105 } else
106 if (ensemble() == Ensemble::Open) {
107 WT lambda;
108 assignExp(lambda, mu_);
109 mul(phi_, lambda, q_);
110 //Equivalent to phi_ = std::exp(mu_)*q_;
111 }
112 }
113
114} // namespace Pscf
WT q() const
Get the molecular partition function for this species.
Definition Species.h:181
void setMu(double mu)
Set value of mu (chemical potential), if ensemble is closed.
Definition Species.tpp:82
Ensemble ensemble() const
Get the statistical ensemble for this species.
Definition Species.h:188
Species()
Constructor.
Definition Species.tpp:22
virtual void readParameters(std::istream &in)
Read phi or mu (but not both) and set ensemble accordingly.
Definition Species.tpp:36
WT mu() const
Get the chemical potential for this species (units kT=1).
Definition Species.h:174
void setQ(WT q)
Set q and compute phi or mu (depending on the ensemble).
Definition Species.tpp:97
WT phi() const
Get the overall volume fraction for this species.
Definition Species.h:167
void setPhi(double phi)
Set value of phi (volume fraction), if ensemble is closed.
Definition Species.tpp:65
static std::ostream & file()
Get log ostream by reference.
Definition Log.cpp:59
void setClassName(const char *className)
Set class name string.
ScalarParam< Type > & readOptional(std::istream &in, const char *label, Type &value)
Add and read a new optional ScalarParam < Type > object.
#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
Ensemble
Statistical ensemble for the number of molecules of one species.
Definition Ensemble.h:27
void div(fftw_complex &z, fftw_complex const &a, fftw_complex const &b)
Division of fftw_complex numbers, z = a / b .
void assignExp(fftw_complex &z, fftw_complex const &a)
Exponentation of a ffts_complex variable, z = exp(a).
Definition complex.cpp:17
void assign(fftw_complex &z, double const &a, double const &b)
Create an fftw_complex from real and imaginary parts, z = a + ib.
void mul(fftw_complex &z, fftw_complex const &a, fftw_complex const &b)
Multiplication of fftw_complex numbers, z = a * b.
void assignLog(fftw_complex &z, fftw_complex const &a)
Logarithm of an fftw_complex variable, z = log(a).
Definition complex.cpp:28
PSCF package top-level namespace.