Salome HOME
Typo-fix by Kunda
[modules/med.git] / doc / tut / medcoupling / testmed_simple.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2011-2016  CEA/DEN, EDF R&D
4 #
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.
9 #
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.
14 #
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
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
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)
26
27 import MEDCoupling as MC
28 import MEDLoader as ML
29
30 #
31 # ===============================================================
32 # Creating a 512x512 cartesian mesh
33 # ===============================================================
34 #
35 # The size is the number of discrete values in a direction, and then
36 # corresponds to the number of cells in that direction.
37 size=8
38 #size=512
39
40 # The mesh is created using MEDCoupling. The code below creates a
41 # cartesian mesh as a sizexsize grid
42
43 # >>>
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.
47 # <<<
48
49 cmesh=MC.MEDCouplingCMesh.New();
50 cmesh.setName("512x512 cartesian mesh")
51
52 sizeX = size
53 nbNodesX = sizeX+1
54 stepX = 0.1
55 arrX = [float(i * stepX) for i in range(nbNodesX)]
56 print "Size of arrX = %d"%len(arrX)
57
58 coordsX=MC.DataArrayDouble.New()
59 coordsX.setValues(arrX,nbNodesX,1)
60
61 sizeY = size
62 nbNodesY = sizeY+1
63 stepY = 0.1
64 arrY=[float(i * stepY) for i in range(nbNodesY)]
65 coordsY=MC.DataArrayDouble.New()
66 coordsY.setValues(arrY,sizeY,1)
67
68 cmesh.setCoords(coordsX,coordsY)
69 print cmesh.getSpaceDimension()
70 #print cmesh
71
72 # WARN: In the current state of development of MEDLoader, only
73 # unstructured meshes are supported for writing 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")
78
79 # This can be used to save the mesh only (can be visualize using
80 # SMESH).
81 meshFileName = "umesh.med"
82 ML.MEDLoader.WriteUMesh(meshFileName,umesh,True);
83
84 # Alternatively, you can use a MEDFileMesh to write the mesh in a
85 # file.
86 medFileCMesh = ML.MEDFileCMesh.New()
87 medFileCMesh.setMesh(cmesh)
88 medFileCMesh.setName(cmesh.getName())
89 meshFileName = "cmesh.med"
90 mode = 2
91 medFileCMesh.write(meshFileName,mode)
92
93 #
94 # ===============================================================
95 # Creating a scalar field on the 512x512 mesh
96 # ===============================================================
97 #
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
100
101 # --- Field on cells
102
103 # Create the field
104 field = MC.MEDCouplingFieldDouble.New(MC.ON_CELLS);
105 field.setName("AnalyticField_onCells");
106 field.setMesh(umesh);
107
108 nbComponents=1 # Only one single component for a scalar field
109 fillFunction="x+y"
110 field.fillFromAnalytic(nbComponents,fillFunction);
111
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
114 # underlying mesh.
115 createFromScratch=True
116 ML.MEDLoader.WriteField("fieldtest.med",field,createFromScratch)
117
118 # --- Field on nodes
119
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)
126
127
128 #
129 # ===============================================================
130 # Creating a scalar field, working with numpy
131 # ===============================================================
132 #
133
134 # We start by creating a numpy matrix
135 import numpy
136 rows=[]
137 for irow in range(sizeY):
138     row = numpy.arange(irow*sizeY,irow*sizeY+sizeX,dtype='float64')
139     rows.append(row)
140
141 marray = numpy.vstack(rows)
142
143 # Then, we can reshape the matrix in a 1D vector that concatenate all
144 # the rows
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
149 listdata=list(data)
150
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);
155
156 nbCells=sizeX*sizeY
157 dataArray=MC.DataArrayDouble.New();
158 dataArray.setValues(listdata,nbCells,nbComponents)
159 fieldWithNumpy.setArray(dataArray);
160
161 createFromScratch=False
162 ML.MEDLoader.WriteField("fieldtest.med",fieldWithNumpy,createFromScratch)
163
164