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