PSCF v1.5.0
Simulator.tpp
1#ifndef RP_SIMULATOR_TPP
2#define RP_SIMULATOR_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 "Simulator.h"
12
13#include <rp/fts/simulator/SimState.h>
14#include <rp/fts/compressor/Compressor.h>
15#include <rp/fts/compressor/CompressorFactory.h>
16#include <rp/fts/perturbation/Perturbation.h>
17#include <rp/fts/perturbation/PerturbationFactory.h>
18#include <rp/fts/ramp/Ramp.h>
19#include <rp/fts/ramp/RampFactory.h>
20
21#include <rp/system/System.h>
22#include <rp/solvers/Mixture.h>
23#include <rp/solvers/Polymer.h>
24#include <rp/solvers/Solvent.h>
25#include <rp/field/Domain.h>
26#include <rp/field/CFields.h>
27#include <rp/field/WFields.h>
28
29#include <pscf/field/RField.h>
30
31#include <pscf/backend/VecRandom.h>
32#include <pscf/backend/Reduce.h>
33#include <pscf/backend/VecOp.h>
34#include <pscf/interaction/Interaction.h>
35#include <pscf/chem/PolymerModel.h>
36#include <pscf/math/IntVec.h>
37
38#include <util/misc/Timer.h>
39#include <util/random/Random.h>
40#include <util/global.h>
41
42#include <gsl/gsl_eigen.h>
43
44namespace Pscf {
45namespace Rp {
46
47 using namespace Util;
48
49 /*
50 * Constructor.
51 */
52 template <int D, class T>
54 : hamiltonian_(0.0),
58 iStep_(0),
59 iTotalStep_(0),
60 seed_(0),
61 hasHamiltonian_(false),
62 hasWc_(false),
63 hasCc_(false),
64 hasDc_(false),
65 systemPtr_(&system),
66 randomPtr_(nullptr),
67 vecRandomPtr_(nullptr),
68 compressorFactoryPtr_(nullptr),
69 compressorPtr_(nullptr),
70 perturbationFactoryPtr_(nullptr),
71 perturbationPtr_(nullptr),
72 rampFactoryPtr_(nullptr),
73 rampPtr_(nullptr),
74 isAllocated_(false)
75 {
77 randomPtr_ = new Random();
78 vecRandomPtr_ = new VecRandom<T>();
79 compressorFactoryPtr_ = new CompressorFactory<D,T>(system);
80 perturbationFactoryPtr_
81 = new PerturbationFactory<D,T>(*this);
82 rampFactoryPtr_ = new RampFactory<D,T>(*this);
83
84 linkVecRandom(vecRandom(), random());
85 }
86
87 /*
88 * Destructor.
89 */
90 template <int D, class T>
92 {
93 // Required children (created in constructor)
94 if (vecRandomPtr_) {
95 delete vecRandomPtr_;
96 }
97 if (randomPtr_) {
98 delete randomPtr_;
99 }
100 if (compressorFactoryPtr_) {
101 delete compressorFactoryPtr_;
102 }
103 if (perturbationFactoryPtr_) {
104 delete perturbationFactoryPtr_;
105 }
106 if (rampFactoryPtr_) {
107 delete rampFactoryPtr_;
109
110 // Optional children (created in readParameters)
111 if (compressorPtr_) {
112 delete compressorPtr_;
114 if (perturbationPtr_) {
115 delete perturbationPtr_;
116 }
117 if (rampPtr_) {
118 delete rampPtr_;
119 }
120 }
121
122 /*
123 * Allocate required memory.
124 */
125 template <int D, class T>
127 {
128 UTIL_CHECK(!isAllocated_);
130 const int nMonomer = system().mixture().nMonomer();
131
132 // Allocate projected chi matrix chiP_ and associated arrays
133 chiP_.allocate(nMonomer, nMonomer);
134 chiEvals_.allocate(nMonomer);
135 chiEvecs_.allocate(nMonomer, nMonomer);
136 sc_.allocate(nMonomer);
137
138 // Allocate memory for eignevector components of w and c fields
139 wc_.allocate(nMonomer);
140 cc_.allocate(nMonomer);
141 const IntVec<D> dimensions = system().domain().mesh().dimensions();
142 for (int i = 0; i < nMonomer; ++i) {
143 wc_[i].allocate(dimensions);
144 cc_[i].allocate(dimensions);
145 }
146
147 // Allocate memory for components of d (functional derivative)
148 dc_.allocate(nMonomer-1);
149 for (int i = 0; i < nMonomer - 1; ++i) {
150 dc_[i].allocate(dimensions);
151 }
152
153 // Allocate memory for r-field used as workspace
154 tmpField_.allocate(dimensions);
155
156 // Allocate state, if necessary.
157 if (!state().isAllocated) {
158 state().allocate(nMonomer, dimensions);
159 }
160
161 isAllocated_ = true;
162 }
163
164 /*
165 * Read parameter block.
166 *
167 * Virtual function - this default version is only used for unit tests.
168 */
169 template <int D, class T>
170 void Simulator<D,T>::readParameters(std::istream &in)
171 {
172 // Optionally read a random number generator seed
173 readRandomSeed(in);
174
175 bool isEnd = false;
177 // Optionally read a Compressor block
178 readCompressor(in, isEnd);
179
180 // Optionally read a Perturbation block
181 readPerturbation(in, isEnd);
182
183 // Optionally read a Ramp block
184 readRamp(in, isEnd);
185 }
186
187 /*
188 * Perform a field theoretic simulation (unimplemented default).
189 */
190 template <int D, class T>
192 { UTIL_THROW("Error: Unimplemented function Simulator<D,T>::simulate"); }
193
194 /*
195 * Open, read and analyze a trajectory file (unimplemented default).
196 */
197 template <int D, class T>
198 void Simulator<D,T>::analyze(int min, int max,
199 std::string classname,
200 std::string filename)
201 { UTIL_THROW("Error: Unimplemented function Simulator<D,T>::analyze"); }
202
203 /*
204 * Clear all local state data (field eigen-components and Hamiltonian)
205 */
206 template <int D, class T>
208 {
209 hasHamiltonian_ = false;
210 hasWc_ = false;
211 hasCc_ = false;
212 hasDc_ = false;
213 }
214
215 /*
216 * Compute field theoretic Hamiltonian and its components.
217 */
218 template <int D, class T>
220 {
221 // Preconditions
222 UTIL_CHECK(isAllocated_);
223 UTIL_CHECK(system().w().hasData());
224 UTIL_CHECK(system().c().hasData());
225
226 // Update wc fields if necessary
227 if (!hasWc_) computeWc();
228
229 Mixture<D,T> const & mixture = system().mixture();
230 Domain<D,T> const & domain = system().domain();
231
232 const int nMonomer = mixture.nMonomer();
233 const int meshSize = domain.mesh().size();
234 const int np = mixture.nPolymer();
235 const int ns = mixture.nSolvent();
236 double phi, mu;
237
238 hasHamiltonian_ = false;
239
240 // Compute field Hamiltonian contribution
241 idealHamiltonian_ = 0.0;
242
243 // Polymer ideal gas contribution
244 if (np > 0) {
245 Polymer<D,T> const * polymerPtr;
246 double length;
247 for (int i = 0; i < np; ++i) {
248 polymerPtr = &mixture.polymer(i);
249 phi = polymerPtr->phi();
250 mu = polymerPtr->mu();
252 length = polymerPtr->length();
253 } else {
254 length = (double)polymerPtr->nBead();
255 }
256 // Recall: mu = ln(phi/q)
257 if (phi > 0.0) {
258 if (polymerPtr->ensemble() == Ensemble::Closed) {
259 idealHamiltonian_ += phi * ( mu - 1.0 ) / length;
260 } else {
261 idealHamiltonian_ -= phi / length;
262 }
263 }
264 }
265 }
266
267 // Compute solvent ideal gas contributions to idealHamiltonian_
268 if (ns > 0) {
269 Solvent<D,T> const * solventPtr;
270 double size;
271 for (int i = 0; i < ns; ++i) {
272 solventPtr = &mixture.solvent(i);
273 phi = solventPtr->phi();
274 mu = solventPtr->mu();
275 size = solventPtr->size();
276 // Recall: mu = ln(phi/q)
277 if (phi > 0.0) {
278 if (solventPtr->ensemble() == Ensemble::Closed) {
279 idealHamiltonian_ += phi * ( mu - 1.0 ) / size;
280 } else {
281 idealHamiltonian_ -= phi / size;
282 }
283 }
284 }
285 }
286
287 // Subtract spatial average of pressure field from idealHamiltonian_
288 double sum_xi = Reduce::sum(wc_[nMonomer-1]);
289 idealHamiltonian_ -= sum_xi / double(meshSize);
290
291 // idealHamiltonian_ now contains a final value per monomer
292
293 // Compute field Hamiltonian contribution
294 fieldHamiltonian_ = 0.0;
295
296 // Quadratic field contribution
297 double prefactor, wSquare;
298 for (int j = 0; j < nMonomer - 1; ++j) {
299 UTIL_CHECK(tmpField_.capacity() == meshSize);
300 // Subtract constant shift sc_[j]
301 VecOp::subVS(tmpField_, wc_[j], sc_[j]);
302 wSquare = Reduce::sumSq(tmpField_);
303 prefactor = -0.5*double(nMonomer)/chiEvals_[j];
304 fieldHamiltonian_ += prefactor * wSquare;
305 }
306 fieldHamiltonian_ /= double(meshSize);
307
308 // Add constant term K/2 per monomer ( K = s = e^{T}chi e/M^2 )
309 fieldHamiltonian_ += 0.5*sc_[nMonomer - 1];
310
311 // fieldHamiltonian_ now contains a final value per monomer
312
313 // Compute nMonomerSystem = number of monomers in the system
314 const double vSystem = domain.unitCell().volume();
315 const double vMonomer = mixture.vMonomer();
316 const double nMonomerSystem = vSystem / vMonomer;
317
318 // Convert per monomer values to extensive values
319 fieldHamiltonian_ *= nMonomerSystem;
320 idealHamiltonian_ *= nMonomerSystem;
321
322 hamiltonian_ = idealHamiltonian_ + fieldHamiltonian_;
323
324 // Add perturbationHamiltonian_, if any
325 if (hasPerturbation()) {
326 perturbationHamiltonian_
327 = perturbation().hamiltonian(hamiltonian_);
328 hamiltonian_ += perturbationHamiltonian_;
329 } else {
330 perturbationHamiltonian_ = 0.0;
331 }
332
333 hasHamiltonian_ = true;
334 }
335
336 /*
337 * Compute eigenvalues and eigenvectors of the projected chi matrix.
338 */
339 template <int D, class T>
341 {
342 UTIL_CHECK(isAllocated_);
343
344 const int nMonomer = system().mixture().nMonomer();
345 DMatrix<double> const & chi = system().interaction().chi();
346 double d = 1.0/double(nMonomer);
347 int i, j, k;
348
349 // Compute orthogonal projection matrix P
351 P.allocate(nMonomer, nMonomer);
352 for (i = 0; i < nMonomer; ++i) {
353 for (j = 0; j < nMonomer; ++j) {
354 P(i,j) = -d;
355 }
356 P(i,i) += 1.0;
357 }
358
359 // Compute CP = chi*P (temporary matrix)
361 CP.allocate(nMonomer, nMonomer);
362 for (i = 0; i < nMonomer; ++i) {
363 for (j = 0; j < nMonomer; ++j) {
364 CP(i, j) = 0.0;
365 for (k = 0; k < nMonomer; ++k) {
366 CP(i,j) += chi(i,k)*P(k,j);
367 }
368 }
369 }
370
371 // Compute chiP = = P*chi*P = P*CP
372 DMatrix<double> chiP;
373 chiP.allocate(nMonomer, nMonomer);
374 for (i = 0; i < nMonomer; ++i) {
375 for (j = 0; j < nMonomer; ++j) {
376 chiP(i, j) = 0.0;
377 for (k = 0; k < nMonomer; ++k) {
378 chiP(i,j) += P(i,k)*CP(k,j);
379 }
380 }
381 }
382
383 // Eigenvalue calculations use data structures and
384 // functions from the Gnu Scientific Library (GSL)
385
386 // Allocate GSL matrix A that will hold a copy of chiP
387 gsl_matrix* A = gsl_matrix_alloc(nMonomer, nMonomer);
388
389 // Copy DMatrix<double> chiP to gsl_matrix A
390 for (i = 0; i < nMonomer; ++i) {
391 for (j = 0; j < nMonomer; ++j) {
392 gsl_matrix_set(A, i, j, chiP(i, j));
393 }
394 }
395
396 // Compute eigenvalues and eigenvectors of chiP (or A)
397 gsl_eigen_symmv_workspace* work = gsl_eigen_symmv_alloc(nMonomer);
398 gsl_vector* Avals = gsl_vector_alloc(nMonomer);
399 gsl_matrix* Avecs = gsl_matrix_alloc(nMonomer, nMonomer);
400 int error;
401 error = gsl_eigen_symmv(A, Avals, Avecs, work);
402 UTIL_CHECK(error == 0);
403
404 // Requirements:
405 // - A has exactly one zero eigenvalue, with eigenvector (1,...,1)
406 // - All other eigenvalues must be negative.
407
408 // Copy eigenpairs with non-null eigenvalues
409 int iNull = -1; // index for null eigenvalue
410 int nNull = 0; // number of null eigenvalue
411 k = 0; // re-ordered index for non-null eigenvalue
412 double val;
413 for (i = 0; i < nMonomer; ++i) {
414 val = gsl_vector_get(Avals, i);
415 if (std::abs(val) < 1.0E-8) {
416 ++nNull;
417 iNull = i;
418 UTIL_CHECK(nNull <= 1);
419 } else {
420 chiEvals_[k] = val;
421 UTIL_CHECK(val < 0.0);
422 for (j = 0; j < nMonomer; ++j) {
423 chiEvecs_(k, j) = gsl_matrix_get(Avecs, j, i);
424 }
425 if (chiEvecs_(k, 0) < 0.0) {
426 for (j = 0; j < nMonomer; ++j) {
427 chiEvecs_(k, j) = -chiEvecs_(k, j);
428 }
429 }
430 ++k;
431 }
432 }
433 UTIL_CHECK(nNull == 1);
434 UTIL_CHECK(iNull >= 0);
435
436 // Set eigenpair with zero eigenvalue
437 i = nMonomer - 1;
438 chiEvals_[i] = 0.0;
439 for (j = 0; j < nMonomer; ++j) {
440 chiEvecs_(i, j) = gsl_matrix_get(Avecs, j, iNull);
441 }
442 if (chiEvecs_(i, 0) < 0) {
443 for (j = 0; j < nMonomer; ++j) {
444 chiEvecs_(i, j) = -chiEvecs_(i, j);
445 }
446 }
447
448 // Free GSL objects
449 gsl_matrix_free(A);
450 gsl_vector_free(Avals);
451 gsl_matrix_free(Avecs);
452 gsl_eigen_symmv_free(work);
453
454 // Normalize all eigenvectors so that the sum of squares = nMonomer
455 double vec, norm, prefactor;
456 for (i = 0; i < nMonomer; ++i) {
457 norm = 0.0;
458 for (j = 0; j < nMonomer; ++j) {
459 vec = chiEvecs_(i, j);
460 norm += vec*vec;
461 }
462 prefactor = sqrt( double(nMonomer)/norm );
463 for (j = 0; j < nMonomer; ++j) {
464 chiEvecs_(i, j) *= prefactor;
465 }
466 }
467
468 // Check final eigenvector is (1, ..., 1)
469 for (j = 0; j < nMonomer; ++j) {
470 UTIL_CHECK(std::abs(chiEvecs_(nMonomer-1, j) - 1.0) < 1.0E-8);
471 }
472
473 // Compute vector s in monomer basis
475 s.allocate(nMonomer);
476 for (i = 0; i < nMonomer; ++i) {
477 s[i] = 0.0;
478 for (j = 0; j < nMonomer; ++j) {
479 s[i] += chi(i,j);
480 }
481 s[i] = s[i]/double(nMonomer);
482 }
483
484 // Compute components of s in eigenvector basis -> sc_
485 for (i = 0; i < nMonomer; ++i) {
486 sc_[i] = 0.0;
487 for (j = 0; j < nMonomer; ++j) {
488 sc_[i] += chiEvecs_(i,j)*s[j];
489 }
490 sc_[i] = sc_[i]/double(nMonomer);
491 }
492
493 }
494
495 /*
496 * Compute the eigenvector components of the w fields, using the
497 * eigenvectors chiEvecs_ of the projected chi matrix as a basis.
498 */
499 template <int D, class T>
501 {
502 UTIL_CHECK(isAllocated_);
503 UTIL_CHECK(system().w().hasData());
504
505 double vec;
506 int i, j;
507 const int nMonomer = system().mixture().nMonomer();
508
509 // Loop over eigenvectors (i is an eigenvector index)
510 for (i = 0; i < nMonomer; ++i) {
511
512 // Initialize field wc_[i] to zero
513 RField<D,T>& Wc = wc_[i];
514 VecOp::eqS(Wc, 0.0);
515
516 // Loop over monomer types (j is a monomer index)
517 for (j = 0; j < nMonomer; ++j) {
518 vec = chiEvecs_(i, j)/double(nMonomer);
519 VecOp::addEqVc(Wc, system().w().rgrid(j), vec);
520 }
521 }
522
523 hasWc_ = true;
524 }
525
526 /*
527 * Compute the eigenvector components of the c-fields, using the
528 * eigenvectors chiEvecs_ of the projected chi matrix as a basis.
529 */
530 template <int D, class T>
532 {
533 // Preconditions
534 UTIL_CHECK(isAllocated_);
535 UTIL_CHECK(system().w().hasData());
536 UTIL_CHECK(system().c().hasData());
537
538 double vec;
539 int i, j;
540 const int nMonomer = system().mixture().nMonomer();
541
542 // Loop over eigenvectors (i is an eigenvector index)
543 for (i = 0; i < nMonomer; ++i) {
544
545 // Initialize field cc_[i] to zero
546 RField<D,T>& Cc = cc_[i];
547 VecOp::eqS(Cc, 0.0);
548
549 // Loop over monomer types (j is a monomer index)
550 for (j = 0; j < nMonomer; ++j) {
551 vec = chiEvecs_(i, j);
552 VecOp::addEqVc(Cc, system().c().rgrid(j), vec);
554 }
555
556 hasCc_ = true;
557 }
558
559 /*
560 * Compute d fields, i.e., functional derivatives of H[W].
561 */
562 template <int D, class T>
564 {
565 // Preconditions
566 UTIL_CHECK(isAllocated_);
567 if (!hasWc_) computeWc();
568 if (!hasCc_) computeCc();
569
570 // Local constants and variables
571 const int nMonomer = system().mixture().nMonomer();
572 const double vMonomer = system().mixture().vMonomer();
573 const double a = 1.0/vMonomer;
574 double b, s;
575
576 // Loop over composition eigenvectors (exclude the last)
577 for (int i = 0; i < nMonomer - 1; ++i) {
578 RField<D,T>& Dc = dc_[i];
579 RField<D,T> const & Wc = wc_[i];
580 RField<D,T> const & Cc = cc_[i];
581 b = -1.0*a*double(nMonomer)/chiEvals_[i];
582 s = -1.0*b*sc_[i];
583 VecOp::addVcVcS(Dc, Cc, a, Wc, b, s);
584 }
585
586 // Add derivatives arising from a perturbation (if any).
587 if (hasPerturbation()) {
588 perturbation().incrementDc(dc_);
589 }
590
591 hasDc_ = true;
592 }
593
594 /*
595 * Save the current state prior to a next move.
596 *
597 * Invoked before each attempted move.
598 */
599 template <int D, class T>
601 {
602 UTIL_CHECK(system().w().hasData());
603 UTIL_CHECK(hasWc());
604 UTIL_CHECK(state().isAllocated);
605 UTIL_CHECK(!state().hasData);
606
607 const int nMonomer = system().mixture().nMonomer();
608
609 // Save all w-field components
610 for (int i = 0; i < nMonomer; ++i) {
611 state().w[i] = system().w().rgrid(i);
612 state().wc[i] = wc_[i];
613 }
614
615 // Save cc, if needed
616 if (state().needsCc) {
617 UTIL_CHECK(hasCc());
618 UTIL_CHECK(state().cc.isAllocated());
619 for (int i = 0; i < nMonomer; ++i) {
620 state().cc[i] = cc_[i];
621 }
622 }
623
624 // Save dc, if needed
625 if (state().needsDc) {
626 UTIL_CHECK(hasDc());
627 UTIL_CHECK(state().dc.isAllocated());
628 for (int i = 0; i < nMonomer - 1; ++i) {
629 state().dc[i] = dc_[i];
630 }
631 }
632
633 // Save Hamiltonian and its components, if needed
634 if (state().needsHamiltonian){
636 state().hamiltonian = hamiltonian();
637 state().idealHamiltonian = idealHamiltonian();
638 state().fieldHamiltonian = fieldHamiltonian();
639 state().perturbationHamiltonian = perturbationHamiltonian();
640 }
641
642 if (hasPerturbation()) {
643 perturbation().saveState();
644 }
645
646 state().hasData = true;
647 }
648
649 /*
650 * Restore a saved simulation state.
651 *
652 * Invoked after the compressor fails to converge or an attempted
653 * Monte-Carlo move is rejected.
654 */
655 template <int D, class T>
658 UTIL_CHECK(state().isAllocated);
659 UTIL_CHECK(state().hasData);
660 const int nMonomer = system().mixture().nMonomer();
661
662 // Restore monomer w-field components
663 system().w().setRGrid(state().w);
664
665 // Restore wc_ field components
666 for (int i = 0; i < nMonomer; ++i) {
667 wc_[i] = state().wc[i];
668 }
669 hasWc_ = true;
670
671 // Restore cc_ c-field components, if needed
672 if (state().needsCc) {
673 for (int i = 0; i < nMonomer; ++i) {
674 cc_[i] = state().cc[i];
675 }
676 hasCc_ = true;
677 }
679 // Restore dc_ fieldc components, if needed
680 if (state().needsDc) {
681 for (int i = 0; i < nMonomer - 1; ++i) {
682 dc_[i] = state().dc[i];
683 }
684 hasDc_ = true;
685 }
686
687 // Restore Hamiltonian and its components, if needed
688 if (state().needsHamiltonian){
689 hamiltonian_ = state().hamiltonian;
690 idealHamiltonian_ = state().idealHamiltonian;
691 fieldHamiltonian_ = state().fieldHamiltonian;
692 perturbationHamiltonian_ = state().perturbationHamiltonian;
693 hasHamiltonian_ = true;
694 }
695
696 // Restore internal state of perturbation, if any
697 if (hasPerturbation()) {
698 perturbation().restoreState();
699 }
700
701 // Clear internal state of SimState object
702 state().hasData = false;
703 }
704
705 /*
706 * Clear the saved system state.
707 *
708 * Invoked when an attempted move is accepted.
709 */
710 template <int D, class T>
712 { state().hasData = false; }
713
714 /*
715 * Output all timer results.
716 */
717 template <int D, class T>
718 void Simulator<D,T>::outputTimers(std::ostream& out) const
720 if (!compressorPtr_) return;
721 outputMdeCounter(out);
722 compressorPtr_->outputTimers(out);
723 }
724
725 /*
726 * Output modified diffusion equation (MDE) counter.
727 */
728 template <int D, class T>
729 void Simulator<D,T>::outputMdeCounter(std::ostream& out) const
730 {
731 UTIL_CHECK(compressorPtr_);
732 out << "MDE counter "
733 << compressorPtr_->mdeCounter() << std::endl;
734 out << std::endl;
735 }
736
737 /*
738 * Clear all timers.
739 */
740 template <int D, class T>
742 {
743 if (hasCompressor()) {
744 compressor().clearTimers();
745 }
746 }
747
748 // Protected Functions
749
750 /*
751 * Optionally read RNG seed, initialize random number generators.
752 */
753 template <int D, class T>
754 void Simulator<D,T>::readRandomSeed(std::istream& in)
755 {
756 // Optionally read a random number generator seed
757 seed_ = 0;
758 readOptional(in, "seed", seed_);
759
760 // Set random number generator seeds on CPU and GPU
761 // Default value seed_ = 0 uses the clock time.
762 random().setSeed(seed_);
763
764 seedVecRandom(vecRandom(), seed_);
765 // CPU version does nothing, GPU version sets the seed.
766 }
767
768 // Functions related to a Compressor
769
770 /*
771 * Optionally read a Compressor parameter file block.
772 */
773 template <int D, class T>
774 void Simulator<D,T>::readCompressor(std::istream& in, bool& isEnd)
775 {
776 if (!isEnd) {
777 UTIL_CHECK(compressorFactoryPtr_);
779 std::string className;
780 compressorPtr_ =
781 compressorFactory().readObjectOptional(in, *this,
782 className, isEnd);
783 }
784 if (!compressorPtr_ && ParamComponent::echo()) {
785 Log::file() << indent() << " Compressor{ [absent] }\n";
786 }
787 }
788
789 /*
790 * Get the Compressor factory by reference.
791 */
792 template <int D, class T>
794 {
795 UTIL_CHECK(compressorFactoryPtr_);
796 return *compressorFactoryPtr_;
797 }
798
799 // Functions related to a Perturbation
800
801 /*
802 * Optionally read a Perturbation parameter file block.
803 */
804 template <int D, class T>
805 void Simulator<D,T>::readPerturbation(std::istream& in, bool& isEnd)
806 {
807 if (!isEnd) {
808 UTIL_CHECK(perturbationFactoryPtr_);
810 std::string className;
811 perturbationPtr_ =
812 perturbationFactory().readObjectOptional(in, *this,
813 className, isEnd);
814 }
815 if (!perturbationPtr_ && ParamComponent::echo()) {
816 Log::file() << indent() << " Perturbation{ [absent] }\n";
817 }
818 }
819
820 /*
821 * Get the Perturbation factory by reference.
822 */
823 template <int D, class T>
825 {
826 UTIL_CHECK(perturbationFactoryPtr_);
827 return *perturbationFactoryPtr_;
828 }
829
830 /*
831 * Set the associated Perturbation object.
832 */
833 template <int D, class T>
835 {
836 UTIL_CHECK(ptr);
837 perturbationPtr_ = ptr;
838 }
839
840 // Functions related to a Ramp
841
842 /*
843 * Optionally read a Ramp parameter file block.
844 */
845 template <int D, class T>
846 void Simulator<D,T>::readRamp(std::istream& in, bool& isEnd)
847 {
848 if (!isEnd) {
849 UTIL_CHECK(rampFactoryPtr_);
851 std::string className;
852 rampPtr_ =
853 rampFactory().readObjectOptional(in, *this, className, isEnd);
854 }
855 if (!rampPtr_ && ParamComponent::echo()) {
856 Log::file() << indent() << " Ramp{ [absent] }\n";
857 }
858 }
859
860 /*
861 * Get the Ramp factory by reference.
862 */
863 template <int D, class T>
865 {
866 UTIL_CHECK(rampFactoryPtr_);
867 return *rampFactoryPtr_;
868 }
869
870 /*
871 * Set the associated Ramp object.
872 */
873 template <int D, class T>
875 {
876 UTIL_CHECK(ptr);
877 rampPtr_ = ptr;
878 }
879
880} // namespace Rp
881} // namespace Pscf
882#endif
int nMonomer() const
Get number of monomer types.
double vMonomer() const
Get monomer reference volume (set to 1.0 by default).
int nPolymer() const
Get number of polymer species.
int nSolvent() const
Get number of solvent (point particle) species.
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
int nBead() const
Total number of beads in the polymer (bead model).
double length() const
Sum of the lengths of all blocks in the polymer (thread model).
Factory for subclasses of Compressor.
CompressorFactory(System< D, T > &system)
Constructor.
Spatial domain for a periodic structure with real fields.
UnitCell< D > & unitCell()
Get the UnitCell by non-const reference.
Mesh< D > & mesh()
Get the Mesh by non-const reference.
Solver and descriptor for a mixture of polymers and solvents.
SolventT & solvent(int id)
Get a solvent solver object.
PolymerT & polymer(int id)
Get a polymer solver object by non-const reference.
Factory for subclasses of Perturbation.
PerturbationFactory(Simulator< D, T > &simulator)
Constructor.
Base class for additive perturbations of standard FTS Hamiltonian.
Descriptor and MDE solver for one polymer species.
Factory for subclasses of Ramp.
Definition RampFactory.h:36
RampFactory(Simulator< D, T > &simulator)
Constructor.
Class that varies parameters during a simulation (abstract).
Definition Ramp.h:35
~Simulator()
Destructor.
Definition Simulator.tpp:91
virtual void clearTimers()
Clear timers.
Random & random()
Get the scalar random number generator by reference.
Definition Simulator.h:952
Simulator(System< D, T > &system)
Constructor.
Definition Simulator.tpp:53
void saveState()
Save a copy of the current system state.
virtual void simulate(int nStep)
Perform a field theoretic Monte-Carlo simulation.
bool hasDc_
Have functional derivatives of H[W] been computed ?
Definition Simulator.h:838
void computeHamiltonian()
Compute the Hamiltonian used in PS-FTS.
void readRamp(std::istream &in, bool &isEnd)
bool hasWc_
Have eigen-components of the current w fields been computed ?
Definition Simulator.h:828
void clearData()
Clear field eigen-components and Hamiltonian components.
CompressorFactory< D, T > & compressorFactory()
Get the Compressor factory by reference.
double fieldHamiltonian_
Quadratic field contribution to Hamiltonian.
Definition Simulator.h:784
void readPerturbation(std::istream &in, bool &isEnd)
bool hasCc() const
Are eigen-components of the current c fields valid ?
Definition Simulator.h:1124
void computeWc()
Compute eigenvector components of the current w fields.
bool hasHamiltonian_
Has the Hamiltonian been computed for the current w and c fields?
Definition Simulator.h:823
void readCompressor(std::istream &in, bool &isEnd)
VecRandom< T > & vecRandom()
Get the vector random number generator by reference.
Definition Simulator.h:960
bool hasDc() const
Are the current d fields valid ?
Definition Simulator.h:1139
void computeCc()
Compute eigenvector components of the current c fields.
RampFactory< D, T > & rampFactory()
Get the Ramp factory by reference.
System< D, T > & system()
Get the parent system by reference.
Definition Simulator.h:944
bool hasWc() const
Are eigen-components of the current w fields valid ?
Definition Simulator.h:1109
void setRamp(Ramp< D, T > *ptr)
Set the associated Ramp.
void allocate()
Allocate required memory during initialization.
PerturbationFactory< D, T > & perturbationFactory()
Get the Perturbation factory by reference.
void readRandomSeed(std::istream &in)
Optionally read a random seed and initialize RNGs.
virtual void outputMdeCounter(std::ostream &out) const
void setPerturbation(Perturbation< D, T > *ptr)
Set the associated Perturbation.
DArray< RField< D, CPT > > dc_
Definition Simulator.h:761
Perturbation< D, T > const & perturbation() const
Get a Perturbation by const reference.
Definition Simulator.h:994
double idealHamiltonian() const
Get an ideal contribution to the Hamiltonian.
Definition Simulator.h:1083
void restoreState()
Restore the system to the saved state.
virtual void outputTimers(std::ostream &out) const
Output timing results.
void computeDc()
Compute functional derivatives of the Hamiltonian.
DArray< RField< D, CPT > > wc_
Definition Simulator.h:742
long iTotalStep_
Step counter - total number of attempted BD or MC steps.
Definition Simulator.h:811
bool hasPerturbation() const
Does this Simulator have a Perturbation?
Definition Simulator.h:989
bool hasRamp() const
Does this Simulator have a Ramp?
Definition Simulator.h:1010
double perturbationHamiltonian() const
Get a perturbation to the standard Hamiltonian.
Definition Simulator.h:1099
double hamiltonian() const
Get the Hamiltonian used in PS-FTS.
Definition Simulator.h:1075
SimState< D, CPT > & state()
Definition Simulator.h:1031
bool hasCompressor() const
Does this Simulator have a Compressor?
Definition Simulator.h:968
Compressor< D, T > const & compressor() const
Get the Compressor by const reference.
Definition Simulator.h:981
bool hasCc_
Have eigen-components of the current c fields been computed ?
Definition Simulator.h:833
virtual void analyze(int min, int max, std::string classname, std::string filename)
Read and analyze a trajectory file.
virtual void readParameters(std::istream &in)
Read parameters for a simulation.
long iStep_
Step counter - number of steps for which the compressor converged.
Definition Simulator.h:806
void clearState()
Clear the saved copy of the system state.
double idealHamiltonian_
Ideal gas contribution (-lnQ) to Hamiltonian.
Definition Simulator.h:779
DArray< RField< D, CPT > > cc_
Definition Simulator.h:753
double perturbationHamiltonian_
Perturbation contribution to the Hamiltonian.
Definition Simulator.h:793
long seed_
Random number generator seed (input value).
Definition Simulator.h:816
DArray< RField< D, T > > const & dc() const
Get all of the current d fields.
Definition Simulator.h:1144
double hamiltonian_
Total field theoretic Hamiltonian H[W] (extensive value).
Definition Simulator.h:774
void analyzeChi()
Perform eigenvalue analysis of projected chi matrix.
bool hasHamiltonian() const
Has the Hamiltonian been computed for the current w and c fields?
Definition Simulator.h:1070
DArray< RField< D, T > > const & cc() const
Get all eigenvector components of the current c fields.
Definition Simulator.h:1129
double fieldHamiltonian() const
Get the quadratic field contribution to the Hamiltonian.
Definition Simulator.h:1091
Solver and descriptor for a solvent species.
Base class template for classes that represent a complete system.
double size() const
Get the size (number of monomers) in this solvent.
Ensemble ensemble() const
Get the statistical ensemble for this species.
Definition Species.h:188
WT mu() const
Get the chemical potential for this species (units kT=1).
Definition Species.h:174
WT phi() const
Get the overall volume fraction for this species.
Definition Species.h:167
Dynamically allocatable contiguous array template.
Definition DArray.h:32
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:262
Dynamically allocated Matrix.
Definition DMatrix.h:25
void allocate(int capacity1, int capacity2)
Allocate memory for a matrix.
Definition DMatrix.h:170
static std::ostream & file()
Get log ostream by reference.
Definition Log.cpp:59
std::string indent() const
Return indent string for this object (string of spaces).
static bool echo()
Get echo parameter.
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.
std::string className() const
Get class name string.
Random()
Constructor.
Definition Random.cpp:13
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
double sumSq(Array< double > const &in)
Compute sum of of squares of array elements (real).
Definition Reduce.cpp:121
double sum(Array< double > const &in)
Compute sum of array elements (real).
Definition Reduce.cpp:19
void eqS(Array< double > &a, double b)
Vector assignment, a[i] = b (real).
Definition VecOp.cpp:55
void subVS(Array< double > &a, Array< double > const &b, double c)
Vector-scalar subtraction, a[i] = b[i] - c (real).
Definition VecOp.cpp:115
void addEqVc(Array< double > &a, Array< double > const &b, const double c)
Add scaled vector in-place, a[i] += b[i]*c (real).
Definition VecOp.cpp:400
bool isThread()
Is the thread model in use ?
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
void addVcVcS(Array< double > &a, Array< double > const &b1, const double c1, Array< double > const &b2, const double c2, const double s)
Add scaled vectors + scalar, a[i] = b1[i]*c1 + b2[2]*c2 + s (real).
Definition VecOp.cpp:414
PSCF package top-level namespace.