Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
MDStackingFaultNoise.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_MDStackingFaultNoise_cpp
9#define model_MDStackingFaultNoise_cpp
10
12#include <cmath>
13
14namespace model
15{
17 const std::string& tag,
18 const std::string& correlationFile_in,
19 const int& seed,
22 const Eigen::Matrix<double,2,2>& latticeBasis
23 ) :
24 /* init */ GlidePlaneNoiseBase<1>("MDStackingFaultNoise"+tag,seed,gridSize,gridSpacing,nonOrthogonalBasisReader(correlationFile_in))
25 /* init */,correlationFile(correlationFile_in)
26{
27 // read the dimension of the original correlation
28 const auto originalDimensions(readVTKfileDimension(correlationFile.c_str()));
29
30 if(originalDimensions(2)!=1)
31 {
32 throw std::runtime_error("vtk stacking fault correlationFile 'DIMENSIONS' should have 3rd component == 1.");
33 }
34
35 const int originalNX = originalDimensions(0);
36 const int originalNY = originalDimensions(1);
37 const int originalNZ = originalDimensions(2);
38 const int originalNR = originalNX*originalNY*originalNZ;
39
40 // compute variable energy correction factor from zero padding
41 eNormFactor = (this->NR!=originalNR ? static_cast<double>(this->NR)/static_cast<double>(originalNR) : 1.0);
42 // fftScaleFactor is the scaling factor for FFT
43 fftScaleFactor = std::pow(static_cast<double>(this->NR), 2.0);
44
45 // original input correlation array
46 REAL_SCALAR *Rr_original = (REAL_SCALAR*) fftw_malloc(sizeof(REAL_SCALAR)*originalNR);
47 // explicitly initialize to zeros, avoid garbarge propagation
48 std::fill(Rr_original, Rr_original+originalNR, REAL_SCALAR{0.0});
49
50 // populate Rr_original with the correlation data
51 StackingFaultCorrelationReader(correlationFile, Rr_original, originalNR);
52
53 // unit conversion (from J^2/m^4 to unitless)
54 for (int i = 0; i < originalNR; ++i)
55 {
56 const REAL_SCALAR unitconvert = mat.b_SI*mat.mu_SI;
57 Rr_original[i] /= (unitconvert*unitconvert);
58 }
59
60 // allocate correlation array with zero padding in real space
61 REAL_SCALAR *Rr = (REAL_SCALAR*) fftw_malloc(sizeof(REAL_SCALAR)*this->NR);
62 std::fill(Rr, Rr+this->NR, REAL_SCALAR{0.0});
63
64 // if the assigned grid size is the same as the actual data
65 if(this->NR == originalNR)
66 {
67 // point to the same memory location
68 Rr = Rr_original;
69 }
70 // pad with zeros if the assigned grid size is larger than the actual data grid size
71 else
72 {
73 int start_y = (this->NY - originalNY) / 2;
74 int start_x = (this->NX - originalNX) / 2;
75 // 0-pading from centere
76 for (int i = 0; i < originalNY; ++i)
77 {
78 for (int j = 0; j < originalNX; ++j)
79 {
80 Rr[(start_y + i) * this->NX + (start_x + j)] = Rr_original[i * originalNX + j];
81 }
82 }
83 // Free allocated memory
84 fftw_free(Rr_original);
85 }
86
87 // allocate correlation array in fourier space
88 Rk = (COMPLEX*) fftw_malloc(sizeof(COMPLEX)*this->NK);
89 std::fill(Rk, Rk+this->NK, COMPLEX{0.0, 0.0});
90
91 // Execute FFTW plans to populate Rk
92 fftw_plan plan_R_r2c = fftw_plan_dft_r2c_2d(this->NY, this->NX, Rr, reinterpret_cast<fftw_complex*>(Rk), FFTW_ESTIMATE);
93 fftw_execute(plan_R_r2c);
94
95 // Destroy FFTW plans
96 fftw_destroy_plan(plan_R_r2c);
97
98 // Free allocated memory
99 fftw_free(Rr);
100}
101
102std::array<MDStackingFaultNoise::COMPLEX,1> MDStackingFaultNoise::kCorrelations(const Eigen::Matrix<double, 3, 1> &kv, const Eigen::Matrix<int, 3, 1> &index) const
103{
104 int idx=(this->NY/2+1)*NZ*index(0) + index(1)*NZ + index(2);
105 // eNormFactor is variable energy correction factor from zero padding (1 if not padded)
106 // fftScaleFactor is the Scaling factor for FFT (match the original input scale)
107 std::array<MDStackingFaultNoise::COMPLEX,1> temp{eNormFactor*Rk[idx]/fftScaleFactor};
108 return temp;
109}
110
111Eigen::Matrix<double,2,2> MDStackingFaultNoise::nonOrthogonalBasisReader(const std::string& fileName_vtk) const
112{
113 typedef Eigen::Matrix<double,2,1> VectorDimD;
114 std::deque<VectorDimD> basis1;
115 std::deque<VectorDimD> basis2;
116 std::ifstream vtkFile(fileName_vtk); //access vtk file
117 // error check
118 if (!vtkFile.is_open()) {
119 throw std::runtime_error("Error opening stacking fault VTK correlation file!");
120 }
121 // begin parsing structured_grid vtk file for lines
122 std::string line;
123 while (std::getline(vtkFile, line))
124 {
125 if(line.find("VECTORS")!=std::string::npos)
126 {
127 std::getline(vtkFile, line);
128 std::stringstream ss(line);
129 double x, y, z;
130 if(basis1.empty())
131 {
132 ss >> x >> y >> z;
133 VectorDimD vec = (VectorDimD() << x, y).finished();
134 vec.normalize(); // Normalize the vector
135 basis1.push_back(vec);
136 }
137 else
138 {
139 ss >> x >> y >> z;
140 VectorDimD vec = (VectorDimD() << x, y).finished();
141 vec.normalize(); // Normalize the vector
142 basis2.push_back(vec);
143 }
144
145 }
146 }
147
148 Eigen::Matrix<double,2,2> nonOrthoBasisMatrix;
149 // fill the nonOrthoBasisMatrix with a block that has the size of <2,1> at the beginning of the first column
150 nonOrthoBasisMatrix.block<2,1>(0,0) = basis1.front();
151 // fill the nonOrthoBasisMatrix with a block that has the size of <2,1> at the beginning of the second column
152 nonOrthoBasisMatrix.block<2,1>(0,1) = basis2.front();
153
154 // transpose the matrix to create a transition matrix
155 return nonOrthoBasisMatrix;
156}
157
159{
160 std::cout << "Reading stacking fault correlation" << std::endl;
161
162 std::ifstream vtkFile(correlationFile); // access vtk file
163 // error check
164 if (!vtkFile.is_open())
165 {
166 throw std::runtime_error("Error opening stacking fault VTK correlation file!");
167 }
168
169 std::string line;
170 while (std::getline(vtkFile, line))
171 {
172 // if the "POINT_DATA" string is read, read the following data
173 if (line.find("POINT_DATA") != std::string::npos)
174 {
175 const size_t numOfHeaders = 2;
176 // get the number of points in the file
177 const size_t firstSpace(line.find(' '));
178 const size_t numOfPoints = std::atoi(line.substr(firstSpace+1).c_str());
179 // read the point coordinates
180 for (size_t n = 0; n < numOfPoints + numOfHeaders; ++n)
181 {
182 std::getline(vtkFile, line);
183 // ignore the headers that come right after point_data
184 if (n < numOfHeaders)
185 continue;
186 const int ind = n - numOfHeaders;
187 // correlationCoeffs.push_back(std::atoi(line.c_str()));
188 if (ind >= NR)
189 {
190 throw std::runtime_error("Index out of bounds while populating the original correlation array.");
191 }
192
193 try
194 {
195 double value = std::stod(line);
196 //float value = std::atof(line.c_str());
197 Rr[ind] = value;
198 }
199 catch(const std::invalid_argument& e)
200 {
201 std::cerr << "Invalid correlation data in line: " << line << std::endl;
202 }
203 catch(const std::out_of_range& e)
204 {
205 std::cerr << "Out of range correlation value in line: " << line << std::endl;
206 }
207 }
208 }
209 }
210 vtkFile.close(); // Close the file after reading
211}
212
214{
215 FILE *InFile=fopen(fname,"r");
216
217 if (InFile == NULL)
218 {
219 fprintf(stderr, "Can't open stacking fault correlation VTK file %s\n",fname);
220 exit(1);
221 }
222 // return the 5th line of the vtk file
223 char line[200];
224 for(int i=0;i<4;i++)
225 {
226 fgets(line, 200, InFile);
227 }
228 // scan the returned line
229 int NXX, NYY, NZZ;
230 fscanf(InFile, "%s %d %d %d\n", line, &(NXX), &(NYY), &(NZZ));
231 return (GridSizeType()<<NXX,NYY,NZZ).finished();
232}
233
234}
235#endif
id z()
x
Definition int_R.m:12
GlidePlaneNoiseBase(const std::string &tag_in, const int &seed_in, const NoiseTraitsBase::GridSizeType &gridSize_in, const NoiseTraitsBase::GridSpacingType &gridSpacing_SI_in, const Eigen::Matrix< double, 2, 2 > &latticeBasis)
Eigen::Matrix< double, 2, 2 > nonOrthogonalBasisReader(const std::string &fileName_vtk) const
MDStackingFaultNoise(const PolycrystallineMaterialBase &mat, const std::string &tag, const std::string &correlationFile_in, const int &seed, const GridSizeType &gridSize, const GridSpacingType &gridSpacing, const Eigen::Matrix< double, 2, 2 > &latticeBasis)
NoiseTraits< 1 >::REAL_SCALAR REAL_SCALAR
NoiseTraits< 1 >::GridSizeType GridSizeType
NoiseTraits< 1 >::COMPLEX COMPLEX
NoiseTraitsBase::GridSpacingType GridSpacingType
std::array< COMPLEX, 1 > kCorrelations(const Eigen::Matrix< double, 3, 1 > &kv, const Eigen::Matrix< int, 3, 1 > &index) const override
void StackingFaultCorrelationReader(const std::string &fileName_vtk, REAL_SCALAR *Rr, const int &NR)
GridSizeType readVTKfileDimension(const char *fname)
int j
newsubseg ss[0]
int index
int i
int e
vertex temp
fprintf(outfile, "# Generated by")