\section ExamplesOther Other \subsection ExamplesOtherInputOutput Input/Output \subsubsection cpp_mcfielddouble_WriteVTK Writing fields in a VTK file In this example we - create an 2D mesh and 3 fields on it, - use \ref ParaMEDMEM::MEDCouplingFieldDouble::WriteVTK "WriteVTK()" to write all the fields and the mesh to a VTK file. \snippet MEDCouplingExamplesTest.cxx CppSnippet_MEDCouplingFieldDouble_WriteVTK_1 \anchor BEGIN_PYTHON_ONLY \subsubsection MEDLoaderExample2 Loading a mesh using basic API Consider a mesh called "Example2" in file "file2.med" containing MED_POLYHEDRA, MED_TETRA4, MED_QUAD8, MED_TRI6, MED_SEG2 and MED_POINT1. In this case you will have : \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_8 To get 3D cells (MED_POLYHEDRA and MED_TETRA4) you should type : \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_7 To get 2D cells (MED_TRI6 and MED_QUAD8) you should type : \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_4 To get 1D cells (MED_SEG2) you should type : \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_5 And finally for 0D cells (MED_POINT1) you will write : \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_6 \anchor END_PYTHON_ONLY \anchor BEGIN_PYTHON_ONLY \subsubsection py_mcumesh_writefile_onemesh_basic Writing one mesh using basic API To write one mesh \b myMesh with name \b "myMeshName" in a MED file \b "wfile1.med" the following code should be typed : \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_1 With the previous code, if "wFile1.med" file exists the file is crashed and will contain after the call only the contents of myMesh instance. If you want to append a mesh in "wFile1.med" you should type : \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_2 With the previous code, if the "wFile1.med" has already a mesh called "myMeshName" an INTERP_KERNEL::Exception will be thrown. \anchor END_PYTHON_ONLY \anchor BEGIN_CPP_ONLY \subsubsection cpp_mcumesh_loadfile Loading a mesh using advanced API \code const char fileName[]=...; const char meshName[]=...; MEDFileUMesh *medmesh=MEDFileUMesh::New(fileName,meshName); std::vector nel=medmesh->getNonEmptyLevels(); if(nel.size()<1) throw INTERP_KERNEL::Exception("The test is not good for my file ! Expecting a multi level mesh to play with !"); MEDCouplingUMesh *m0=medmesh->getMeshAtLevel(nel[1],false); MEDCouplingUMesh *g1=medmesh->getGroup(nel[1],"mesh2",false); DataArrayInt *dag1=medmesh->getGroupArr(nel[1],"mesh2",false); MEDCouplingUMesh *g1bis=m0->buildPartOfMySelf(dag1->getConstPointer(),dag1->getConstPointer()+dag1->getNbOfElems()); g1bis->setName(dag1->getName()); if(!g1->isEqual(g1bis,1e-12)) throw INTERP_KERNEL::Exception("hmmmm :g1 and g1bis should be equal..."); // dag1->decrRef(); g1->decrRef(); m0->decrRef(); medmesh->decrRef(); \endcode \anchor END_CPP_ONLY \anchor BEGIN_CPP_ONLY \subsubsection cpp_mcumesh_writefile Writing a mesh using advanced API \code MEDCouplingUMesh *m=...; //m is a mesh with meshDim=2 spaceDim=2 MEDCouplingUMesh *m1=...; //m1 is a mesh with meshDim=1 spaceDim=2 same coords than m MEDCouplingUMesh *m2=...; //m2 is a mesh with meshDim=0 spaceDim=2 same coords than m MEDFileUMesh *mm=MEDFileUMesh::New(); mm->setName("mm");//name needed to be non empty mm->setDescription("Description mm"); mm->setCoords(m1->getCoords()); mm->setMeshAtLevel(-1,m1,false); mm->setMeshAtLevel(0,m,false); mm->setMeshAtLevel(-2,m2,false); DataArrayInt *g1=DataArrayInt::New(); g1->alloc(2,1); g1->setName("G1"); const int val1[2]={1,3}; std::copy(val1,val1+2,g1->getPointer()); DataArrayInt *g2=DataArrayInt::New(); g2->alloc(3,1); g2->setName("G2"); const int val2[3]={1,2,3}; std::copy(val2,val2+3,g2->getPointer()); // std::vector grps(2); grps[0]=g1; grps[1]=g2; mm->setGroupsAtLevel(0,grps,false); // g2->decrRef(); g1->decrRef(); // mm->write(2); \endcode \anchor END_CPP_ONLY \anchor BEGIN_PYTHON_ONLY \subsubsection py_mcfield_loadfile_onetimestep_basic Reading a field at one time step using basic API So to retrieve a field on 3D cell called "F1Cell" in example file "file2.med" on a mesh "Example2" (\ref MEDLoaderExample2 "as defined here") on time step defined by iteration number 2 and iteration 3 the request will be : \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_12 To retrieve the same field (same iteration) on 2D cells only the call will be : \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_13 \anchor END_PYTHON_ONLY \anchor BEGIN_PYTHON_ONLY \subsubsection py_mcfield_loadfile_alltimesteps_basic Reading a field at all time steps using basic API It is typically recommended to use the following code when you want to load all time steps of a field on cell "myField" lying on same mesh "mesh1" in one shot : \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_11 \anchor END_PYTHON_ONLY \anchor BEGIN_PYTHON_ONLY \subsubsection py_mcfield_loadfile_allentities Reading a field on all entities using advanced API 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 by \a iteration and \a order. \snippet MEDLoaderExamplesTest.py PySnippetReadFieldOnAllEntity1_1 To read it there are 3 main approaches : - Use ParaMEDMEM::MEDFileField1TS class : \snippet MEDLoaderExamplesTest.py PySnippetReadFieldOnAllEntity1_3 - Use ParaMEDMEM::MEDFileFieldMultiTS class : \snippet MEDLoaderExamplesTest.py PySnippetReadFieldOnAllEntity1_4 - Use iteration ParaMEDMEM::MEDFileFieldMultiTS class : \snippet MEDLoaderExamplesTest.py PySnippetReadFieldOnAllEntity1_5 \anchor END_PYTHON_ONLY \anchor BEGIN_PYTHON_ONLY \subsubsection py_mcfield_loadfile_partial Reading a partial field using advanced API 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 by \a iteration and \a order. \snippet MEDLoaderExamplesTest.py PySnippetReadFieldPartial1_1 Fields defined partially on a meshes can been read using 2 main approaches : - 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... \n In this mode the link with the whole mesh is not useful for the user \snippet MEDLoaderExamplesTest.py PySnippetReadFieldPartial1_3 - 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. \snippet MEDLoaderExamplesTest.py PySnippetReadFieldPartial1_4 \ref medcoupling "MEDCoupling" allows to make bridges between the approaches. For example \a pfl \ref ParaMEDMEM::DataArrayInt "DataArrayInt instance" retrieved directly from the file in the second approach can be retrieved starting from first approach. Starting from mesh \a firstApproachMesh of read field in first approach \a fread, whith the whole mesh \a wholeMesh the profile \a pflComputed can be computed : \snippet MEDLoaderExamplesTest.py PySnippetReadFieldPartial1_5 Inversely, it is possible to rebuild field obtained in first approach starting from second approach : \snippet MEDLoaderExamplesTest.py PySnippetReadFieldPartial1_6 \anchor END_PYTHON_ONLY \anchor BEGIN_PYTHON_ONLY \subsubsection py_mcfield_writefile_severaltimesteps_basic Writing several time steps of a field using basic API To write a serie of time steps in a "file3.med" file lying on the same unstructured mesh the typical code to write is the following : \snippet MEDLoaderExamplesTest.py PySnippetMeshAdvAPI1_3 In the above code, it is important to note that the values of pairs (iteration,order) should be different between two calls to avoid that a call to MEDLoader::WriteFieldUsingAlreadyWrittenMesh overwrites a previous call. Another important thing is the fact that \c f.getMesh() is not modified. This write method presents the big advantage to be fast, because neither check nor read is performed. That's why the parameter of \b writeFromScratch is not needed here, contrary to other MEDLoader::Write* methods. \anchor END_PYTHON_ONLY \anchor BEGIN_PYTHON_ONLY \subsubsection py_mcfield_writefile_allentities Writing a field defined on all entities using advanced API 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 by \a iteration and \a order. \snippet MEDLoaderExamplesTest.py PySnippetWriteFieldOnAllEntity1_2 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. The mode of write is 0 to simply add to the file the field specific part. \anchor END_PYTHON_ONLY \anchor BEGIN_PYTHON_ONLY \subsubsection py_mcfield_writefile_partial Writing a partial field using advanced API \snippet MEDLoaderExamplesTest.py PySnippetWriteFieldPartial1_2 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. \anchor END_PYTHON_ONLY