oILAB
Loading...
Searching...
No Matches
GbMesoStateEnsembleImplementation.h
Go to the documentation of this file.
1//
2// Created by Nikhil Chandra Admal on 5/26/24.
3//
4
5#ifndef OILAB_GBMESOSTATEENSEMBLEIMPLEMENTATION_H
6#define OILAB_GBMESOSTATEENSEMBLEIMPLEMENTATION_H
7
8#include <randomInteger.h>
9
10namespace gbLAB {
11 template<int dim>
14 //const Eigen::Vector<int,dim>& scales) :
15 std::vector<LatticeVector<dim>>& ensembleCslVectors,
16 const double& bhalfMax):
17 GbShifts<dim>(gb,
18 axis,
19 std::vector<LatticeVector<dim>>(ensembleCslVectors.begin()+1,
20 ensembleCslVectors.end()),
21 bhalfMax),
22 ensembleCslVectors(ensembleCslVectors),
23 bicrystalConfig(getBicrystalConfig((const GbShifts<dim>&) *this,
24 ensembleCslVectors))
25 {
26 std::cout << "--------------------GBMesoStateEnsemble class construction ---------------------------" << std::endl;
27 std::cout << "Forming mesostate ensemble with material parameters: ";
28 std::cout << "lambda = " << GbMaterialTensors::lambda;
29 std::cout << "; mu = " << GbMaterialTensors::mu<< std::endl;
30 std::cout << std::endl;
31
32 std::cout << "Ensemble CSL vectors:" << std::endl;
33 for(const auto& latticeVector : ensembleCslVectors)
34 std::cout << latticeVector.cartesian().transpose() << std::endl;
35 std::cout << std::endl;
36
37 }
38
39 /*-------------------------------------*/
40 template<int dim>
43 std::vector<LatticeVector<dim>>& ensembleCslVectors)
44 {
45 auto allLatticeVectors= gbs.gb.bc.box(ensembleCslVectors,1,1,"bc.txt");
46 std::vector<LatticeVector<dim>> output;
47
48 // include only lattice vectors in A and B
49 for (const auto& latticeVector : allLatticeVectors) {
50 if (&latticeVector.lattice == &gbs.gb.bc.A || &latticeVector.lattice == &gbs.gb.bc.B)
51 output.push_back(latticeVector);
52 }
53
54 return output;
55 }
56 /*-------------------------------------*/
57 template<int dim>
58 std::deque<std::tuple<LatticeVector<dim>,typename GbMesoStateEnsemble<dim>::VectorDimD,int>>
60 const typename GbMesoStateEnsemble<dim>::Constraints& constraints)
61 {
63 assert(bShiftPairs.size() == constraints.size());
64
65 std::deque<std::tuple<LatticeVector<dim>,VectorDimD,int>> bsPairs;
66 for(int i=0; i<bShiftPairs.size(); ++i) {
67 if (constraints(i) == 1 || constraints(i) == 2) {
68 const auto& b= bShiftPairs[i].first;
69 auto shift= bShiftPairs[i].second;
70 bsPairs.push_back(std::make_tuple(b,shift,constraints(i)));
71 }
72 }
73 return bsPairs;
74 }
75
76 /*-------------------------------------*/
77 template<int dim>
78 std::map<typename GbMesoStateEnsemble<dim>::Constraints,GbMesoState<dim>> GbMesoStateEnsemble<dim>::collectMesoStates(const std::string& filename) const
79 {
80 std::deque<Constraints> constraintsEnsemble(enumerateConstraints( (const GbShifts<dim>&) *this));
81 std::cout << "Number of mesostates in the ensemble = " << constraintsEnsemble.size() << std::endl;
82 std::cout << std::endl;
83 std::cout << "------------------------------" << std::endl;
84 std::map<Constraints,GbMesoState<dim>> mesoStates;
85
86 int count= -1;
87 for(const Constraints& constraints : constraintsEnsemble)
88 {
89 try {
90 if (constraints(0) != 1) continue;
91 //mesoStates.emplace_back(constructMesoState(constraints));
92 mesoStates.emplace(constraints,constructMesoState(constraints));
93 count++;
94 std::cout << "Constructing mesostate " << count << " of " << constraintsEnsemble.size() << std::endl;
95 std::cout << "Mesostate signature: " << constraints.transpose() << std::endl;
96 if (!filename.empty())
97 //mesoStates.back().box(filename + std::to_string(count));
98 mesoStates.at(constraints).box(filename + std::to_string(count));
99 }
100 catch(std::runtime_error& e)
101 {
102 std::cout << e.what() << std::endl;
103 }
104
105 }
106 return mesoStates;
107 }
108
109 /*-------------------------------------*/
110 template<int dim>
112 std::deque<std::tuple<LatticeVector<dim>,VectorDimD,int>> bsPairs(bsPairsFromConstraints(this->bShiftPairs,constraints));
113 try {
114 GbMesoState<dim> mesostate(this->gb, this->axis, bsPairs, ensembleCslVectors, bicrystalConfig);
115 return mesostate;
116 }
117 catch(std::runtime_error& e)
118 {
119 throw(e);
120 }
121 }
122
123 /*-------------------------------------*/
124 /*
125 template<int dim>
126 std::map<typename GbMesoStateEnsemble<dim>::Constraints, GbMesoState<dim>> GbMesoStateEnsemble<dim>::evolveMesoStates(const double& temperature, const int& resetEvery, const int& maxIterations, const std::string& filename) const
127 {
128 std::map<Constraints,GbMesoState<dim>> mesoStates;
129 std::map<Constraints,double> visitedStatesEnergy;
130
131 // create the initial random mesostate
132 Constraints initialConstraints(this->bShiftPairs.size());
133 initialConstraints.setZero();
134 std::cout << "Initial mesostate signature = " << initialConstraints.transpose() << std::endl;
135 GbMesoState<dim> initial_ms(this->gb,
136 this->axis,
137 bsPairsFromConstraints(this->bShiftPairs,initialConstraints),
138 ensembleCslVectors,
139 bicrystalConfig);
140 //double newEnergy= initial_ms.energy();
141 std::pair<double, double> newDensityEnergyPair= initial_ms.densityEnergy();
142 double newEnergy= newDensityEnergyPair.second;
143 int count= 0;
144 int mesoStateCount= 0;
145 double currentEnergy= 0.0;
146 Constraints currentConstraints(initialConstraints);
147 bool transition= true;
148
149 for(int i=0; i< maxIterations; ++i) {
150 // form mesostate with currentConstraints
151 if (transition) {
152 GbMesoState<dim> current_ms(this->gb,
153 this->axis,
154 bsPairsFromConstraints(this->bShiftPairs, currentConstraints),
155 ensembleCslVectors,
156 bicrystalConfig);
157 currentEnergy = newEnergy;
158
159 // box the mesostate only if it is a new state
160 if (mesoStates.find(currentConstraints) == mesoStates.end()) {
161 mesoStates.insert({currentConstraints, current_ms});
162 if (!filename.empty())
163 current_ms.box(filename + std::to_string(mesoStateCount));
164 std::cout << "Step " << i << " Accept " << count << " Mesostate " << mesoStateCount << " constraints = "
165 << currentConstraints.transpose() << "; energy = " << currentEnergy << std::endl;
166 mesoStateCount++;
167 }
168 transition = false;
169 }
170
171 // new mesostate construction
172 bool randomize= false;
173 if (count % resetEvery == 0 && count != 0) {
174 std::cout << "Starting over using ramdomized constraints" << std::endl;
175 randomize= true;
176 }
177 Constraints newConstraints(this->bShiftPairs.size());
178 bool msConstructionSuccess= false;
179 while(!msConstructionSuccess) {
180 try {
181 newConstraints= currentConstraints;
182 // alter the constraints
183 if (count % resetEvery == 0 && count != 0) {
184 for (auto &elem: newConstraints)
185 elem = random<int>(0, 2);
186 }
187 else {
188 int randomSpot = random<int>(0, this->bShiftPairs.size() - 1);
189 newConstraints(randomSpot) = random<int>(0, 2);
190 }
191 GbMesoState<dim> temp(this->gb,
192 this->axis,
193 bsPairsFromConstraints(this->bShiftPairs, newConstraints),
194 ensembleCslVectors,
195 bicrystalConfig);
196 msConstructionSuccess= true;
197 }
198 catch(std::runtime_error& e)
199 {
200 //throw(e);
201 }
202 }
203 GbMesoState<dim> new_ms(this->gb,
204 this->axis,
205 bsPairsFromConstraints(this->bShiftPairs, newConstraints),
206 ensembleCslVectors,
207 bicrystalConfig);
208
209
210 // auto newConstraintsMesoStatePair= sampleNewMesoState(currentConstraints,randomize);
211 // Constraints newConstraints(newConstraintsMesoStatePair.first);
212 // GbMesoState<dim> new_ms(newConstraintsMesoStatePair.second);
213
214 Constraints newConstraints(sampleNewState(currentConstraints,randomize));
215 GbMesoState<dim> new_ms(constructMesoState(newConstraints));
216
217 if (visitedStatesEnergy.find(newConstraints) != visitedStatesEnergy.end())
218 newEnergy = visitedStatesEnergy.at(newConstraints);
219 else {
220 //newEnergy = new_ms.energy();
221 newDensityEnergyPair= new_ms.densityEnergy();
222 newEnergy= newDensityEnergyPair.second;
223 visitedStatesEnergy[newConstraints] = newEnergy;
224 }
225
226 double delta = newEnergy - currentEnergy;
227 double probability = std::min(0.9, exp(-delta / temperature));
228
229 if (random<double>(0.0, 1.0) <= probability) {
230 currentConstraints = newConstraints;
231 count++;
232 transition = true;
233 }
234 }
235 return mesoStates;
236 }
237 */
238 /*-------------------------------------*/
239 template<int dim>
240 std::deque<typename GbMesoStateEnsemble<dim>::Constraints> GbMesoStateEnsemble<dim>::enumerateConstraints(const GbShifts<dim>& gbs)
241 {
242 std::deque<Constraints> constraintsEnsemble;
243
244 //auto tuples= XTuplet::generate_tuples(3,gbs.bShiftPairs.size());
245 auto tuples= XTuplet::generate_tuples(2,gbs.bShiftPairs.size());
246
247 // identify indices of pre-existing CSL points
248 std::vector<int> cslIndices;
249 for(int i=0; i<gbs.bShiftPairs.size(); ++i)
250 {
251 if((gbs.bShiftPairs[i].first.array()==0).all())
252 cslIndices.push_back(i);
253 }
254
255 for (auto& tuple : tuples) {
256 bool pass= true;
257 if (tuple(0) !=1)
258 pass= false;
259
260 for(const auto& index : cslIndices) {
261 if (tuple(index) == 0)
262 tuple(index)= 2;
263 }
264
265
266 /*
267 //if (!(tuple.array()==0).all())
268 // allow only constraints that do not alter pre-existing CSL points
269 for(const auto& index : cslIndices) {
270 if (tuple(index) != 1) {
271 pass = false;
272 break;
273 }
274 }
275 */
276 if (pass)
277 constraintsEnsemble.push_back(tuple);
278 }
279
280 return constraintsEnsemble;
281 }
282
283 /*-------------------------------------*/
284 template<int dim>
286 const bool& randomize) const
287 {
288 // new mesostate construction
289 Constraints newConstraints(this->bShiftPairs.size());
290 bool msConstructionSuccess = false;
291 while (!msConstructionSuccess) {
292 try {
293 newConstraints = currentConstraints;
294 // alter the constraints
295 if (randomize){
296 for (auto &elem: newConstraints)
297 elem = random<int>(0, 2);
298 } else {
299 int randomSpot = random<int>(0, this->bShiftPairs.size() - 1);
300 newConstraints(randomSpot) = random<int>(0, 2);
301 }
302 GbMesoState<dim> temp(this->gb,
303 this->axis,
304 bsPairsFromConstraints(this->bShiftPairs, newConstraints),
305 ensembleCslVectors,
306 bicrystalConfig);
307 msConstructionSuccess = true;
308 }
309 catch (std::runtime_error &e) {
310 //throw(e);
311 }
312 }
313 return newConstraints;
314 }
315
316 /*-------------------------------------*/
317 template<int dim>
319 {
320 Constraints initialConstraints(this->bShiftPairs.size());
321 initialConstraints.setZero();
322 return initialConstraints;
323 }
324
325}
326#endif
Definition Gb.h:16
std::map< Constraints, GbMesoState< dim > > collectMesoStates(const std::string &filename="") const
Constructs an ensemble of mesostates.
std::vector< LatticeVector< dim > > BicrystalLatticeVectors
Constraints sampleNewState(const Constraints &currentConstraints, const bool &randomize=false) const
static std::deque< Constraints > enumerateConstraints(const GbShifts< dim > &gbs)
typename LatticeCore< dim >::VectorDimD VectorDimD
static BicrystalLatticeVectors getBicrystalConfig(const GbShifts< dim > &gbs, std::vector< LatticeVector< dim > > &ensembleCslVectors)
Constructs bicrystalConfig and ensembleCslVectors.
std::vector< LatticeVector< dim > > ensembleCslVectors
GbMesoStateEnsemble(const Gb< dim > &gb, const ReciprocalLatticeVector< dim > &axis, std::vector< LatticeVector< dim > > &ensembleCslVectors, const double &bhalfMax)
static std::deque< std::tuple< LatticeVector< dim >, VectorDimD, int > > bsPairsFromConstraints(const std::vector< std::pair< LatticeVector< dim >, VectorDimD > > &bShiftPairs, const Constraints &constraints)
GbMesoState< dim > constructMesoState(const Constraints &constraints) const
Evove mesostates using a Monte Carlo algorithm.
std::vector< std::pair< LatticeVector< dim >, VectorDimD > > bShiftPairs
Definition GbShifts.h:27
const Gb< dim > & gb
Definition GbShifts.h:24
LatticeVector class.
static void generate_tuples(int n, int k, std::vector< XTuplet > &results, XTuplet &current_tuple, int index)