PSCF v1.4.0
AmbdInteraction.cpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2025, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "AmbdInteraction.h"
9#include <pscf/interaction/Interaction.h>
10#include <pscf/math/LuSolver.h>
11
12namespace Pscf
13{
14
15 using namespace Util;
16
17 /*
18 * Constructor.
19 */
21 : nMonomer_(0),
22 isAllocated_(false)
23 {}
24
25 /*
26 * Destructor.
27 */
30
31 /*
32 * Set the number of monomer types and allocate memory.
33 */
35 {
36 UTIL_CHECK(isAllocated_ == false);
37 UTIL_CHECK(nMonomer_ == 0);
39 nMonomer_ = nMonomer;
40 chi_.allocate(nMonomer, nMonomer);
41 chiInverse_.allocate(nMonomer, nMonomer);
42 p_.allocate(nMonomer, nMonomer);
43 isAllocated_ = true;
44 }
45
46 void AmbdInteraction::update(Interaction const & interaction)
47 {
48
49 // Set nMonomer and allocate memory if not done previously
50 if (!isAllocated_) {
51 setNMonomer(interaction.nMonomer());
52 }
53 UTIL_CHECK(nMonomer_ == interaction.nMonomer());
54
55 // Copy all chi and chiInverse matrix values
56 int i, j;
57 for (i = 0; i < nMonomer_; ++i) {
58 for (j = 0; j < nMonomer_; ++j) {
59 chi_(i, j) = interaction.chi(i, j);
60 //chiInverse_(i, j) = interaction.chiInverse(i, j);
61 }
62 }
63
64 if (nMonomer() == 2) {
65 double det = chi_(0,0)*chi_(1, 1) - chi_(0,1)*chi_(1,0);
66 double norm = chi_(0,0)*chi_(0, 0) + chi_(1,1)*chi_(1,1)
67 + 2.0*chi_(0,1)*chi_(1,0);
68 if (fabs(det/norm) < 1.0E-8) {
69 UTIL_THROW("Singular chi matrix");
70 }
71 chiInverse_(0,1) = -chi_(0,1)/det;
72 chiInverse_(1,0) = -chi_(1,0)/det;
73 chiInverse_(1,1) = chi_(0,0)/det;
74 chiInverse_(0,0) = chi_(1,1)/det;
75
76 } else {
77 LuSolver solver;
78 solver.allocate(nMonomer());
79 solver.computeLU(chi_);
80 solver.inverse(chiInverse_);
81 }
82
83 // Compute p and sumChiInverse
84 int k;
85 double sum = 0.0;
86 for (i = 0; i < nMonomer_; ++i) {
87 p_(0,i) = 0.0;
88 for (j = 0; j < nMonomer_; ++j) {
89 p_(0,i) -= chiInverse_(j,i);
90 }
91 sum -= p_(0,i);
92 for (k = 0; k < nMonomer_; ++k) { //row
93 p_(k,i) = p_(0,i);
94 }
95 }
96
97 for (i = 0; i < nMonomer_; ++i) { //row
98 for (j = 0; j < nMonomer_; ++j) { //coloumn
99 p_(i,j) /= sum;
100 }
101 p_(i,i) += 1.0 ;
102 }
103 sumChiInverse_ = sum;
104
105 }
106
107} // namespace Pscf
int nMonomer() const
Get number of monomer types.
AmbdInteraction()
Constructor.
void setNMonomer(int nMonomer)
Set number of monomers and allocate required memory.
void update(Interaction const &interaction)
Update all computed quantities.
virtual ~AmbdInteraction()
Destructor.
Interaction model for complex Langevin FTS.
Definition Interaction.h:24
DMatrix< double > const & chi() const
Return the chi matrix by const reference.
int nMonomer() const
Get number of monomer types.
Solve Ax=b by LU decomposition of A.
Definition LuSolver.h:31
void inverse(Matrix< double > &inv)
Compute inverse of matrix A.
Definition LuSolver.cpp:104
void allocate(int n)
Allocate memory.
Definition LuSolver.cpp:46
void computeLU(const Matrix< double > &A)
Compute the LU decomposition for later use.
Definition LuSolver.cpp:63
#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
PSCF package top-level namespace.