Salome HOME
typo-fix by Kunda
[tools/medcoupling.git] / doc / user / doxygen / doxfiles / examples / medcouplingexamplesother.doxy
1
2 \section ExamplesOther Other
3
4 \subsection ExamplesOtherInputOutput Input/Output
5
6 \subsubsection cpp_mcfielddouble_WriteVTK Writing fields in a VTK file
7
8 In this example we
9 - create an 2D mesh and 3 fields on it,
10 - use
11 \ref MEDCoupling::MEDCouplingFieldDouble::WriteVTK "WriteVTK()"
12 to write all the fields and the mesh to a VTK file.
13
14 \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_WriteVTK_1
15
16
17 \anchor BEGIN_PYTHON_ONLY
18 \subsubsection MEDLoaderExample2 Loading a mesh using basic API
19
20 Consider a mesh called "Example2" in file "file2.med"
21 containing MED_POLYHEDRA, MED_TETRA4, MED_QUAD8, MED_TRI6, MED_SEG2
22 and MED_POINT1. In this case you will have :
23
24 \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_8
25
26 To get 3D cells (MED_POLYHEDRA and MED_TETRA4) you should type :
27
28 \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_7
29
30 To get 2D cells (MED_TRI6 and MED_QUAD8) you should type :
31
32 \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_4
33
34 To get 1D cells (MED_SEG2) you should type :
35
36 \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_5
37
38 And finally for 0D cells (MED_POINT1) you will write :
39
40 \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_6
41
42 \anchor END_PYTHON_ONLY
43
44
45 \anchor BEGIN_PYTHON_ONLY
46 \subsubsection py_mcumesh_writefile_onemesh_basic Writing one mesh using basic API
47
48 To write one mesh \b myMesh with name \b "myMeshName" in a MED file \b "wfile1.med" the following code should be typed :
49
50 \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_1
51
52 With the previous code, if "wFile1.med" file exists the file is
53 crashed and will contain after the call only the contents of myMesh
54 instance.
55
56 If you want to append a mesh in "wFile1.med" you should type :
57
58 \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_2
59
60 With the previous code, if the "wFile1.med" has already a mesh called "myMeshName" an
61 INTERP_KERNEL::Exception will be thrown.
62 \anchor END_PYTHON_ONLY
63
64
65 \anchor BEGIN_CPP_ONLY
66 \subsubsection cpp_mcumesh_loadfile Loading a mesh using advanced API
67
68 \code
69
70 const char fileName[]=...;
71 const char meshName[]=...;
72 MEDFileUMesh *medmesh=MEDFileUMesh::New(fileName,meshName);
73 std::vector<int> nel=medmesh->getNonEmptyLevels();
74 if(nel.size()<1)
75   throw INTERP_KERNEL::Exception("The test is not good for my file ! Expecting a multi level mesh to play with !");
76 MEDCouplingUMesh *m0=medmesh->getMeshAtLevel(nel[1],false);
77 MEDCouplingUMesh *g1=medmesh->getGroup(nel[1],"mesh2",false);
78 DataArrayInt *dag1=medmesh->getGroupArr(nel[1],"mesh2",false);
79 MEDCouplingUMesh *g1bis=m0->buildPartOfMySelf(dag1->getConstPointer(),dag1->getConstPointer()+dag1->getNbOfElems());
80 g1bis->setName(dag1->getName());
81 if(!g1->isEqual(g1bis,1e-12))
82   throw INTERP_KERNEL::Exception("hmmmm :g1 and g1bis should be equal...");
83 //
84 dag1->decrRef();
85 g1->decrRef();
86 m0->decrRef();
87 medmesh->decrRef();
88
89 \endcode
90 \anchor END_CPP_ONLY
91
92 \anchor BEGIN_CPP_ONLY
93 \subsubsection cpp_mcumesh_writefile Writing a mesh using advanced API
94
95 \code
96
97 MEDCouplingUMesh *m=...; //m is a mesh with meshDim=2 spaceDim=2
98 MEDCouplingUMesh *m1=...; //m1 is a mesh with meshDim=1 spaceDim=2 same coords than m
99 MEDCouplingUMesh *m2=...; //m2 is a mesh with meshDim=0 spaceDim=2 same coords than m
100 MEDFileUMesh *mm=MEDFileUMesh::New();
101 mm->setName("mm");//name needed to be non empty
102 mm->setDescription("Description mm");
103 mm->setCoords(m1->getCoords());
104 mm->setMeshAtLevel(-1,m1,false);
105 mm->setMeshAtLevel(0,m,false);
106 mm->setMeshAtLevel(-2,m2,false);
107 DataArrayInt *g1=DataArrayInt::New();
108 g1->alloc(2,1);
109 g1->setName("G1");
110 const int val1[2]={1,3};
111 std::copy(val1,val1+2,g1->getPointer());
112 DataArrayInt *g2=DataArrayInt::New();
113 g2->alloc(3,1);
114 g2->setName("G2");
115 const int val2[3]={1,2,3};
116 std::copy(val2,val2+3,g2->getPointer());
117 //
118 std::vector<const DataArrayInt *> grps(2);
119 grps[0]=g1; grps[1]=g2;
120 mm->setGroupsAtLevel(0,grps,false);
121 //
122 g2->decrRef();
123 g1->decrRef();
124 //
125 mm->write(2);
126
127 \endcode
128 \anchor END_CPP_ONLY
129
130
131 \anchor BEGIN_PYTHON_ONLY
132 \subsubsection py_mcfield_loadfile_onetimestep_basic Reading a field at one time step using basic API
133 So to retrieve a field on 3D cell called "F1Cell" in example file
134 "file2.med" on a mesh "Example2" (\ref MEDLoaderExample2 "as defined here") on time
135 step defined by iteration number 2 and iteration 3 the request will be :
136
137 \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_12
138
139 To retrieve the same field (same iteration) on 2D cells only the call will be :
140
141 \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_13
142 \anchor END_PYTHON_ONLY
143
144
145 \anchor BEGIN_PYTHON_ONLY
146 \subsubsection py_mcfield_loadfile_alltimesteps_basic Reading a field at all time steps using basic API
147
148 It is typically recommended to use the following
149 code when you want to load all time steps of a field on cell "myField" lying on
150 same mesh "mesh1" in one shot :
151
152 \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_11
153
154 \anchor END_PYTHON_ONLY
155
156
157
158 \anchor BEGIN_PYTHON_ONLY
159 \subsubsection py_mcfield_loadfile_allentities Reading a field on all entities using advanced API
160
161 Let's read a field on all entity called \a fieldName lying on a mesh called \a meshName in a MED file called \a fname at a iteration defined on time step defined
162 by \a iteration and \a order.
163
164 \snippet MEDLoaderExamplesTest.py PySnippetReadFieldOnAllEntity1_1
165
166 To read it there are 3 main approaches :
167
168 - Use MEDCoupling::MEDFileField1TS class :
169
170 \snippet MEDLoaderExamplesTest.py PySnippetReadFieldOnAllEntity1_3
171
172 - Use MEDCoupling::MEDFileFieldMultiTS class :
173
174 \snippet MEDLoaderExamplesTest.py PySnippetReadFieldOnAllEntity1_4
175
176 - Use iteration MEDCoupling::MEDFileFieldMultiTS class :
177
178 \snippet MEDLoaderExamplesTest.py PySnippetReadFieldOnAllEntity1_5
179
180 \anchor END_PYTHON_ONLY
181
182
183
184 \anchor BEGIN_PYTHON_ONLY
185 \subsubsection py_mcfield_loadfile_partial Reading a partial field using advanced API
186
187 Let's read a partial field called \a fieldName lying on a mesh called \a meshName in a MED file called \a fname at a iteration defined on time step defined
188 by \a iteration and \a order.
189
190 \snippet MEDLoaderExamplesTest.py PySnippetReadFieldPartial1_1
191
192 Fields defined partially on a meshes can been read using 2 main approaches :
193
194 - Either the user wants to retrieve it's field in %MEDCoupling sense, that is to say for interpolation, to evaluate such field on different points...
195 \n In this mode the link with the whole mesh is not useful for the user
196
197 \snippet MEDLoaderExamplesTest.py PySnippetReadFieldPartial1_3
198
199 - Or the user wants to retrieve the binding (cell ids or node ids) with the whole mesh on which the partial field lies partially on.
200
201 \snippet MEDLoaderExamplesTest.py PySnippetReadFieldPartial1_4
202
203 \ref medcoupling "MEDCoupling" allows to make bridges between the approaches. For example \a pfl \ref MEDCoupling::DataArrayInt "DataArrayInt instance" retrieved directly
204 from the file in the second approach can be retrieved starting from first approach.
205
206 Starting from mesh \a firstApproachMesh of read field in first approach \a fread, with the whole mesh \a wholeMesh the profile \a pflComputed can be computed :
207
208 \snippet MEDLoaderExamplesTest.py PySnippetReadFieldPartial1_5
209
210 Inversely, it is possible to rebuild field obtained in first approach starting from second approach :
211
212 \snippet MEDLoaderExamplesTest.py PySnippetReadFieldPartial1_6
213
214 \anchor END_PYTHON_ONLY
215
216
217 \anchor BEGIN_PYTHON_ONLY
218 \subsubsection py_mcfield_writefile_severaltimesteps_basic Writing several time steps of a field using basic API
219
220 To write a serie of time steps in a "file3.med" file lying on the same
221 unstructured mesh the typical code
222 to write is the following :
223
224 \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_3
225
226 In the above code, it is important to note that the values of pairs
227 (iteration,order) should be different between two calls to avoid that
228 a call to MEDLoader::WriteFieldUsingAlreadyWrittenMesh overwrites a
229 previous call.
230 Another important thing is the fact that \c f.getMesh() is not modified.
231 This write method presents the big advantage to be fast, because
232 neither check nor read is performed. That's why the parameter of \b writeFromScratch
233 is not needed here, contrary
234 to other MEDLoader::Write* methods.
235
236 \anchor END_PYTHON_ONLY
237
238
239 \anchor BEGIN_PYTHON_ONLY
240 \subsubsection py_mcfield_writefile_allentities Writing a field defined on all entities using advanced API
241
242 Let's write a cell field on all entities called \a fieldName lying on a mesh called \a meshName in a MED file called \a fname at a iteration defined on time step defined
243 by \a iteration and \a order.
244
245 \snippet MEDLoaderExamplesTest.py PySnippetWriteFieldOnAllEntity1_2
246
247 We can see here that the necessity to deal with both mesh and field to write a field is exposed by the API. The mesh write mode is 2 to tell to MED file that is file already exists to scratch it.
248 The mode of write is 0 to simply add to the file the field specific part.
249
250 \anchor END_PYTHON_ONLY
251
252
253 \anchor BEGIN_PYTHON_ONLY
254 \subsubsection py_mcfield_writefile_partial Writing a partial field using advanced API
255
256 \snippet MEDLoaderExamplesTest.py PySnippetWriteFieldPartial1_2
257
258 To write a partial field \a f can have a **null mesh**, because the link with mesh is made given the entry of \a mm MEDFileField1TS::setFieldProfile method.
259
260 \anchor END_PYTHON_ONLY