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