Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
ShearLoopGenerator.cpp
Go to the documentation of this file.
1/* This file is part of MoDELib, the Mechanics Of Defects Evolution Library.
2 *
3 *
4 * MoDELib is distributed without any warranty under the
5 * GNU General Public License (GPL) v2 <http://www.gnu.org/licenses/>.
6 */
7
8#ifndef model_ShearLoopGenerator_cpp_
9#define model_ShearLoopGenerator_cpp_
10
12#include <iostream>
13#include <ShearLoopGenerator.h>
14
15namespace model
16{
17
18// ShearLoopGenerator::ShearLoopGenerator(const std::string& fileName) :
19// /* init */ MicrostructureGeneratorBase(fileName)
20// {
21//
22// }
23
25{
26 // const double targetDensity(this->parser.readScalar<double>("targetDensity",true));
27 std::cout<<magentaBoldColor<<"Generating shear loop density"<<defaultColor<<std::endl;
28 double density=0.0;
29 std::cout<<"shear loop density="<<density<<std::endl;
30 if(spec.targetDensity_SI>0.0)
31 {
32 // const int numberOfSides(this->parser.readScalar<int>("numberOfSides",true));
33 // const double radiusDistributionMean(this->parser.readScalar<double>("radiusDistributionMean",true));
34 // const double radiusDistributionStd(this->parser.readScalar<double>("radiusDistributionStd",true));
35 std::normal_distribution<double> radiusDistribution(spec.radiusDistributionMean_SI/mg.ddBase.poly.b_SI,spec.radiusDistributionStd_SI/mg.ddBase.poly.b_SI);
36 std::mt19937 generator;
37
38 std::uniform_int_distribution<> allowedSlipSystemDist(0,spec.allowedSlipSystemIDs.size()-1);
39
40 std::set<int> allowedGrainIDsSet;
41 for(const auto& gID : spec.allowedGrainIDs)
42 {
43 allowedGrainIDsSet.insert(gID);
44 }
45 const double allowAllGrains(allowedGrainIDsSet.size() ? (*allowedGrainIDsSet.begin())<0 : true);
46
47
48 while(density<spec.targetDensity_SI)
49 {
50 const std::pair<LatticeVector<3>, int> rp(mg.ddBase.poly.randomLatticePointInMesh());
51 const LatticeVector<3> L0=rp.first;
52 const size_t grainID=rp.second;
53
54 if(allowAllGrains || allowedGrainIDsSet.find(grainID)!=allowedGrainIDsSet.end())
55 {
56 std::uniform_int_distribution<> ssDist(0,mg.ddBase.poly.grain(grainID)->slipSystems().size()-1);
57 const int allowedIndex(allowedSlipSystemDist(generator));
58 const int allowedSlipID(spec.allowedSlipSystemIDs[allowedIndex]);
59 const int rSS(allowedSlipID<0 ? ssDist(generator) : allowedSlipID); // a random SlipSystem
60
61 const double radius(radiusDistribution(generator));
62 try
63 {
64 const bool success=generateSingle(mg,rSS,L0.cartesian(),radius,spec.numberOfSides);
65 if(success)
66 {
67 density+=2.0*std::numbers::pi*radius/mg.ddBase.mesh.volume()/std::pow(mg.ddBase.poly.b_SI,2);
68 std::cout<<"shear loop density="<<density<<std::endl;
69 }
70 }
71 catch(const std::exception& e)
72 {
73
74 }
75 }
76 }
77 }
78}
79
81{
82 std::cout<<magentaBoldColor<<"Generating individual shear loops"<<defaultColor<<std::endl;
83
84 if(spec.slipSystemIDs.size()!=spec.loopRadii.size())
85 {
86 throw std::runtime_error("loopSlipSystemIDs.size()="+std::to_string(spec.slipSystemIDs.size())+" NOT EQUAL TO loopRadii.size()="+std::to_string(spec.loopRadii.size()));
87 }
88 if(int(spec.slipSystemIDs.size())!=spec.loopCenters.rows())
89 {
90 throw std::runtime_error("slipSystemIDs.size()="+std::to_string(spec.slipSystemIDs.size())+" NOT EQUAL TO loopCenters.rows()="+std::to_string(spec.loopCenters.rows()));
91 }
92 if(spec.slipSystemIDs.size()!=spec.loopSides.size())
93 {
94 throw std::runtime_error("slipSystemIDs.size()="+std::to_string(spec.slipSystemIDs.size())+" NOT EQUAL TO loopSides.size()="+std::to_string(spec.loopSides.size()));
95 }
96
97 double density=0.0;
98 for(size_t k=0;k<spec.slipSystemIDs.size();++k)
99 {
100 try
101 {
102 const bool success=generateSingle(mg,spec.slipSystemIDs[k],spec.loopCenters.row(k),spec.loopRadii[k]/mg.ddBase.poly.b_SI,spec.loopSides[k]);
103 if(success)
104 {
105 density+=2.0*std::numbers::pi*spec.loopRadii[k]/mg.ddBase.mesh.volume()/std::pow(mg.ddBase.poly.b_SI,2);
106 std::cout<<"shear loop density="<<density<<std::endl;
107 }
108 }
109 catch(const std::exception& e)
110 {
111
112 }
113 }
114}
115
116bool ShearLoopGenerator::generateSingle(MicrostructureGenerator& mg,const int& rSS,const VectorDimD& center,const double& radius,const size_t& sides)
117{
118 bool success =false;
119 std::pair<bool,const Simplex<3,3>*> found(mg.ddBase.mesh.search(center));
120 if(!found.first)
121 {
122 throw std::runtime_error("ShearLoopGenerator::generateSingle:: point is outside mesh.");
123 }
124
125 const int grainID(found.second->region->regionID);
126 // std::cout<<"grainID="<<grainID<<std::endl;
127 assert(mg.ddBase.poly.grains.size()==1 && "Periodic dislocations only supported for single crystals");
128 const auto& grain(mg.ddBase.poly.grain(grainID));
129
130 if(rSS>=0 && rSS<int(grain->slipSystems().size()))
131 {
132 const auto& slipSystem(*grain->slipSystems().at(rSS));
133
134
135 const long int planeIndex(slipSystem.n.closestPlaneIndexOfPoint(center));
136 GlidePlaneKey<3> glidePlaneKey(planeIndex, slipSystem.n);
137 std::shared_ptr<PeriodicGlidePlane<3>> glidePlane(mg.ddBase.dislocationDynamics().periodicGlidePlaneFactory.get(glidePlaneKey));
138 const VectorDimD P0(glidePlane->referencePlane->snapToPlane(center));
139 // std::cout<<"P0="<<P0.transpose()<<std::endl;
140
141 std::vector<VectorDimD> loopNodePos;
142 for(size_t k=0;k< sides;++k)
143 {
144 loopNodePos.push_back(P0+Eigen::AngleAxisd(k*2.0*std::numbers::pi/sides,slipSystem.unitNormal)*slipSystem.s.cartesian().normalized()*radius);
145 }
146
147
148 // std::map<VectorDimD,size_t,CompareVectorsByComponent<double,dim,float>> uniqueNetworkNodeMap; // networkNodePosition->networkNodeID
149
150// mg.insertJunctionLoop(loopNodePos,glidePlane,
151// slipSystem.s.cartesian(),glidePlane->referencePlane->unitNormal,
152// P0,grainID,DislocationLoopIO<3>::GLISSILELOOP);
153// success=true;
154
155
156 success=mg.insertJunctionLoop(loopNodePos,glidePlane,
157 slipSystem.s.cartesian(),glidePlane->referencePlane->unitNormal,
159 }
160 else
161 {
162 if(rSS<0)
163 {
164 std::cout<<"Skipping slip system "<<rSS<<std::endl;
165 }
166 else
167 {
168 throw std::runtime_error("slipSystem "+std::to_string(rSS)+" not found, skipping.");
169 }
170 }
171 return success;
172}
173
174}
175#endif
bool insertJunctionLoop(const std::vector< VectorDimD > &loopNodePos, const std::shared_ptr< PeriodicGlidePlane< 3 > > &periodicPlane, const VectorDimD &b, const VectorDimD &unitNormal, const VectorDimD &P0, const size_t &grainID, const DislocationLoopIO< dim >::DislocationLoopType &loopType)
DefectiveCrystalBase< 3 > & ddBase
ShearLoopGenerator(const ShearLoopDensitySpecification &, MicrostructureGenerator &mg)
std::pair< bool, const Simplex< _dim, _dim > * > search(const Eigen::Matrix< double, dim, 1 > &P) const
const double & volume() const
static std::string defaultColor
static std::string magentaBoldColor
LatticePlaneKey< dim > GlidePlaneKey
const Polycrystal< dim > & poly
const SimplicialMesh< dim > & mesh
DislocationDynamicsBase< dim > & dislocationDynamics() const
int sides
int k
enum insertvertexresult success
int e