2 # Copyright (C) 2011-2016 CEA/DEN, EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # This simple use case illustrates the basic usage of MEDCoupling and
22 # MEDLoader to create a cartesian mesh, define a field on this mesh,
23 # and save all the stuff in a med file.
24 # (gboulant - 27/06/2011)
26 import MEDCoupling as MC
27 import MEDLoader as ML
30 # ===============================================================
31 # Creating a 512x512 cartesian mesh
32 # ===============================================================
34 # The size is the number of discrete values in a direction, and then
35 # corresponds to the number of cells in that direction.
39 # The mesh is created using MEDCoupling. The code below creates a
40 # cartesian mesh as a sizexsize grid
43 # WARNING: remember the problem of tics and spaces. The data values
44 # are considered as values defined on cells. With size values in a
45 # direction, we have to create size+1 mesh nodes in that direction.
48 cmesh=MC.MEDCouplingCMesh.New();
49 cmesh.setName("512x512 cartesian mesh")
54 arrX = [float(i * stepX) for i in range(nbNodesX)]
55 print("Size of arrX = %d"%len(arrX))
57 coordsX=MC.DataArrayDouble.New()
58 coordsX.setValues(arrX,nbNodesX,1)
63 arrY=[float(i * stepY) for i in range(nbNodesY)]
64 coordsY=MC.DataArrayDouble.New()
65 coordsY.setValues(arrY,sizeY,1)
67 cmesh.setCoords(coordsX,coordsY)
68 print(cmesh.getSpaceDimension())
71 # WARN: In the current state of development of MEDLoader, only
72 # unstructured meshes are supported for writing function in med
73 # files. We just have to convert the cartesian mesh in an unstructured
74 # mesh before creating the field.
75 umesh=cmesh.buildUnstructured();
76 umesh.setName("512x512 unstructured mesh")
78 # This can be used to save the mesh only (can be visualize using
80 meshFileName = "umesh.med"
81 ML.MEDLoader.WriteUMesh(meshFileName,umesh,True);
83 # Alternatively, you can use a MEDFileMesh to write the mesh in a
85 medFileCMesh = ML.MEDFileCMesh.New()
86 medFileCMesh.setMesh(cmesh)
87 medFileCMesh.setName(cmesh.getName())
88 meshFileName = "cmesh.med"
90 medFileCMesh.write(meshFileName,mode)
93 # ===============================================================
94 # Creating a scalar field on the 512x512 mesh
95 # ===============================================================
97 # For the simple test, we create a field that varies in space as
98 # field(x,y)=x+y where x and y are coordinates on the mesh
103 field = MC.MEDCouplingFieldDouble.New(MC.ON_CELLS);
104 field.setName("AnalyticField_onCells");
105 field.setMesh(umesh);
107 nbComponents=1 # Only one single component for a scalar field
109 field.fillFromAnalytic(nbComponents,fillFunction);
111 # The MEDLoader can be used to save all the stuff in a med file. You
112 # just have to specify the field and the MEDLoader will save the
114 createFromScratch=True
115 ML.MEDLoader.WriteField("fieldtest.med",field,createFromScratch)
119 field = MC.MEDCouplingFieldDouble.New(MC.ON_NODES);
120 field.setName("AnalyticField_onNodes");
121 field.setMesh(umesh);
122 field.fillFromAnalytic(nbComponents,fillFunction);
123 createFromScratch=False
124 ML.MEDLoader.WriteField("fieldtest.med",field,createFromScratch)
128 # ===============================================================
129 # Creating a scalar field, working with numpy
130 # ===============================================================
133 # We start by creating a numpy matrix
136 for irow in range(sizeY):
137 row = numpy.arange(irow*sizeY,irow*sizeY+sizeX,dtype='float64')
140 marray = numpy.vstack(rows)
142 # Then, we can reshape the matrix in a 1D vector that concatenate all
144 data=marray.reshape(1,sizeX*sizeY)[0]
145 # Finally, we can create a simple list as required by the MEDCoupling
146 # DataArrayDouble. Note also the usage of float type because
147 # MEDCoupling works only with real numbers
150 # Create the field using the list obtained from the numpy array
151 fieldWithNumpy = MC.MEDCouplingFieldDouble.New(MC.ON_CELLS);
152 fieldWithNumpy.setName("Numpy Field");
153 fieldWithNumpy.setMesh(umesh);
156 dataArray=MC.DataArrayDouble.New();
157 dataArray.setValues(listdata,nbCells,nbComponents)
158 fieldWithNumpy.setArray(dataArray);
160 createFromScratch=False
161 ML.MEDLoader.WriteField("fieldtest.med",fieldWithNumpy,createFromScratch)