PSCF v1.4.0
cpc/solvers/Solvent.tpp
1#ifndef CPC_SOLVENT_TPP
2#define CPC_SOLVENT_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2025, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "Solvent.h"
12#include <pscf/mesh/Mesh.h>
13#include <pscf/cpu/complex.h>
14
15namespace Pscf {
16namespace Cpc {
17
18 /*
19 * Constructor
20 */
21 template <int D>
23 : SolventSpecies< std::complex<double> >(),
24 meshPtr_(nullptr)
25 { ParamComposite::setClassName("Solvent"); }
26
27 /*
28 * Destructor
29 */
30 template <int D>
33
34 /*
35 * Create an association with a Mesh.
36 */
37 template <int D>
38 void Solvent<D>::associate(Mesh<D> const & mesh)
39 { meshPtr_ = &mesh; }
40
41 /*
42 * Allocate the concentration field (cField).
43 */
44 template <int D>
46 {
47 UTIL_CHECK(meshPtr_);
48 cField_.allocate(meshPtr_->dimensions());
49 }
50
51 /*
52 * Compute concentration, q, and phi or mu.
53 */
54 template <int D>
55 void Solvent<D>::compute(CField<D> const & wField)
56 {
57 int nx = meshPtr_->size(); // Number of grid points
58 UTIL_CHECK(nx > 0);
59 UTIL_CHECK(cField_.capacity() == nx);
60 UTIL_CHECK(wField.capacity() == nx);
61
62 double r;
63
64 // Initialize cField_ to zero
65 r = 0.0;
66 for (int i = 0; i < nx; ++i) {
67 assign(cField_[i], r);
68 }
69 fftw_complex Q;
70 assign(Q, r);
71
72 // Evaluate unnormalized integral and Q
73 r = - size();
74 fftw_complex z;
75 for (int i = 0; i < nx; ++i) {
76 mul(z, wField[i], r);
77 assignExp(cField_[i], z);
78 addEq(Q, cField_[i]);
79 // cField_[i] = exp(-size*wField[i]);
80 // Q += cField_[i];
81 }
82 r = (double)nx;
83 divEq(Q, r);
84 // Q /= (double)nx
85
86 // Set Q in Species base class and compute mu or phi
87 std::complex<double> Qstd;
88 assign(Qstd, Q);
90
91 // Normalize concentration
92 fftw_complex prefactor;
93 assign(z, phi());
94 div(prefactor, z, Q);
95 // prefactor = phi()/Q
96 for (int i = 0; i < nx; ++i) {
97 mulEq(cField_[i], prefactor);
98 //cField_[i] *= prefactor;
99 }
100
101 }
102
103} // namespace Cpc
104} // namespace Rpc
105#endif
void associate(Mesh< D > const &mesh)
Create an association with the mesh.
void compute(CField< D > const &wField)
Compute monomer concentration field, q and phi and/or mu.
void allocate()
Allocate memory for concentrationf field.
double size() const
Get the size (number of monomers) in this solvent.
Description of a regular grid of points in a periodic domain.
Definition Mesh.h:61
Field of complex double precision values on an FFT mesh.
Definition cpu/CField.h:29
Base class for a molecular species (polymer or solvent).
Definition Species.h:35
void setQ(std::complex< double > q)
std::complex< double > phi() const
int capacity() const
Return allocated size.
Definition Array.h:144
void setClassName(const char *className)
Set class name string.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
void mulEq(fftw_complex &a, fftw_complex const &b)
In-place multiplication of two complex number, a *= b.
void addEq(fftw_complex &a, fftw_complex const &b)
In-place addition of fftw_complex numbers, a += b.
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).
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 divEq(fftw_complex &a, fftw_complex const &b)
In-place division of fftw_complex numbers, a /= b.
Complex periodic fields, CL-FTS (CPU).
Definition cpc.mod:6
PSCF package top-level namespace.