PSCF v1.3
Species.cpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2025, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "Species.h"
9
10namespace Pscf
11{
12
13 using namespace Util;
14
15 /*
16 * Default constructor.
17 */
19 : phi_(0.0),
20 mu_(0.0),
21 q_(0.0),
22 ensemble_(Species::Closed)
23 { setClassName("Species"); }
24
25 /*
26 * Read phi or mu (but not both) and set ensemble.
27 */
28 void Species::readParameters(std::istream& in)
29 {
30 // Read phi or mu (but not both)
31 bool hasPhi = readOptional(in, "phi", phi_).isActive();
32 if (hasPhi) {
33 ensemble_ = Species::Closed;
34 } else {
35 ensemble_ = Species::Open;
36 read(in, "mu", mu_);
37 }
38 }
39
40 /*
41 * Set volume fraction (if ensemble is closed).
42 */
43 void Species::setPhi(double phi)
44 {
45 UTIL_CHECK(ensemble() == Species::Closed);
46 UTIL_CHECK(phi >= 0.0);
47 UTIL_CHECK(phi <= 1.0);
48 phi_ = phi;
49 }
50
51 /*
52 * Set chemical potential (if ensemble is open).
53 */
54 void Species::setMu(double mu)
55 {
56 UTIL_CHECK(ensemble() == Species::Open);
57 mu_ = mu;
58 }
59
60 /*
61 * Set q and compute mu or phi (depending on ensemble).
62 */
63 void Species::setQ(double q)
64 {
65 q_ = q;
66 if (ensemble() == Species::Closed) {
67 mu_ = log(phi_/q_);
68 } else
69 if (ensemble() == Species::Open) {
70 phi_ = exp(mu_)*q_;
71 }
72 }
73
74 /*
75 * Extract a Species::Ensemble from an istream as a string.
76 */
77 std::istream& operator >> (std::istream& in, Species::Ensemble& policy)
78 {
79 std::string buffer;
80 in >> buffer;
81 if (buffer == "Closed" || buffer == "closed") {
82 policy = Species::Closed;
83 } else
84 if (buffer == "Open" || buffer == "open") {
85 policy = Species::Open;
86 } else {
87 UTIL_THROW("Invalid Species::Ensemble string in operator >>");
88 }
89 return in;
90 }
92 /*
93 * Insert a Species::Ensemble to an ostream as a string.
94 */
95 std::ostream& operator<<(std::ostream& out, Species::Ensemble policy)
96 {
97 if (policy == Species::Closed) {
98 out << "Closed";
99 } else
100 if (policy == Species::Open) {
101 out << "Open";
102 } else
103 if (policy == Species::Unknown) {
104 out << "Unknown";
105 } else {
106 std::cout << "Invalid Species::Ensemble value on input"
107 << std::endl;
108 UTIL_THROW("Unrecognized value for Species::Ensemble");
109 }
110 return out;
111 }
112
113} // namespace Pscf
double phi() const
Get the overall volume fraction for this species.
Definition Species.h:149
virtual void readParameters(std::istream &in)
Read phi or mu (but not both) and set ensemble accordingly.
Definition Species.cpp:28
void setPhi(double phi)
Set value of phi (volume fraction), if ensemble is closed.
Definition Species.cpp:43
void setQ(double q)
Set q and compute phi or mu (depending on the ensemble).
Definition Species.cpp:63
double mu() const
Get the chemical potential for this species (units kT=1).
Definition Species.h:155
Ensemble ensemble() const
Get the statistical ensemble for this species (open or closed).
Definition Species.h:167
void setMu(double mu)
Set value of mu (chemical potential), if ensemble is closed.
Definition Species.cpp:54
Species()
Default constructor.
Definition Species.cpp:18
double q() const
Get the molecular partition function for this species.
Definition Species.h:161
Ensemble
Statistical ensemble for number of molecules.
Definition Species.h:40
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.
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
PSCF package top-level namespace.
Definition param_pc.dox:1
std::istream & operator>>(std::istream &in, Pair< Data > &pair)
Input a Pair from an istream.
Definition Pair.h:44
std::ostream & operator<<(std::ostream &out, const Pair< Data > &pair)
Output a Pair to an ostream, without line breaks.
Definition Pair.h:57