PSCF v1.5.0
AmCompressor.tpp
1#ifndef RP_AM_COMPRESSOR_TPP
2#define RP_AM_COMPRESSOR_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
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 "AmCompressor.h"
12
13#include <rp/system/System.h>
14#include <rp/solvers/Mixture.h>
15#include <rp/field/Domain.h>
16#include <rp/field/WFields.h>
17#include <rp/field/CFields.h>
18
19#include <pscf/field/RField.h>
20
21#include <pscf/backend/Reduce.h>
22#include <pscf/backend/VecOp.h>
23#include <pscf/math/IntVec.h>
24#include <util/global.h>
25
26#include <pscf/iterator/AmIteratorTmpl.tpp> // base class implementation
27
28
29namespace Pscf {
30namespace Rp {
31
32 using namespace Util;
33
34 // Public member functions
35
36 /*
37 * Constructor.
38 */
39 template <int D, class T>
41 : isAllocated_(false)
42 {
44 ParamComposite::setClassName("AmCompressor");
45 }
46
47 /*
48 * Read parameters from file.
49 */
50 template <int D, class T>
51 void AmCompressor<D,T>::readParameters(std::istream& in)
52 {
53 // Default values
54 AmTmplT::maxItr_ = 100;
56 AmTmplT::errorType_ = "rms";
57 bool useLambdaRamp = false;
58
61 AmTmplT::readMixingParameters(in, useLambdaRamp);
62 }
63
64 /*
65 * Initialize just before entry to iterative loop.
66 */
67 template <int D, class T>
68 void AmCompressor<D,T>::setup(bool isContinuation)
69 {
70 // Allocate memory required by AM algorithm if not done earlier.
71 AmTmplT::setup(isContinuation);
72
73 const int nMonomer = system().mixture().nMonomer();
74 const IntVec<D> dimensions = system().domain().mesh().dimensions();
75
76 // Allocate memory required by compressor if not done earlier.
77 if (!isAllocated_) {
78 w0_.allocate(nMonomer);
79 wFieldTmp_.allocate(nMonomer);
80 for (int i = 0; i < nMonomer; ++i) {
81 w0_[i].allocate(dimensions);
82 wFieldTmp_[i].allocate(dimensions);
83 }
84 isAllocated_ = true;
85 }
86
87 // Store value of initial guess chemical potential fields
88 for (int i = 0; i < nMonomer; ++i) {
89 VecOp::eqV(w0_[i], system().w().rgrid(i));
90 }
91 }
92
93 /*
94 * Main function - identify partial saddle-point state.
95 */
96 template <int D, class T>
99
100 /*
101 * Output timer information, if requested.
102 */
103 template <int D, class T>
104 void AmCompressor<D,T>::outputTimers(std::ostream& out) const
105 {
106 out << "\n";
107 out << "Compressor time contributions:\n";
109 }
110
111 /*
112 * Clear timers and MDE counter.
113 */
114 template <int D, class T>
120
121 // Private virtual functions that interact with the parent System
122
123 /*
124 * Compute and return the number of elements in a field vector.
125 */
126 template <int D, class T>
127 int AmCompressor<D,T>::nElements()
128 { return system().domain().mesh().size(); }
129
130 /*
131 * Does the system have an initial field guess?
132 */
133 template <int D, class T>
134 bool AmCompressor<D,T>::hasInitialGuess()
135 { return system().w().hasData(); }
136
137 /*
138 * Get the current field from the system.
139 */
140 template <int D, class T>
141 void AmCompressor<D,T>::getCurrent(VectorT& curr)
142 {
143 /*
144 * The field that we are adjusting is the Langrange multiplier
145 * field. The current value is the difference between w and w0_
146 * for the first monomer type, but any monomer type would give
147 * the same answer.
148 */
149 VecOp::subVV(curr, system().w().rgrid(0), w0_[0]);
150 }
151
152 /*
153 * Perform the main system computation (solve the MDE).
154 */
155 template <int D, class T>
156 void AmCompressor<D,T>::evaluate()
157 {
158 system().compute();
159 ++(CompressorT::mdeCounter_);
160 }
161
162 /*
163 * Compute the residual vector for the current system state.
164 */
165 template <int D, class T>
166 void AmCompressor<D,T>::getResidual(VectorT& resid)
167 {
168 // Initialize residual to -1.0
169 VecOp::eqS(resid, -1.0);
170
171 // Add c fields to get SCF residual vector
172 const int nMonomer = system().mixture().nMonomer();
173 for (int i = 0; i < nMonomer; ++i) {
174 VecOp::addEqV(resid, system().c().rgrid(i));
175 }
176 }
177
178 /*
179 * Update the current system w fields.
180 */
181 template <int D, class T>
182 void AmCompressor<D,T>::update(VectorT& newGuess)
183 {
184 // New field is w0_ + newGuess for the pressure field
185 const int nMonomer = system().mixture().nMonomer();
186 for (int i = 0; i < nMonomer; ++i) {
187 VecOp::addVV(wFieldTmp_[i], w0_[i], newGuess);
188 }
189
190 // Set system r-grid fields
191 system().w().setRGrid(wFieldTmp_);
192 }
193
194 /*
195 * Do-nothing output function.
196 */
197 template <int D, class T>
198 void AmCompressor<D,T>::outputToLog()
199 {}
200
201}
202}
203#endif
virtual void setup(bool isContinuation)
void readMixingParameters(std::istream &in, bool useLambdaRamp=true)
void outputTimers(std::ostream &out) const
virtual void readParameters(std::istream &in)
int solve(bool isContinuation=false)
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
void readParameters(std::istream &in) override
Read all parameters and initialize.
int compress() override
Compress to obtain partial saddle point w+.
void clearTimers() override
Clear all timers and mde counter.
void outputTimers(std::ostream &out) const override
Return compressor times contributions.
void setup(bool isContinuation) override
Initialize just before entry to iterative loop.
AmCompressor(System< D, T > &system)
Constructor.
void setSystem(System< D, T > &system)
Create association with the parent System.
int mdeCounter_
Count how many times MDE has been solved.
Definition Compressor.h:113
Base class template for classes that represent a complete system.
void setClassName(const char *className)
Set class name string.
File containing preprocessor macros for error handling.
void addEqV(Array< double > &a, Array< double > const &b)
Vector-vector in-place addition, a[i] += b[i] (real).
Definition VecOp.cpp:203
void eqV(Array< double > &a, Array< double > const &b, const int beginIdA, const int beginIdB, const int n)
Vector assignment, a[i] = b[i] (real, slice).
Definition VecOp.cpp:26
void subVV(Array< double > &a, Array< double > const &b, Array< double > const &c)
Vector-vector subtraction, a[i] = b[i] - c[i] (real).
Definition VecOp.cpp:100
void eqS(Array< double > &a, double b)
Vector assignment, a[i] = b (real).
Definition VecOp.cpp:55
void addVV(Array< double > &a, Array< double > const &b, Array< double > const &c)
Vector-vector addition, a[i] = b[i] + c[i] (real).
Definition VecOp.cpp:69
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.