PSCF v1.5.0
LinearRamp.tpp
1#ifndef RP_LINEAR_RAMP_TPP
2#define RP_LINEAR_RAMP_TPP
3
4#include "LinearRamp.h"
5#include <rp/fts/simulator/Simulator.h>
6#include <rp/fts/compressor/Compressor.h>
7#include <rp/system/System.h>
8#include <rp/solvers/Mixture.h>
9#include <util/global.h>
10
11namespace Pscf {
12namespace Rp {
13
14 using namespace Util;
15
16 /*
17 * Constructor.
18 */
19 template <int D, class T>
23
24 template <int D, class T>
25 void LinearRamp<D,T>::readParameters(std::istream& in)
26 {
27 // Read the number of ramp parameters, allocate parameters_ array
28 ParamComposite::read(in, "nParameter", nParameter_);
29 UTIL_CHECK(nParameter_ > 0);
30 parameters_.allocate(nParameter_);
31
32 // Read array of RampParameters, using << operator for each
33 ParamComposite::readDArray(in, "parameters",
34 parameters_, nParameter_);
35
36 // Verify net zero change in volume fractions, if these are swept
37 // and every species is closed (open species take up the rest of
38 // the volume, so closed volume fractions may then change freely)
39 if (Ramp<D,T>::simulator().system().mixture().isCanonical()) {
40 double sum = 0.0;
41 for (int i = 0; i < nParameter_; ++i) {
42 if (parameters_[i].type() == "phi_polymer" ||
43 parameters_[i].type() == "phi_solvent")
44 {
45 sum += parameters_[i].change();
46 }
47 }
48 if (sum > 1.0E-10 || sum < -1.0E-10) {
49 UTIL_THROW("Changes of the volume fractions of a closed "
50 "mixture must add up to zero");
51 }
52 }
53 }
54
55 template <int D, class T>
56 void LinearRamp<D,T>::setup(int nStep)
57 {
58 Ramp<D,T>::nStep_ = nStep;
59
60 // Set simulator pointer and initial value for each parameter object
61 for (int i = 0; i < nParameter_; ++i) {
62 parameters_[i].setSimulator(Ramp<D,T>::simulator());
63 parameters_[i].getInitial();
64 }
65 }
66
67 template <int D, class T>
69 {
70 // Compute a ramp parameter in the range [0,1]
71 double s = double(iStep)/double(Ramp<D,T>::nStep_);
72
73 // Update the system parameter values
74 double newVal;
75 bool hasChi = false;
76 for (int i = 0; i < nParameter_; ++i) {
77
78 // Update value
79 newVal = parameters_[i].initial() + s*parameters_[i].change();
80 parameters_[i].update(newVal);
81
82 // Check if this is a chi parameter
83 if (parameters_[i].type() == "chi") hasChi = true;
84 }
85
86 // If chi parameters changed, update chiEvals and chiEvecs
87 if (hasChi) {
88 Ramp<D,T>::simulator().analyzeChi();
89 }
90
91 // Clear compressor data that may depend on ramped parameters
92 if (Ramp<D,T>::simulator().hasCompressor()) {
93 Ramp<D,T>::simulator().compressor().clearCache();
94 }
95
96 }
97
98 template <int D, class T>
100 {
101 for (int i = 0; i < nParameter_; ++i) {
102 Log::file() << "Parameter type: "
103 << parameters_[i].type() << std::endl;
104 Log::file() << "Initial: "
105 << parameters_[i].initial() << " ";
106 Log::file()<< "Change: "
107 << parameters_[i].change() << std::endl;
108 }
109 }
110
111}
112}
113#endif
void output() override
Output information at the end of a simulation.
void setParameters(int iStep) override
Set new parameters values in associated System and Simulator.
LinearRamp(Simulator< D, T > &simulator)
Constructor.
void readParameters(std::istream &in) override
Read parameters from parameter file input stream.
void setup(int nStep) override
Set nStep and complete initialization.
Simulator< D, T > const & simulator() const
Get parent Simulator<D,T> by const reference.
Definition Ramp.h:107
int nStep_
Number of steps planned for this simulation (set in setup).
Definition Ramp.h:94
Base class for field theoretic PS-FTS simulator.
Definition Simulator.h:93
static std::ostream & file()
Get log ostream by reference.
Definition Log.cpp:59
DArrayParam< Type > & readDArray(std::istream &in, const char *label, DArray< Type > &array, int n)
Add and read a required DArray < Type > parameter.
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.
File containing preprocessor macros for error handling.
#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
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.