Salome HOME
medcoupling documentation modification
[tools/medcoupling.git] / doc / user / input / data_movement.rst
1 Data movement (mesh/field construction)
2 =======================================
3
4 Read Mesh from file
5 -------------------
6
7 To read a mesh from a MED file simply invoke
8
9 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
10    :start-after: UG_ReadMeshFromFile_1
11    :end-before:  UG_ReadMeshFromFile_1
12
13 If the file contains more than one mesh, the previous call will
14 return the first one.
15
16 You can access to a precise mesh by doing
17
18 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
19    :start-after: UG_ReadMeshFromFile_2
20    :end-before:  UG_ReadMeshFromFile_2
21
22 Read field from file
23 --------------------
24
25 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
26    :start-after: UG_ReadField_1
27    :end-before:  UG_ReadField_1
28
29 This command will succeed if there is exactly one field in
30 "file.med" and only one time step attached to it.
31
32 If there are more than one field in "file.med" you are expected to
33 specify the field name.
34
35 To know all fields in "file.med" either you read exception thrown or you can invoke
36
37 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
38    :start-after: UG_ReadField_2
39    :end-before:  UG_ReadField_2
40
41 When you have the fieldName you can safely invoke.
42
43 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
44    :start-after: UG_ReadField_3
45    :end-before:  UG_ReadField_3
46
47 This command will succeed if there are exactly one time step
48 attached on it. If no you are expected to specify the time step
49 attached on it.
50
51 A time step is identified by two piece of information :
52
53 - pair of integers specifying without ambiguity a key for access
54 - floating point (physical time step)
55
56 To retrieve list of time step of a field invoke
57
58 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
59    :start-after: UG_ReadField_4
60    :end-before:  UG_ReadField_4
61
62 This method returns a list of triplet. The first 2 elements of
63 triplets is pair of integers of time step and the last element in
64 the triplet is the physical time step.
65
66 To read a field "Field1" at time step defined by pair "(ts0,ts1)"
67 you can invoke
68
69 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
70    :start-after: UG_ReadField_5
71    :end-before:  UG_ReadField_5
72
73 If you do not succeed reading field in "file.med" using this method
74 it means that your MED file is complex and requires more information
75 to be extracted. :ref:`Go to advanced reading<medcoupling_AdvancedReading>`.
76
77 .. admonition:: Remark
78
79    the method is concise but by calling this method several
80    times it leads to a several mesh loads.
81
82
83 .. _medcoupling_Write_mesh:
84    
85 Write mesh into file
86 --------------------
87
88 MED file format expects a mesh sorted by geometric type. You are
89 responsible to reorder, if needed, cells to match MED file format
90 requirements.
91
92 This responsability is let to the end user to avoid misrenumbering effect.
93
94 You can check this by invoking:
95
96 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
97    :start-after: UG_ReadMeshFromFile_3
98    :end-before:  UG_ReadMeshFromFile_3
99
100 To reorder cells you are encouraged to read :ref:`this <renumber_for_MED>`.
101
102 If *m* is well numbered, you can dump it into a file by doing :
103
104 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
105    :start-after: UG_ReadMeshFromFile_0
106    :end-before:  UG_ReadMeshFromFile_0
107
108 The last element specifies the behavior in case if "file2.med" would
109 already exist. True means, scratch it and write it from scratch.
110 False means do not scratch try to append it.
111
112
113 Write field into file
114 ---------------------
115
116 You are expected to have a field *f* with a mesh :ref:`correctly numbered.<medcoupling_Write_mesh>`
117
118 If *f* is a valid MEDCouplingFieldDouble you can dump it into a MED file by simply :
119
120 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
121    :start-after: UG_ReadField_0
122    :end-before:  UG_ReadField_0
123
124 The last element specifies the behavior in case if "file.med" would
125 already exist. The same meaning than for :ref:`writing mesh.<medcoupling_Write_mesh>`
126
127 The typical usecase is to store a multi time step field.
128
129 To do that, let's consider that *fs* store a list of
130 MEDCouplingFieldDouble with at least one element in it.
131
132 .. WARNING:: All meshes of elements in *fs* are expected to be the same
133
134 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
135    :start-after: UG_ReadField_6
136    :end-before:  UG_ReadField_6
137
138 .. admonition:: Remark
139
140    f.getTime()[1:3] returns the pair of integer that specifies the time step.
141
142 f.getTime()[1:3] should be different each other. If two
143 elements in *fs* have the same pair of integer time step key, the
144 second one will take the place of the first !
145
146 Create an array from scratch
147 ----------------------------
148
149 There are several simple ways to create a medcoupling array from a Python list.
150
151 The following call creates an array of double values consisting of 3 tuples with 2 components:
152
153 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
154    :start-after: UG_DataArrayDouble_0
155    :end-before:  UG_DataArrayDouble_0
156
157 The next call creates an array equivalent to one create above:
158                  
159 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
160    :start-after: UG_DataArrayDouble_1
161    :end-before:  UG_DataArrayDouble_1
162
163 The call below creates an array holding the same but differently arranged values: 2 tuples with 3 components:
164
165 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
166    :start-after: UG_DataArrayDouble_2
167    :end-before:  UG_DataArrayDouble_2
168
169 You can change number of components in *d* so that it holds 3 tuples with 2 components again:
170                  
171 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
172    :start-after: UG_DataArrayDouble_3
173    :end-before:  UG_DataArrayDouble_3
174
175 Arrays of different types (DataArrayInt, DataArrayFloat) can be created in the same way as DataArrayDouble:
176                  
177 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
178    :start-after: UG_DataArrayDouble_4
179    :end-before:  UG_DataArrayDouble_4
180
181 A medcoupling array can be created from a numpy array and can be transformed to a numpy array:
182
183 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest_numpy.py
184    :start-after: UG_DataArrayNumpy_0
185    :end-before:  UG_DataArrayNumpy_0
186
187
188
189 Create an unstructured mesh from scratch
190 ----------------------------------------
191
192 MEDCouplingUMesh
193 ^^^^^^^^^^^^^^^^
194
195 MEDCouplingUMesh class represents a general case unstructured mesh. Data of MEDCouplingUMesh is defined in several steps.
196
197 Firstly define basic mesh data in full interlace mode for coordinates and nodal connectivity cell per cell.
198
199 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
200    :start-after: PySnippetUMeshStdBuild1_1
201    :end-before:  PySnippetUMeshStdBuild1_1
202
203 Then create MEDCouplingUMesh instance giving its mesh dimension (2 here) and a name.
204
205 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
206    :start-after: PySnippetUMeshStdBuild1_2
207    :end-before:  PySnippetUMeshStdBuild1_2
208
209 Then add cells to the mesh. This step includes 
210
211 - giving an upper bound of the number of cells to be inserted into the unstructured mesh.
212 - entering nodal connectivity of all cells, cell per cell using MEDCouplingUMesh.insertNextCell method.
213 - compacting connectivity arrays by calling MEDCouplingUMesh.finishInsertingCells method.
214
215 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
216    :start-after: PySnippetUMeshStdBuild1_3
217    :end-before:  PySnippetUMeshStdBuild1_3
218
219 As the connectivity of the mesh has been defined, let's set the coordinates using array *coords* defined above.
220
221 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
222    :start-after: PySnippetUMeshStdBuild1_4
223    :end-before:  PySnippetUMeshStdBuild1_4
224
225 Now the mesh is usable. To assure this, call
226
227 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
228    :start-after: PySnippetUMeshStdBuild1_5
229    :end-before:  PySnippetUMeshStdBuild1_5
230
231 MEDCoupling1SGTUMesh
232 ^^^^^^^^^^^^^^^^^^^^
233
234 MEDCoupling1SGTUMesh class represents an unstructured mesh composed of cells of same geometric type. It is more optimal due to this simplicity.
235
236 Basically a MEDCoupling1SGTUMesh is defined in the same way as MEDCouplingUMesh. A difference is that the geometric type of cells is specified at construction, and not specified later e.g. in insertNextCell method:
237
238 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
239    :start-after: GU_MEDCoupling1SGTUMesh_0
240    :end-before:  GU_MEDCoupling1SGTUMesh_0
241
242 MEDCoupling1DGTUMesh
243 ^^^^^^^^^^^^^^^^^^^^
244
245 MEDCoupling1DGTUMesh also represents an unstructured mesh composed of cells of same geometric type but it is specialized for cell of "dynamic" geometric type only: NORM_POLYHED and NORM_POLYG.
246
247 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
248    :start-after: GU_MEDCoupling1SGTUMesh_1
249    :end-before:  GU_MEDCoupling1SGTUMesh_1
250
251 When connectivity of a polyhedron is defined, facets are separated one from another by -1.
252
253 Create a cartesian mesh from scratch
254 ------------------------------------
255
256 We are going to build a 2D cartesian mesh, constituted from 9 nodes along X axis, and 7 nodes along Y axis.
257
258 Firstly define for each direction the discretization and build a DataArrayDouble on the corresponding direction.
259
260 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
261    :start-after: PySnippetCMeshStdBuild1_1
262    :end-before:  PySnippetCMeshStdBuild1_1
263
264 Then create MEDCoupling.MEDCouplingCMesh instance giving the 2 instances of DataArrayDouble defined above.
265
266 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
267    :start-after: PySnippetCMeshStdBuild1_2
268    :end-before:  PySnippetCMeshStdBuild1_2
269
270 The mesh is now usable.
271
272 .. figure:: ../images/cartesian.png
273    :align: center
274
275    A cartesian mesh created by the code above
276
277 Create a curvelinear mesh from scratch
278 --------------------------------------
279
280 First we create a curvelinear mesh and define its structure, for instance a 2-dimensional mesh with 2 nodes in one direction and 3 nodes in the other direction:
281
282 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
283    :start-after: UG_MEDCouplingCurveLinearMesh_0
284    :end-before:  UG_MEDCouplingCurveLinearMesh_0
285
286 Then define coordinates of 6 nodes in 2D space:
287
288 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
289    :start-after: UG_MEDCouplingCurveLinearMesh_1
290    :end-before:  UG_MEDCouplingCurveLinearMesh_1
291
292 The mesh is now usable. It's a good habit to assure this:
293
294 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
295    :start-after: UG_MEDCouplingCurveLinearMesh_2
296    :end-before:  UG_MEDCouplingCurveLinearMesh_2
297
298 .. figure:: ../images/curvelinear.png
299    :align: center
300
301    A curvelinear mesh created by the code above
302
303 .. _MEDCouplingFieldDoubleOnCells:
304
305 Create a field on cell from scratch
306 -----------------------------------
307
308 Assume we already have a mesh. We create a field on all cells of the mesh.
309
310 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
311    :start-after: UG_MEDCouplingFieldDouble_0
312    :end-before:  UG_MEDCouplingFieldDouble_0
313
314 ONE_TIME indicates that the field data relates to a single time step. Now define this time moment.
315
316 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
317    :start-after: UG_MEDCouplingFieldDouble_1
318    :end-before:  UG_MEDCouplingFieldDouble_1
319
320 Then define field values:
321
322 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
323    :start-after: UG_MEDCouplingFieldDouble_2
324    :end-before:  UG_MEDCouplingFieldDouble_2
325
326
327 Create a field on node from scratch
328 -----------------------------------
329
330 Assume we already have a mesh. We create a field on all nodes of the mesh. The procedure is same as for a :ref:`field on cells <MEDCouplingFieldDoubleOnCells>` except two points:
331
332 - Spatial discretization in the field constructor is ON_NODES
333 - Number of tuples in an array of values is equal to mesh.getNumberOfNodes()
334
335 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
336    :start-after: UG_MEDCouplingFieldDouble_3
337    :end-before:  UG_MEDCouplingFieldDouble_3
338
339
340 Create a field on Gauss points from scratch
341 -------------------------------------------
342
343 Assume we already have a 2D mesh consisting of triangle and quadrangle cells. We create a field on Gauss points. First, a field is constructed with use of ON_GAUSS_PT and its basic attributes are set:
344
345 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
346    :start-after: UG_MEDCouplingGaussPointField_0
347    :end-before:  UG_MEDCouplingGaussPointField_0
348
349 Now define localization of Gauss points on cells. In this example, we define two Gauss points on triangle cells and four Gauss points on quadrangle cells. Localization of Gauss points is defined by three lists of float values:
350
351 #. Coordinates of nodes of the reference cell.
352 #. Coordinates of Gauss points on the reference cell.
353 #. Weights of Gauss points.
354
355 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
356    :start-after: UG_MEDCouplingGaussPointField_1
357    :end-before:  UG_MEDCouplingGaussPointField_1
358
359 Finally set field values:
360
361 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
362    :start-after: UG_MEDCouplingGaussPointField_2
363    :end-before:  UG_MEDCouplingGaussPointField_2
364
365 Modify field values
366 -------------------
367
368 applyFunc method modifies all tuples of a field at once. It changes both values and number of components, only number of tuples remains the same. 
369
370 To set value *val* to all tuples of the field *f* and to make it have *nbComp* components, call:
371
372 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
373    :start-after: UG_MEDCouplingFieldDouble_5
374    :end-before:  UG_MEDCouplingFieldDouble_5
375
376 It is also possible to compute new values basing on current values. To do so you can specify a function by which a new value will be computed. For more info on supported expressions that can be used in the function, see `expressions supported`_.
377
378 .. _`expressions supported`: ../../developer/arrays.html#MEDCouplingArrayApplyFuncExpr
379
380 You can use some letters, for example "x", "y", "z" etc., to refer to current component values. For example, to transform a 3D vector field to its magnitude, call:
381
382 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
383    :start-after: UG_MEDCouplingFieldDouble_6
384    :end-before:  UG_MEDCouplingFieldDouble_6
385
386 This way a value resulting from the function evaluation is assigned to all components.
387
388 But you can have its own expression for each component within one function. For this purpose, there are predefined variable names (IVec, JVec, KVec, LVec etc) each dedicated to a certain component (IVec, to the component #0 etc). A factor of such a variable is added to the corresponding component only.
389
390 Using this feature, you can set a magnitude of a 3D vector as the fourth component and swap X and Y components by calling:
391
392 .. literalinclude:: ../../../src/MEDCoupling_Swig/UsersGuideExamplesTest.py
393    :start-after: UG_MEDCouplingFieldDouble_7
394    :end-before:  UG_MEDCouplingFieldDouble_7
395
396 Define groups and write mesh using advanced API
397 -----------------------------------------------
398
399 To get access to full power of MED file, for example to define groups of cells, it is 
400 necessary to use the advanced medcoupling API, namely class MEDFileUMesh_.
401
402 .. _MEDFileUMesh: ../../developer/classMEDCoupling_1_1MEDFileUMesh.html
403
404 First of all we populate a MEDFileUMesh with meshes (MEDCouplingUMesh) of different dimensions, if present:
405
406 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
407    :start-after: UG_ReadMeshFromFile_4
408    :end-before:  UG_ReadMeshFromFile_4
409
410 Level must be 0 for a mesh of highest dimension, -1 for a mesh of dimension a unit less than highest dimension etc.
411
412 If cells are not yet sorted by geometric type, we can pass True as the third argument of setMeshAtLevel to make them sorted:
413
414 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
415    :start-after: UG_ReadMeshFromFile_9
416    :end-before:  UG_ReadMeshFromFile_9
417
418 .. WARNING:: meshes of different dimension must share the same point coordinate array and have the same name
419
420 We can change point coordinates as soon as all meshes are added:
421
422 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
423    :start-after: UG_ReadMeshFromFile_5
424    :end-before:  UG_ReadMeshFromFile_5
425
426 To define groups we call, for example:
427
428 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
429    :start-after: UG_ReadMeshFromFile_6
430    :end-before:  UG_ReadMeshFromFile_6
431
432 The first argument of addGroup defines a type of group. 1 stands for nodes. 0,-1,-2 and -3 have the same meaning as the level in setMeshAtLevel method. Note that a name of DataArrayInt defines a group name.
433
434 It is possible to change name of a group or a family by calling:
435
436 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
437    :start-after: UG_ReadMeshFromFile_7
438    :end-before:  UG_ReadMeshFromFile_7
439
440 Finally we write all data added to *mm* to a file:
441
442 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
443    :start-after: UG_ReadMeshFromFile_8
444    :end-before:  UG_ReadMeshFromFile_8
445
446 The last argument defines behavior if a file exists. 2 means remove. 1 means append; if data with same ID present, an exception is thrown. 0 means overwrite data with same ID; that can lead to a file corruption.
447
448 .. _medcoupling_AdvancedReading:
449
450 Read/write fields using advanced API
451 ------------------------------------
452
453 Having *field* on *mesh* we can write it using MEDFileField1TS_ class, which is a part of advanced medcoupling API, as following:
454
455 .. _MEDFileField1TS: ../../developer/classMEDCoupling_1_1MEDFileField1TS.html
456
457 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
458    :start-after: UG_RWFieldAdv_0
459    :end-before:  UG_RWFieldAdv_0
460
461 The last argument of write method defines behavior if a file exists. 2 means remove. 1 means append; if data with same ID present, an exception is thrown. 0 means overwrite data with same ID; that can lead to a file corruption.
462
463 If there is a need to write a field lying only on a part of a mesh, the following code gives an example of this:
464
465 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
466    :start-after: UG_RWFieldAdv_1
467    :end-before:  UG_RWFieldAdv_1
468
469 *profile* defines entities on which *fieldPartial* lies. *level* defines entities the field lies on: 1 stands for nodes, 0 is for entities of dimension equal to the mesh dimension, -1 is for entities of dimension a unit less than the mesh dimension etc.
470
471 MEDFileField1TS also can be used to read a field:
472
473 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
474    :start-after: UG_RWFieldAdv_2
475    :end-before:  UG_RWFieldAdv_2
476
477 * field method can be used if field support is simple: one spatial discretization and one *level*.
478 * getFieldAtLevel method allows to choose spatial discretization and *level*.
479 * getFieldOnMeshAtLevel method allows to specify spatial discretization, *level* and mesh.
480
481 *level* of a field, if unknown, can be defined by calling:
482
483 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
484    :start-after: UG_RWFieldAdv_3
485    :end-before:  UG_RWFieldAdv_3
486
487 *maxDim* is the maximal absolute dimension of entities the field lies on. *maxRelDims* is a sequence returning the dimensions relative to the maximal absolute one.
488
489 To read/write fields including several time steps medcoupling provides MEDFileFieldMultiTS_ class. To write all time steps it is necessary just to append them to MEDFileFieldMultiTS:
490
491 .. _MEDFileFieldMultiTS: ../../developer/classMEDCoupling_1_1MEDFileFieldMultiTS.html
492
493 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
494    :start-after: UG_RWFieldAdv_4
495    :end-before:  UG_RWFieldAdv_4
496
497 To read a time step with a known *iteration* and *order* MEDFileField1TS can be used as shown above. To iterate through all time steps, use MEDFileFieldMultiTS as following:
498
499 .. literalinclude:: ../../../src/MEDLoader/Swig/UsersGuideExamplesTest.py
500    :start-after: UG_RWFieldAdv_5
501    :end-before:  UG_RWFieldAdv_5
502