v0.16.0
Loading...
Searching...
No Matches
DispMap.hpp
Go to the documentation of this file.
1/* Copyright (c) 2025 Authors under the MIT License.
2 * See LICENSE.md for details.
3 * SPDX-License-Identifier: MIT
4 */
5
6#ifndef __DISP_MAP_HPP__
7#define __DISP_MAP_HPP__
8
9namespace CellEngineering {
10
14 /** \brief Set integrate rule
15 */
16 int getRule(int order) { return 2 * order + 1; };
17};
18
20
21 string &fIle;
22
23 double sCale;
24 double cUbe_size;
25 vector<vector<double>> main_vector;
26 double lAmbda;
27
28 DataFromFiles(string &file) : fIle(file) {}
29
30 MoFEMErrorCode loadFileData() {
32 ifstream in(fIle.c_str());
33 if (!in.is_open()) {
34 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
35 "file with data points not found");
36 // cout << ">> couldn't read the file <<"<< endl;
37 }
38
39 typedef boost::tokenizer<boost::escaped_list_separator<char>> Tokenizer;
40
41 string line;
42
43 main_vector.clear();
44 while (getline(in, line)) {
45 vector<double> vec;
46 Tokenizer tok(line);
47 for (Tokenizer::iterator it(tok.begin()), end(tok.end()); it != end;
48 ++it) {
49 vec.push_back(atof(it->c_str()));
50 }
51
52 main_vector.push_back(vec);
53 }
54 reverse(main_vector.begin(), main_vector.end());
56 }
57
58 MoFEMErrorCode fromOptions() {
60 PetscOptionsBegin(PETSC_COMM_WORLD, "", "Get parameters", "none");
61
62 cUbe_size = 4;
63 CHKERR PetscOptionsScalar("-cube_size", "get cube size for the averaging",
64 "", 0, &cUbe_size, PETSC_NULLPTR);
65
66 sCale = 1;
67 CHKERR PetscOptionsScalar(
68 "-scale", "scale the distance between voxels (eg. from mm to m)", "",
69 sCale, &sCale, PETSC_NULLPTR);
70
71 PetscOptionsEnd();
72
74 }
75
76 /**
77 * returns vertors of data for given index of the point
78 * @param vec_idx reference vector of indices
79 * @param vec_data reference vector of data (colors)
80 * @return error code
81 */
82
83 double getDataForGivenCoordinate(const double x, const double y,
84 vector<vector<double>> &main_vector,
85 const double sCale, const double cUbe_size) {
86
87 const int size_y = main_vector.size(); // flip
88 const int size_x = main_vector.begin()->size();
89 double data = 0;
90
91 vector<int> vec_ix;
92 vector<int> vec_iy;
93 vector<double> vec_data;
94 vector<double> vec_dist;
95
96 int nx = ceil(cUbe_size);
97 int ny = ceil(cUbe_size);
98
99 int ix = ceil(x / sCale);
100 int iy = ceil(y / sCale);
101
102 vec_ix.resize(2 * nx);
103 vec_iy.resize(2 * ny);
104
105 int ii = 0;
106 for (int i = 0; i < 2 * nx; i++) {
107 int idx = ix - nx + i;
108 if (idx >= size_x / 2 || idx < -size_x / 2)
109 continue;
110 vec_ix[ii++] = idx;
111 }
112 vec_ix.resize(ii);
113 ii = 0;
114 for (int i = 0; i < 2 * ny; i++) {
115 int idx = iy - ny + i;
116 if (idx >= size_y / 2 || idx < -size_y / 2)
117 continue;
118 vec_iy[ii++] = idx;
119 }
120 vec_iy.resize(ii);
121 ii = 0;
122
123 vec_data.resize(vec_iy.size() * vec_ix.size());
124 vec_dist.resize(vec_iy.size() * vec_ix.size());
125
126 for (vector<int>::iterator it_iy = vec_iy.begin(); it_iy != vec_iy.end();
127 it_iy++) {
128 for (vector<int>::iterator it_ix = vec_ix.begin(); it_ix != vec_ix.end();
129 it_ix++) {
130 vec_data[ii] =
131 main_vector[*it_iy + size_y / 2][*it_ix + size_x / 2]; // flip
132 vec_dist[ii] = ((*it_iy * sCale - y) * (*it_iy * sCale - y) +
133 (*it_ix * sCale - x) * (*it_ix * sCale - x));
134
135 ii++;
136 }
137 }
138
139 // gaussian smoothing
140 vector<double> kernel;
141 kernel.resize(vec_data.size());
142 int i = 0;
143 double sigma = 10;
144 double sum = 0;
145 const double m = (1 / sqrt(M_PI * 2 * sigma * sigma));
146 for (int ii = 0; ii < vec_dist.size(); ii++) {
147 kernel[i] = m * exp(-(vec_dist[ii]) /
148 (2 * sigma * sigma)); // distance is already squared
149 sum += kernel[i++];
150 }
151 ii = 0;
152 for (vector<double>::iterator vec_itr = vec_data.begin();
153 vec_itr != vec_data.end(); vec_itr++) {
154 kernel[ii] /= sum;
155 data += (*vec_itr) * kernel[ii++];
156 }
157 return data;
158 }
159
160};
161
162struct CommonData {
163
164 VectorDouble dispAtGaussPts; ///< Values at integration Pts
165 VectorDouble locF; ///< Local element rhs vector
166 MatrixDouble locA; ///< Local element matrix
167 MatrixDouble transLocA;
168 Mat globA; ///< Global matrix
169 Vec globF;
170 double lAmbda;
171
172 MoFEMErrorCode getParameters() {
173
175 PetscOptionsBegin(PETSC_COMM_WORLD, "", "", "none");
176
177 this->lAmbda = 1;
178 CHKERR PetscOptionsScalar("-lambda", "lambda parameter", "", 1, &lAmbda,
179 PETSC_NULLPTR);
180 PetscOptionsEnd();
181
183 }
184};
185
188
191
192 OpCalculateLhs(const std::string &row_field, const std::string &col_field,
193 CommonData &common_data, DataFromFiles &data_from_files)
195 row_field, col_field, UserDataOperator::OPROWCOL),
196 commonData(common_data), dataFromFiles(data_from_files) {
197 sYmm = true;
198 }
199
200 MoFEMErrorCode doWork(int row_side, int col_side, EntityType row_type,
201 EntityType col_type,
202 DataForcesAndSourcesCore::EntData &row_data,
203 DataForcesAndSourcesCore::EntData &col_data) {
204
206
207 int nb_rows = row_data.getIndices().size(); // number of rows
208 int nb_cols = col_data.getIndices().size(); // number of columns
209 if (nb_rows == 0)
211 if (nb_cols == 0)
213
214 commonData.locA.resize(nb_rows, nb_cols, false);
215 commonData.locA.clear();
217 int nb_gauss_pts = row_data.getN().size1();
218 for (int gg = 0; gg < nb_gauss_pts; gg++) {
219 double area = getArea() * getGaussPts()(2, gg);
220 // if(getHoGaussPtsDetJac().size()>0) {
221 // area *= getHoGaussPtsDetJac()[gg]; ///< higher order geometry
222 // }
223 VectorAdaptor row_base_function = row_data.getN(gg);
224 VectorAdaptor col_base_function = col_data.getN(gg);
226 area * outer_prod(row_base_function, col_base_function);
227 MatrixAdaptor row_diff_base_function = row_data.getDiffN(gg);
228 MatrixAdaptor col_diff_base_function = col_data.getDiffN(gg);
229
230 // const double lambda = 1;
231
234 prod(row_diff_base_function, trans(col_diff_base_function));
235 }
236 CHKERR MatSetValues(commonData.globA, row_data.getIndices().size(),
237 &row_data.getIndices()[0], col_data.getIndices().size(),
238 &col_data.getIndices()[0], &commonData.locA(0, 0),
239 ADD_VALUES);
240
241 // Assemble off symmetric side
242 if (row_side != col_side || row_type != col_type) {
243 commonData.transLocA.resize(commonData.locA.size2(),
244 commonData.locA.size1(), false);
245 noalias(commonData.transLocA) = trans(commonData.locA);
246 CHKERR MatSetValues(
247 commonData.globA, col_data.getIndices().size(),
248 &col_data.getIndices()[0], row_data.getIndices().size(),
249 &row_data.getIndices()[0], &commonData.transLocA(0, 0), ADD_VALUES);
250 }
251
253 }
254};
255
258
261
262 OpCalulatefRhoAtGaussPts(const string &row_field, CommonData &common_data,
263 DataFromFiles &data_from_files)
265 row_field, row_field, UserDataOperator::OPROW),
266 commonData(common_data), dataFromFiles(data_from_files) {}
267
268 MoFEMErrorCode doWork(int row_side, EntityType row_type,
269 DataForcesAndSourcesCore::EntData &row_data) {
271
272 if (row_type != MBVERTEX)
274
275 int nb_rows = row_data.getIndices().size();
276 if (nb_rows == 0)
278
279 FTensor::Index<'i', 3> i;
280
282 &getCoordsAtGaussPts()(0, 1),
283 &getCoordsAtGaussPts()(0, 2), 3);
284 const int nb_gauss_pts = row_data.getN().size1();
285 commonData.dispAtGaussPts.resize(nb_gauss_pts, false);
286 for (int gg = 0; gg < nb_gauss_pts; gg++) {
287
289 t1_xyz(0), t1_xyz(1), dataFromFiles.main_vector, dataFromFiles.sCale,
291 ++t1_xyz;
292 }
293
295 }
296};
297
300
302
303 OpAssembleRhs(const string &row_field, CommonData &common_data)
305 row_field, row_field, UserDataOperator::OPROW),
306 commonData(common_data) {}
307
308 MoFEMErrorCode doWork(int row_side, EntityType row_type,
309 DataForcesAndSourcesCore::EntData &row_data) {
311
312 int nb_rows = row_data.getIndices().size();
313 if (nb_rows == 0)
315 commonData.locF.resize(nb_rows, false);
316 commonData.locF.clear();
317
318 int nb_gauss_pts = row_data.getN().size1();
319 for (int gg = 0; gg < nb_gauss_pts; gg++) {
320
321 double area = getArea() * getGaussPts()(2, gg);
322 VectorAdaptor base_function = row_data.getN(gg);
323 commonData.locF += area * commonData.dispAtGaussPts[gg] * base_function;
324 }
325
326 CHKERR VecSetValues(commonData.globF, row_data.getIndices().size(),
327 &row_data.getIndices()[0], &commonData.locF[0],
328 ADD_VALUES);
329
331 }
332};
333
334} // namespace CellEngineering
335
336#endif //__DISP_MAP_HPP__
ForcesAndSourcesCore::UserDataOperator UserDataOperator
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
constexpr int order
FTensor::Index< 'i', SPACE_DIM > i
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
FTensor::Index< 'm', 3 > m
Mat globA
Global matrix.
Definition DispMap.hpp:168
VectorDouble locF
Local element rhs vector.
Definition DispMap.hpp:165
VectorDouble dispAtGaussPts
Values at integration Pts.
Definition DispMap.hpp:164
MoFEMErrorCode getParameters()
Definition DispMap.hpp:172
MatrixDouble locA
Local element matrix.
Definition DispMap.hpp:166
vector< vector< double > > main_vector
Definition DispMap.hpp:25
MoFEMErrorCode loadFileData()
Definition DispMap.hpp:30
double getDataForGivenCoordinate(const double x, const double y, vector< vector< double > > &main_vector, const double sCale, const double cUbe_size)
Definition DispMap.hpp:83
MoFEMErrorCode fromOptions()
Definition DispMap.hpp:58
DispMapFe(MoFEM::Interface &mField)
Definition DispMap.hpp:12
int getRule(int order)
Set integrate rule.
Definition DispMap.hpp:16
MoFEMErrorCode doWork(int row_side, EntityType row_type, DataForcesAndSourcesCore::EntData &row_data)
Definition DispMap.hpp:308
OpAssembleRhs(const string &row_field, CommonData &common_data)
Definition DispMap.hpp:303
MoFEMErrorCode doWork(int row_side, int col_side, EntityType row_type, EntityType col_type, DataForcesAndSourcesCore::EntData &row_data, DataForcesAndSourcesCore::EntData &col_data)
Definition DispMap.hpp:200
OpCalculateLhs(const std::string &row_field, const std::string &col_field, CommonData &common_data, DataFromFiles &data_from_files)
Definition DispMap.hpp:192
OpCalulatefRhoAtGaussPts(const string &row_field, CommonData &common_data, DataFromFiles &data_from_files)
Definition DispMap.hpp:262
MoFEMErrorCode doWork(int row_side, EntityType row_type, DataForcesAndSourcesCore::EntData &row_data)
Definition DispMap.hpp:268
bool sYmm
If true assume that matrix is symmetric structure.
Deprecated interface functions.
MatrixDouble & getCoordsAtGaussPts()
Gauss points and weight, matrix (nb. of points x 3)
@ OPROW
operator doWork function is executed on FE rows
@ OPROWCOL
operator doWork is executed on FE rows &columns
MatrixDouble & getGaussPts()
matrix of integration (Gauss) points for Volume Element