PSCF v1.5.0
FhMixture.tpp
1#ifndef PSCF_FLORY_HUGGINS_MIXTURE_TPP
2#define PSCF_FLORY_HUGGINS_MIXTURE_TPP
3
4/*
5* PSCF - Molecule Self-Consistent Field Theory
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 "FhMixture.h"
12#include "FhMolecule.h"
13#include "FhClump.h"
14#include <pscf/chem/Composition.h>
15#include <pscf/chem/PolymerSpecies.h>
16#include <pscf/chem/SolventSpecies.h>
17#include <pscf/chem/Edge.h>
18
19namespace Pscf {
20
21 using namespace Util;
22
23 /*
24 * Initialize state to that of a Composition<W>.
25 *
26 * This function allocates iff necessary, and may be called more
27 * than once.
28 */
29 template <typename WT>
31 {
32
33 // Set number of molecular species and monomers
34 int nm = mixture.nMonomer();
35 int np = mixture.nPolymer();
36 int ns = mixture.nSolvent();
37 int nt = np + ns;
38
39 // Check nMonomer, set if necessary
40 if (nMonomer_ == 0) {
41 setNMonomer(nm);
42 }
43 UTIL_CHECK(nMonomer() == nm);
44 UTIL_CHECK(nMonomer_ == nm);
45 UTIL_CHECK(c_.capacity() == nm);
46 UTIL_CHECK(w_.capacity() == nm);
47
48 // Check nMolecule, set if necessary
49 if (nMolecule_ == 0) {
50 setNMolecule(nt);
51 }
52 UTIL_CHECK(nMolecule_ == nt);
53 UTIL_CHECK(molecules_.capacity() == nt);
54 UTIL_CHECK(phi_.capacity() == nt);
55 UTIL_CHECK(mu_.capacity() == nt);
56
57 /*
58 * Work space for identifying clumps for each polymer species
59 * clumpSizes[j] is the sum of block lengths of monomer type j
60 * clumpFlags[j] is true iff a molecule contain a block of type j
61 */
62 DArray<double> clumpSizes;
63 DArray<bool> clumpFlags;
64 clumpSizes.allocate(nm);
65 clumpFlags.allocate(nm);
66
67 int i; // species index
68 int j; // monomer index
69 int k; // block or clump index
70 int nb; // number of blocks in a polymer species
71 int nc; // number of clumps in a polymer species
72
73 // Loop over polymer molecule species
74 if (np > 0) {
75 for (i = 0; i < np; ++i) {
76 PolymerSpecies<WT> const& polymer = mixture.polymerSpecies(i);
77
78 // Initial array of clump sizes
79 for (j = 0; j < nm; ++j) {
80 clumpSizes[j] = 0.0;
81 clumpFlags[j] = false;
82 }
83
84 // Compute clump sizes and flags for all monomer types.
85 nb = polymer.nBlock();
86 for (k = 0; k < nb; ++k) {
87 Edge const& edge = polymer.edge(k);
88 UTIL_CHECK(edge.length() > 0);
89 j = edge.monomerId();
90 clumpSizes[j] += edge.length();
91 clumpFlags[j] = true;
92 }
93
94 // Count the number nc of clumps
95 nc = 0;
96 for (j = 0; j < nm; ++j) {
97 UTIL_CHECK(clumpSizes[j] >= 0.0);
98 if (clumpFlags[j]) {
99 UTIL_CHECK(clumpSizes[j] > 0.0);
100 ++nc;
101 } else {
102 UTIL_CHECK(clumpSizes[j] == 0.0);
103 }
104 }
105 molecule(i).setNClump(nc);
106
107 // Set clump properties for this FhMolecule species
108 k = 0; // Clump index
109 for (j = 0; j < nm; ++j) {
110 if (clumpFlags[j]) {
111 molecule(i).clump(k).setMonomerId(j);
112 molecule(i).clump(k).setSize(clumpSizes[j]);
113 ++k;
114 }
115 }
116 UTIL_CHECK(k == nc);
118
119 }
120 }
121
122 // Add solvent contributions
123 if (ns > 0) {
124 double size;
125 int monomerId;
126 for (int is = 0; is < ns; ++is) {
127 i = is + np;
128 SolventSpecies<WT> const& solvent = mixture.solventSpecies(is);
129 monomerId = solvent.monomerId();
130 size = solvent.size();
131 molecule(i).setNClump(1);
132 molecule(i).clump(0).setMonomerId(monomerId);
133 molecule(i).clump(0).setSize(size);
135 }
136 }
137
138 }
139
140} // namespace Pscf
141#endif
Descriptor for a mixture of polymer and solvent species.
Definition Composition.h:68
int nMonomer() const
Get number of monomer types.
virtual PolymerSpecies< WT > const & polymerSpecies(int id) const =0
Get a PolymerSpecies by const reference.
virtual SolventSpecies< WT > const & solventSpecies(int id) const =0
Set a solvent solver object by const reference.
int nPolymer() const
Get number of polymer species.
int nSolvent() const
Get number of solvent (point particle) species.
Descriptor for one block within a block polymer.
Definition Edge.h:62
int monomerId() const
Get the monomer type id for this block.
Definition Edge.h:287
double length() const
Get the length of this block, in the thread model.
Definition Edge.h:314
void setMonomerId(int monomerId)
Set the monomer type id.
Definition FhClump.cpp:23
void setSize(double size)
Set the size of this clump.
Definition FhClump.cpp:29
FhMolecule & molecule(int id)
Get a molecule object (non-const reference).
Definition FhMixture.h:340
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 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.
void setNClump(int nClump)
Set the number of clumps, and allocate memory.
FhClump & clump(int id)
Get a specified FhClump.
Definition FhMolecule.h:139
void computeSize()
Compute total molecule size by adding clump sizes.
Descriptor for a linear or acyclic branched block polymer.
virtual Edge & edge(int id)=0
Get a specified Edge (block descriptor) by non-const reference.
int nBlock() const
Number of blocks.
Descriptor for a solvent species.
double size() const
Get the size (number of monomers) in this solvent.
int monomerId() const
Get the monomer type id.
Dynamically allocatable contiguous array template.
Definition DArray.h:32
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:262
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
PSCF package top-level namespace.