Salome HOME
Correction of bug EDF10723 for 0D mesh.
[modules/med.git] / doc / doxygen / doxfiles / examples / medcouplingexamplesfields.doxy
1
2 \section ExamplesFields Fields
3
4 \subsection ExamplesFieldsCreate Create
5
6 \subsubsection medcouplingcppexamplesFieldDoubleBuild1 Standard build of a tensor field on cells with no time attached
7
8 \snippet MEDCouplingExamplesTest.cxx CppSnippetFieldDoubleBuild1_1
9
10
11 \subsubsection medcouplingcppexamplesFieldDoubleBuild2 Standard build of a scalar field on nodes with no time attached
12
13 \snippet MEDCouplingExamplesTest.cxx CppSnippetFieldDoubleBuild2_1
14
15
16 \subsubsection medcouplingcppexamplesFieldDoubleBuild3 Standard build of a vector field on cells with one time attached and no time interval
17
18 \snippet MEDCouplingExamplesTest.cxx CppSnippetFieldDoubleBuild3_1
19
20
21 \subsubsection medcouplingcppexamplesFieldDoubleBuild4 Standard build of a vector field on nodes defined on a time interval with a constant value during this interval
22
23 \snippet MEDCouplingExamplesTest.cxx CppSnippetFieldDoubleBuild4_1
24
25
26 \subsubsection cpp_mcfielddouble_MaxFields Getting maximal and minimal fields
27
28 In this example we
29 - create two fields with two tuples per two components,
30 - use
31 \ref ParaMEDMEM::MEDCouplingFieldDouble::MaxFields "MaxFields()"
32 to get a field holding maximal values of the two fields.
33 - use
34 \ref ParaMEDMEM::MEDCouplingFieldDouble::MinFields "MinFields()"
35 to get a field holding minimal values of the two fields.
36
37 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_MaxFields_1
38
39
40 \subsubsection cpp_mcfielddouble_MergeFields Concatenating fields
41
42 In this example we
43 - create an 1D mesh and a field on it,
44 - make a deep copy of the mesh and the field,
45 - translate the mesh and the field,
46 - use two variants of
47 \ref ParaMEDMEM::MEDCouplingFieldDouble::MergeFields "MergeFields()"
48 to create one field from the two by concatenating them and their meshes.
49
50 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_MergeFields_1
51 The result field is twice "longer" than \b field1.
52
53
54 \subsubsection cpp_mcfielddouble_buildNewTimeReprFromThis Getting a field copy with different time discretization
55
56 First, we create a supporting 2D mesh and a field on it got using
57 \ref ParaMEDMEM::MEDCouplingFieldDouble::fillFromAnalytic "fillFromAnalytic()".
58 \ref MEDCouplingTemporalDisc "Time discretization" of this field is
59 \ref ParaMEDMEM::ONE_TIME "ONE_TIME".
60 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_buildNewTimeReprFromThis_1
61 Now we use
62 \ref ParaMEDMEM::MEDCouplingFieldDouble::buildNewTimeReprFromThis "buildNewTimeReprFromThis()"
63 to get a copy of \b field1 whose time discretization is
64 \ref ParaMEDMEM::NO_TIME "NO_TIME".
65 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_buildNewTimeReprFromThis_2
66
67
68 \subsubsection cpp_mcmesh_fillFromAnalytic3 Creating a field using an expression
69
70 First, we create a 2D Cartesian mesh constituted by 2 cells.
71 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingMesh_fillFromAnalytic3_1
72 Now we use
73 \ref ParaMEDMEM::MEDCouplingMesh::fillFromAnalytic3 "fillFromAnalytic3()"
74 to get a \ref ParaMEDMEM::MEDCouplingFieldDouble "MEDCouplingFieldDouble" on cells filled
75 with values computed using an expression \b func. This expression is applied to coordinates of
76 each point (barycenter) for which the field value is computed. We want to get the
77 field on cells, with 3 components computed as follows. (In \b func, we refer to the
78 first component of a point using the variable "a", and to the second component, using
79 the variable "b").
80 - Component #0 = the second coordinate of the point; hence "IVec * b" in \b func.
81 - Component #1 = the first coordinate of the point; hence "JVec * a".
82 - Component #2 = distance between the point and SC origin (0.,0.); hence
83 "KVec * sqrt( a*a + b*b )".
84
85 In addition we want to add 10.0 to each component computed as described above, hence
86 "10" in \b func.
87 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingMesh_fillFromAnalytic3_2
88 Now we ascertain that the result field is as we expect. We check the second tuple of
89 the \b field. We get barycenter of the cell #1 and checks that values of the second
90 tuple are computed as we want.
91 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingMesh_fillFromAnalytic3_3
92
93
94 \subsubsection cpp_mcmesh_fillFromAnalytic2 Creating a field using an expression
95
96 First, we create a 2D Cartesian mesh constituted by 2 cells.
97 Note that we set names to coordinates arrays ("a" and "b" ) which will be used to refer to
98 corresponding coordinates within a function.
99 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingMesh_fillFromAnalytic2_1
100 Now we use
101 \ref ParaMEDMEM::MEDCouplingMesh::fillFromAnalytic2 "fillFromAnalytic2()"
102 to get a \ref ParaMEDMEM::MEDCouplingFieldDouble "MEDCouplingFieldDouble" on cells filled
103 with values computed using an expression \b func. This expression is applied to coordinates of
104 each point (barycenter) for which the field value is computed. We want to get the
105 field on cells, with 3 components computed as follows. (In \b func, we refer to the
106 first component of a point using the variable "a", and to the second component, using
107 the variable "b").
108 - Component #0 = the second coordinate of the point; hence "IVec * b" in \b func.
109 - Component #1 = the first coordinate of the point; hence "JVec * a".
110 - Component #2 = distance between the point and SC origin (0.,0.); hence
111 "KVec * sqrt( a*a + b*b )".
112
113 In addition we want to add 10.0 to each component computed as described above, hence
114 "10" in \b func.
115 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingMesh_fillFromAnalytic2_2
116 Now we ascertain that the result field is as we expect. We check the second tuple of
117 the \b field. We get barycenter of the cell #1 and checks that values of the second
118 tuple are computed as we want.
119 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingMesh_fillFromAnalytic2_3
120
121
122 \subsubsection cpp_mcmesh_fillFromAnalytic Creating a field using an expression
123
124 First, we create a 2D Cartesian mesh constituted by 2 cells.
125 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingMesh_fillFromAnalytic_1
126 Now we use
127 \ref ParaMEDMEM::MEDCouplingMesh::fillFromAnalytic "fillFromAnalytic()"
128 to get a \ref ParaMEDMEM::MEDCouplingFieldDouble "MEDCouplingFieldDouble" on cells filled
129 with values computed using an expression \b func. This expression is applied to coordinates of
130 each point (barycenter) for which the field value is computed. We want to get the
131 field on cells, with 3 components computed as follows. (In \b func, we refer to the
132 first component of a point using the variable "a", and to the second component, using
133 the variable "b").
134 - Component #0 = the second coordinate of the point; hence "IVec * b" in \b func.
135 - Component #1 = the first coordinate of the point; hence "JVec * a".
136 - Component #2 = distance between the point and SC origin (0.,0.); hence
137 "KVec * sqrt( a*a + b*b )".
138
139 In addition we want to add 10.0 to each component computed as described above, hence
140 "10" in \b func.
141 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingMesh_fillFromAnalytic_2
142 Now we ascertain that the result field is as we expect. We check the second tuple of
143 the \b field. We get barycenter of the cell #1 and checks that values of the second
144 tuple are computed as we want.
145 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingMesh_fillFromAnalytic_3
146
147
148 \subsubsection cpp_mcfielddouble_subpart1 Creation of a sub part of a field
149
150 <br><h3>Creation of a sub part of a field on cells</h3>
151 \snippet MEDCouplingExamplesTest.cxx CppSnippetFieldDoubleBuildSubPart1_1
152 The field on cells \b f1 lies on a mesh containing 5 cells and 9 nodes.
153 So this field \b f1 contains 5 tuples of 2 components each (10 values).
154 Now let's create a subfield on cells \b f2 from \b f1.
155 \snippet MEDCouplingExamplesTest.cxx CppSnippetFieldDoubleBuildSubPart1_2
156
157 \b f1 is a field on cells, \ref ParaMEDMEM::MEDCouplingFieldDouble::buildSubPart "buildSubPart" method performs an extraction on cells too.
158
159 So the array \b part1 lists ids on cells.
160
161 - cell #0 of \b f2 is the same cell of cell #2 of \b f1
162 - cell #1 of \b f2 is the same cell of cell #1 of \b f1
163 - cell #2 of \b f2 is the same cell of cell #4 of \b f1
164
165 So \b f2 contains 3 tuples with 2 components.
166
167 The underlying mesh of \b f2 contains a newly created mesh with 3 cells (not as \b mesh1 in \b f1) and 9 nodes (as \b mesh1 in \b f1).
168 \n For fields on cells the number of tuples of the returned field is always equal to the number of ids given in input (here \b part1).
169 \nOnly fields on cells have this particular behaviour.
170
171 <br><h3>Creation of a sub part of a field on nodes</h3>
172 \snippet MEDCouplingExamplesTest.cxx CppSnippetFieldDoubleBuildSubPart1_3
173 The field on nodes \b f1 lies on a mesh containing 5 cells and 9 nodes.
174 So this field \b f1 contains 9 tuples of 2 components each (18 values).
175 Now let's create a subfield on nodes \b f2 from \b f1.
176 \snippet MEDCouplingExamplesTest.cxx CppSnippetFieldDoubleBuildSubPart1_4
177
178 \b f1 is a field on nodes, but \ref ParaMEDMEM::MEDCouplingFieldDouble::buildSubPart "buildSubPart" method performs an extraction on \b cells.
179
180 After the call of \ref ParaMEDMEM::MEDCouplingFieldDouble::buildSubPart "buildSubPart" on node field \b f1, \b f1 will be reduced on a
181 submesh of \b mesh1 containing cells whose ids are in \b part2. So here the number of cells of \b f2 is 2 and the number of nodes is 4.
182 \nSo contrary to fields on cells, it is normal for fields on nodes that number of tuples of the returned field of \ref ParaMEDMEM::MEDCouplingFieldDouble::buildSubPart "buildSubPart"
183 method does not match the size of the input array (here \b part2).
184
185
186 \subsection ExamplesFieldsModify Modify
187
188 \subsubsection cpp_mcfielddouble_substractInPlaceDM Subtracting field on different meshes
189
190 We make two meshes in 1D space with no cells and 4 nodes. Nodes #0 and #2 are swapped
191 in the two meshes.<br>
192 And we make two fields on these meshes, so that fields values to equal to node
193 coordinates of the underlying meshes.
194 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_substractInPlaceDM_1
195 We are going to subtract \b field2 from \b field1, though they are on
196 different meshes.
197 \ref ParaMEDMEM::MEDCouplingFieldDouble::substractInPlaceDM "substractInPlaceDM()"
198 allows us doing this. We use a mesh comparison level \b levOfCheck = 10 that allows
199 subtracting fields on meshes with different node arrays.<br>
200 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_substractInPlaceDM_2
201 After applying
202 \ref ParaMEDMEM::MEDCouplingFieldDouble::substractInPlaceDM "substractInPlaceDM()"
203 the both fields lie on \b mesh2. As
204 \ref ParaMEDMEM::MEDCouplingFieldDouble::substractInPlaceDM "substractInPlaceDM()"
205 permutes values of \b field1 before value subtraction, and thus \b field1 becomes
206 equal to \b field2, hence their subtraction results in a zero field.
207 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_substractInPlaceDM_3
208
209
210 \subsubsection cpp_mcfielddouble_changeUnderlyingMesh Changing the underlying mesh
211
212 We make two meshes in 1D space with no cells and 4 nodes. Nodes #0 and #2 are swapped
213 in the two meshes.
214 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_changeUnderlyingMesh_1
215 We are going to use
216 \ref ParaMEDMEM::MEDCouplingFieldDouble::changeUnderlyingMesh "changeUnderlyingMesh()"
217 to set \b mesh2 instead of \b mesh1 as a support of a field. <br>
218 We use
219 \ref ParaMEDMEM::MEDCouplingMesh::fillFromAnalytic "fillFromAnalytic()"
220 to make a field on nodes of \b mesh1, so that its values to equal to node coordinates.
221 Then we use
222 \ref ParaMEDMEM::MEDCouplingFieldDouble::changeUnderlyingMesh "changeUnderlyingMesh()"
223 to change the underlying mesh of the \b field.
224 (We use a mesh comparison level \b levOfCheck = 10 that allows substituting meshes with
225 different node arrays.) As a result, we expect that values of the \b field are also
226 permuted same as nodes of the two meshes, and thus its values become equal to the
227 array \b coords2.
228 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_changeUnderlyingMesh_2
229
230
231 \subsubsection cpp_mcfielddouble_applyFunc_same_nb_comp Changing a field using an expression
232
233 We create a 2D vector field with 2 tuples and we want to transform this
234 field using an expression using
235 \ref ParaMEDMEM::MEDCouplingFieldDouble::applyFunc(const char *func) "applyFunc()".
236 The expression \b func is applied to each atomic value of the \b field. We want to change
237 the \b field as follows. (In \b func, we use the variable "v" to refer to an atomic field value).
238 - Component #0 = component #0 (remains the same); hence "IVec * v" in \b func.
239 - Component #1 = component #1 ^ 2; hence "JVec * v*v".
240
241 In addition we want to add 10.0 to each component computed as described above, hence
242 "10" in \b func.
243 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_applyFunc_same_nb_comp_1
244 Now we ascertain that the result field is as we expect.
245 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_applyFunc_same_nb_comp_2
246
247
248 \subsubsection cpp_mcfielddouble_applyFunc3 Changing a field using an expression
249
250 We create a 2D vector field with 2 values (vectors) and then we transform this
251 field into a 3D vector field by applying an expression to values of the 2D field
252 using
253 \ref ParaMEDMEM::MEDCouplingFieldDouble::applyFunc3() "applyFunc3()".
254 The expression \b func is applied to components of each vector of the \b field. We want
255 the \b field to have 3 components computed as follows. (In \b func, we refer to the
256 first component of a field value using the variable "a", and to the second component, using
257 the variable "b", as we define it by \b varNamesVec).
258 - Component #0 = the second vector component; hence "IVec * b" in \b func.
259 - Component #1 = the first vector component; hence "JVec * a".
260 - Component #2 = a vector magnitude; hence "KVec * sqrt( a*a + b*b )".
261
262 In addition we want to add 10.0 to each component computed as described above, hence
263 "10" in \b func.
264 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_applyFunc3_1
265 Now we ascertain that the result field is as we expect. We check the second vector of
266 the \b field.
267 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_applyFunc3_2
268
269
270 \subsubsection cpp_mcfielddouble_applyFunc2 Changing a field using an expression
271
272 We create a 2D vector field with 2 values (vectors) and then we transform this
273 field into a 3D vector field by applying an expression to values of the 2D field
274 using
275 \ref ParaMEDMEM::MEDCouplingFieldDouble::applyFunc2(int nbOfComp, const char *func) "applyFunc2()".
276 Note that we set component info the \b array ("a" and "b" ) which will be used to refer to
277 corresponding components within a function.
278 The expression \b func is applied to components of each vector of the \b field. We want
279 the \b field to have 3 components computed as follows. (In \b func, we refer to the
280 first component of a field value using the variable "a", and to the second component, using
281 the variable "b").
282 - Component #0 = the second vector component; hence "IVec * b" in \b func.
283 - Component #1 = the first vector component; hence "JVec * a".
284 - Component #2 = a vector magnitude; hence "KVec * sqrt( a*a + b*b )".
285
286 In addition we want to add 10.0 to each component computed as described above, hence
287 "10" in \b func.
288 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_applyFunc2_1
289 Now we ascertain that the result field is as we expect. We check the second vector of
290 the \b field.
291 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_applyFunc2_2
292
293
294 \subsubsection cpp_mcfielddouble_applyFunc Changing a field using an expression
295
296 We create a 2D vector field with 2 values (vectors) and then we transform this
297 field into a 3D vector field by applying an expression to values of the 2D field
298 using
299 \ref ParaMEDMEM::MEDCouplingFieldDouble::applyFunc(int nbOfComp, const char *func) "applyFunc()".
300 The expression \b func is applied to components of each vector of the \b field. We want
301 the \b field to have 3 components computed as follows. (In \b func, we refer to the
302 first component of a field value using the variable "a", and to the second component, using
303 the variable "b").
304 - Component #0 = the second vector component; hence "IVec * b" in \b func.
305 - Component #1 = the first vector component; hence "JVec * a".
306 - Component #2 = a vector magnitude; hence "KVec * sqrt( a*a + b*b )".
307
308 In addition we want to add 10.0 to each component computed as described above, hence
309 "10" in \b func.
310 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_applyFunc_1
311 Now we ascertain that the result field is as we expect. We check the second vector of
312 the \b field.
313 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_applyFunc_2
314
315
316 \subsubsection cpp_mcfielddouble_applyFunc_val Filling a field with a value
317
318 We want to transform a 2D vector field to a 3D vector field so that all values to be
319 equal to a certain value. First, we create the 2D mesh and the vector field on it.
320 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_applyFunc_val_1
321 Finally we use
322 \ref ParaMEDMEM::MEDCouplingFieldDouble::applyFunc(int nbOfComp, double val) "applyFunc()"
323 to change the number of components and all field values.
324 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_applyFunc_val_2
325 As a result, number of tuples in the field equals to the number of cells in the mesh,
326 and number of components becomes equal to 3 as required.
327
328
329 \subsubsection cpp_mcfielddouble_fillFromAnalytic3 Filling a field using an expression
330
331 First, we create a 2D Cartesian mesh constituted by 2 cells.
332 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_fillFromAnalytic3_1
333 Now we create a field on cells and use
334 \ref ParaMEDMEM::MEDCouplingFieldDouble::fillFromAnalytic2 "fillFromAnalytic2()"
335 to fill it
336 with values computed using an expression \b func. This expression is applied to coordinates of
337 each point (barycenter) for which the field value is computed. We want the \b field
338 to have 3 components computed as follows. (In \b func, we refer to the
339 first component of a point using the variable "a", and to the second component, using
340 the variable "b").
341 - Component #0 = the second coordinate of the point; hence "IVec * b" in \b func.
342 - Component #1 = the first coordinate of the point; hence "JVec * a".
343 - Component #2 = distance between the point and SC origin (0.,0.); hence
344 "KVec * sqrt( a*a + b*b )".
345
346 In addition we want to add 10.0 to each component computed as described above, hence
347 "10" in \b func.
348 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_fillFromAnalytic3_2
349 Now we ascertain that the result field is as we expect. We check the second tuple of
350 the \b field. We get barycenter of the cell #1 and checks that values of the second
351 tuple are computed as we want.
352 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_fillFromAnalytic3_3
353
354
355 \subsubsection cpp_mcfielddouble_fillFromAnalytic2 Filling a field using an expression
356
357 First, we create a 2D Cartesian mesh constituted by 2 cells.
358 Note that we set names to coordinates arrays ("a" and "b" ) which will be used to refer to
359 corresponding coordinates within a function.
360 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_fillFromAnalytic2_1
361 Now we create a field on cells and use
362 \ref ParaMEDMEM::MEDCouplingFieldDouble::fillFromAnalytic2 "fillFromAnalytic2()"
363 to fill it
364 with values computed using an expression \b func. This expression is applied to coordinates of
365 each point (barycenter) for which the field value is computed. We want the \b field
366 to have 3 components computed as follows. (In \b func, we refer to the
367 first component of a point using the variable "a", and to the second component, using
368 the variable "b").
369 - Component #0 = the second coordinate of the point; hence "IVec * b" in \b func.
370 - Component #1 = the first coordinate of the point; hence "JVec * a".
371 - Component #2 = distance between the point and SC origin (0.,0.); hence
372 "KVec * sqrt( a*a + b*b )".
373
374 In addition we want to add 10.0 to each component computed as described above, hence
375 "10" in \b func.
376 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_fillFromAnalytic2_2
377 Now we ascertain that the result field is as we expect. We check the second tuple of
378 the \b field. We get barycenter of the cell #1 and checks that values of the second
379 tuple are computed as we want.
380 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_fillFromAnalytic2_3
381
382
383 \subsubsection cpp_mcfielddouble_fillFromAnalytic Filling a field using an expression
384
385 First, we create a 2D Cartesian mesh constituted by 2 cells.
386 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_fillFromAnalytic_1
387 Now we create a field on cells and use
388 \ref ParaMEDMEM::MEDCouplingFieldDouble::fillFromAnalytic(int nbOfComp, const char *func) "fillFromAnalytic()"
389 to fill it
390 with values computed using an expression \b func. This expression is applied to coordinates of
391 each point (barycenter) for which the field value is computed. We want the \b field to have
392  3 components computed as follows. (In \b func, we refer to the
393 first component of a point using the variable "a", and to the second component, using
394 the variable "b").
395 - Component #0 = the second coordinate of the point; hence "IVec * b" in \b func.
396 - Component #1 = the first coordinate of the point; hence "JVec * a".
397 - Component #2 = distance between the point and SC origin (0.,0.); hence
398 "KVec * sqrt( a*a + b*b )".
399
400 In addition we want to add 10.0 to each component computed as described above, hence
401 "10" in \b func.
402 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_fillFromAnalytic_2
403 Now we ascertain that the result field is as we expect. We check the second tuple of
404 the \b field. We get barycenter of the cell #1 to check that values of the second
405 tuple (#1) are computed as we want.
406 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_fillFromAnalytic_3
407
408
409 \subsubsection medcouplingcppexamplesFieldDoubleBuild5 Some operations that can be carried out on fields on cells
410
411 \snippet MEDCouplingExamplesTest.cxx CppSnippetFieldDoubleBuild1_2
412
413 \anchor BEGIN_CPP_ONLY
414 The decrementation of ref counter should be carried out in CPlusPlus only ...
415
416 \snippet MEDCouplingExamplesTest.cxx CppSnippetFieldDoubleBuild1_3
417 \anchor END_CPP_ONLY
418
419
420 \subsubsection cpp_mcfielddouble_renumberNodes Permuting a field on nodes
421
422 First, we create a supporting 2D mesh constituted by 4 cells. We create a 2x2
423 Cartesian mesh and then convert it to an unstructured one, since the Cartesian mesh
424 is not suitable for
425 \ref ParaMEDMEM::MEDCouplingFieldDouble::renumberNodes "renumberNodes()" as its
426  nature does not imply node renumbering.
427 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_renumberNodes_1
428 Then we create a field on nodes using
429 \ref ParaMEDMEM::MEDCouplingFieldDouble::fillFromAnalytic "fillFromAnalytic()",
430 such that its values to coincide with coordinates of field location points that are
431  nodes in our case (as our field is \ref ParaMEDMEM::ON_NODES "ON_NODES").
432 At last we ascertain that field values are equal to node coordinates.
433 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_renumberNodes_2
434 Now, we are going to reverse order of nodes using
435 \ref ParaMEDMEM::MEDCouplingFieldDouble::renumberNodes "renumberNodes()".
436 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_renumberNodes_3
437 As a result, the underlying mesh of \b field is changed and its nodes are also
438  renumbered.
439 And the field values are still equal to node coordinates of the renumbered \b mesh2.
440
441
442 \subsubsection cpp_mcfielddouble_renumberCells Permuting a field on cells
443
444 First, we create a supporting 2D mesh constituted by 4 cells. We create a 2x2
445 Cartesian mesh and then convert it to an unstructured one, since the Cartesian mesh
446 is not suitable for
447 \ref ParaMEDMEM::MEDCouplingFieldDouble::renumberCells "renumberCells()" as its
448  nature does not imply cell renumbering.
449 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_renumberCells_1
450 Then we create a field on cells using
451 \ref ParaMEDMEM::MEDCouplingFieldDouble::fillFromAnalytic "fillFromAnalytic()",
452 such that its values to coincide with coordinates of field location points that are
453  cell barycenters in our case (as our field is \ref ParaMEDMEM::ON_CELLS "ON_CELLS").
454 At last we ascertain that field values are equal to cell barycenters.
455 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_renumberCells_2
456 Now, we are going to reverse order of cells using
457 \ref ParaMEDMEM::MEDCouplingFieldDouble::renumberCells "renumberCells()".
458 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_renumberCells_3
459 As a result, the underlying mesh of \b field is changed and its cells are also
460  renumbered.
461 And the field values are still equal to cell barycenters of the renumbered \b mesh2.
462
463
464 \anchor BEGIN_CPP_ONLY
465 \subsubsection cpp_mcfielddouble_fillFromAnalytic_c_func Filling a field using a C function
466
467 We want to create a 3D vector field lying on a 2D mesh using a C function as a value
468 generator. For that, first, we define the function that computes 3 values basing on 2
469 coordinates of a 2D point.
470 \snippet MEDCouplingExamplesTest.cxx Snippet_MEDCouplingFieldDouble_fillFromAnalytic_c_func_0
471 Then we create the 2D mesh and the field on it, and finally we use
472 \ref ParaMEDMEM::MEDCouplingFieldDouble::fillFromAnalytic(int nbOfComp, FunctionToEvaluate func) "fillFromAnalytic()"
473 to fill the field with values each composed of 3 components.
474 \snippet MEDCouplingExamplesTest.cxx Snippet_MEDCouplingFieldDouble_fillFromAnalytic_c_func_1
475 As a result, number of tuples in the field equals to the number of cells in the mesh,
476 and number of components equals to 3 as required.
477 \anchor END_CPP_ONLY
478
479
480 \anchor BEGIN_CPP_ONLY
481 \subsubsection cpp_mcfielddouble_applyFunc_c_func Changing a field by applying a C function
482
483 We want to transform a 2D vector field to a 3D vector field by
484 applying a C function to each vector value. For that, first, we define the function
485 that computes 3 values basing on 2 components of a 2D vector.
486 \snippet MEDCouplingExamplesTest.cxx Snippet_MEDCouplingFieldDouble_fillFromAnalytic_c_func_0
487 Then we create the 2D mesh and the vector field on it.
488 \snippet MEDCouplingExamplesTest.cxx Snippet_MEDCouplingFieldDouble_applyFunc_c_func_1
489 Finally we use
490 \ref ParaMEDMEM::MEDCouplingFieldDouble::applyFunc(int nbOfComp, FunctionToEvaluate func) "applyFunc()"
491 to change the field values.
492 \snippet MEDCouplingExamplesTest.cxx Snippet_MEDCouplingFieldDouble_applyFunc_c_func_2
493 As a result, number of tuples in the field equals to the number of cells in the mesh,
494 and number of components becomes equal to 3 as required.
495 \anchor END_CPP_ONLY
496
497
498 \anchor BEGIN_CPP_ONLY
499
500 \subsubsection cpp_mcfield_remapper_highlevel Using interpolation tools - High level usage
501
502 \code
503 ...
504 const char sourceFileName[]="source.med";
505 MEDCouplingFieldDouble *sourceField=MEDLoader::ReadFieldCell(sourceFileName,"Source_Mesh",0,"Density",/*iteration*/0,/*order*/0);
506 const char targetFileName[]="target.med";
507 MEDCouplingUMesh *med_target_mesh=MEDLoader::ReadUMeshFromFile(targetFileName,"Target_Mesh",0);
508 //
509 sourceField->setNature(ConservativeVolumic);//Specify which formula to use in case of non overlapping meshes
510 MEDCouplingRemapper remapper;
511 remapper.setPrecision(1e-12);
512 remapper.setIntersectionType(INTERP_KERNEL::Triangulation);
513 remapper.prepare(sourceField->getMesh(),med_target_mesh,"P0P0");
514 MEDCouplingFieldDouble *targetField=remapper.transferField(sourceField,/*default_value*/4.57);//Any target cell not intercepted by any source cell will have value set to 4.57.
515 ...
516 // clean-up
517 targetField->decrRef();
518 sourceField->decrRef();
519 med_target_mesh->decrRef();
520 \endcode
521
522 \anchor END_CPP_ONLY
523
524
525 \anchor BEGIN_CPP_ONLY
526
527 \subsubsection cpp_mcfield_remapper_middlelevel Using interpolation tools - Middle level usage
528
529 - The simplest way to use the interpolator with \ref medcoupling data struture is illustrated in the following example.
530
531 \code
532 ...
533 MEDCouplingUMesh *med_source_mesh=MEDLoader::ReadUMeshFromFile("source.med","Source_mesh",0);
534 MEDCouplingUMesh *med_target_mesh=MEDLoader::ReadUMeshFromFile("target.med","Target_mesh",0);
535 MEDCouplingNormalizedUnstructuredMesh<2,2> wrap_source_mesh(med_source_mesh);
536 MEDCouplingNormalizedUnstructuredMesh<2,2> wrap_target_mesh(med_target_mesh);
537 // Go for interpolation...
538 INTERP_KERNEL::Interpolation2D myInterpolator;
539 myInterpolator.setPrecision(1e-7);
540 myInterpolator.setIntersectionType(INTERP_KERNEL::Geometric2D);
541 std::vector<std::map<int,double> > resultMatrix;
542 INTERP_KERNEL::Matrix<double,ALL_C_MODE> resultMatrix2;
543 // here the interpolation is performed twice for this code to illustrate the possibility of storing data the interpolation matrix in 2 different data structures.
544 myInterpolator.interpolateMeshes(wrap_source_mesh,wrap_target_mesh,resultMatrix,"P0P0");
545 myInterpolator.interpolateMeshes(wrap_source_mesh,wrap_target_mesh,resultMatrix2,"P0P0");
546 //Ok resultMatrix and resultMatrix2 contain matrix now
547 ...
548 \endcode
549
550
551 - Same with VTK data structure :
552
553 \code
554 ...
555 vtkXMLUnstructuredGridReader *readerSource=vtkXMLUnstructuredGridReader::New();
556 readerSource->SetFileName("source.vtu");
557 vtkUnstructuredGrid *vtk_source_mesh=readerSource->GetOutput();
558 readerSource->Update();
559 vtkXMLUnstructuredGridReader *readerTarget=vtkXMLUnstructuredGridReader::New();
560 readerTarget->SetFileName("target.vtu");
561 vtkUnstructuredGrid *vtk_target_mesh=readerTarget->GetOutput();
562 readerTarget->Update();
563 // Ok at this point we have our mesh in VTK format.
564 // Go to wrap vtk_source_mesh and vtk_target_mesh.
565 VTKNormalizedUnstructuredMesh<2> wrap_source_mesh(vtk_source_mesh);
566 VTKNormalizedUnstructuredMesh<2> wrap_target_mesh(vtk_target_mesh);
567 // Go for interpolation...
568 INTERP_KERNEL::Interpolation2D myInterpolator;
569 //optional call to parametrize your interpolation. First precision, tracelevel, intersector wanted.
570 myInterpolator.setOptions(1e-7,0,Geometric2D);
571 INTERP_KERNEL::Matrix<double,ALL_C_MODE> resultMatrix;
572 myInterpolator.interpolateMeshes(wrap_source_mesh,wrap_target_mesh,resultMatrix,"P0P0");
573 //Ok let's multiply resultMatrix by source field to interpolate to target field.
574 resultMatrix.multiply(...)
575 //clean-up
576 readerSource->Delete();
577 readerTarget->Delete();
578 ...
579 \endcode
580
581 \anchor END_CPP_ONLY
582
583
584 \subsection ExamplesFieldsAccess Access
585
586 \subsubsection cpp_mcfielddouble_getValueOn_time Getting a field value at some point at certain time
587
588 First, we create a supporting structured mesh. We create a 2x2 Cartesian mesh
589 constituted by 4 cells.
590 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_getValueOn_time_1
591 Then we create a scalar field on cells, whose values vary linearly in time.
592 We set all field values at a start time to be equal 10.0 using
593 \ref ParaMEDMEM::MEDCouplingFieldDouble::fillFromAnalytic "fillFromAnalytic()".
594 And we set all field values at an end time to be equal 20.0 by doubling the start
595 time array.
596 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_getValueOn_time_2
597 Now, we want to get a field value at a point [0,0] at a middle time between the start
598 and end times. We expect the returned value to be equal to an average of 10. and 20.
599 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_getValueOn_time_3
600
601
602 \subsubsection cpp_mcfielddouble_getValueOnMulti Getting field values at some points
603
604 First, we create a supporting structured mesh. We create a 2x2 Cartesian mesh
605 constituted by 4 cells. Then we create a scalar field on cells using
606 \ref ParaMEDMEM::MEDCouplingFieldDouble::fillFromAnalytic "fillFromAnalytic()".
607 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_getValueOnMulti_1
608 Now, we want to retrieve all field values using
609 \ref ParaMEDMEM::MEDCouplingFieldDouble::getValueOnMulti "getValueOnMulti()".
610 The field values relate to cells, hence we will use cell barycenters as a parameter of
611 \ref ParaMEDMEM::MEDCouplingFieldDouble::getValueOnMulti "getValueOnMulti()".
612 We expect that the double array returned
613 \ref ParaMEDMEM::MEDCouplingFieldDouble::getValueOnMulti "getValueOnMulti()"
614 is equal to that stored by \b field.
615 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_getValueOnMulti_2
616
617
618 \subsubsection cpp_mcfielddouble_getValueOn Getting a field value at a point
619
620 First, we create a supporting structured mesh. We create a 2x2 Cartesian mesh
621 constituted by 4 cells. Then we create a scalar field on cells using
622 \ref ParaMEDMEM::MEDCouplingFieldDouble::fillFromAnalytic "fillFromAnalytic()".
623 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_getValueOn_1
624 Now, we want to retrieve all field values using
625 \ref ParaMEDMEM::MEDCouplingFieldDouble::getValueOn "getValueOn()".
626 The field values relate to cells, hence we will use cell barycenters to get a field
627 value at each cell.
628 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_getValueOn_2
629 We collected all values returned by
630 \ref ParaMEDMEM::MEDCouplingFieldDouble::getValueOn "getValueOn()" in an array, so
631 that we can ascertain that the array of returned values is same as that stored by \b
632 field.
633
634
635 \subsubsection cpp_mcfielddouble_getValueOnPos Getting a value of field lying on a structured mesh
636
637 First, we create a supporting structured mesh. We create a 2x2 Cartesian mesh
638 constituted by 4 cells. Then we create a scalar field on cells using
639 \ref ParaMEDMEM::MEDCouplingFieldDouble::fillFromAnalytic "fillFromAnalytic()".
640 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_getValueOnPos_1
641 Now, we retrieve a field value relating to the cell #3 (this cell has a structured indexed
642 (1,1)). For that we use
643 \ref ParaMEDMEM::MEDCouplingFieldDouble::getValueOnPos "getValueOnPos()" where we
644 pass the structured indexed of the cell: 1,1,-1 (the last index is meaningless as the
645 mesh is 2D).
646 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_getValueOnPos_2
647 After all we ascertain that the returned value corresponds to the expression used for
648 the field creation. Namely that the value equals to the sum of components of
649 barycenter of cell #3.