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