Salome HOME
27b84a29be65e2791908b573aff16f6b4b095322
[modules/med.git] / src / MEDOP / tut / medcoupling / testmed_simple.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3
4 # This simple use case illustrates the basic usage of MEDCoupling and
5 # MEDLoader to create a cartesian mesh, define a field on this mesh,
6 # and save all the stuff in a med file.
7 # (gboulant - 27/06/2011)
8
9 import MEDCoupling as MC
10 import MEDLoader as ML
11
12 #
13 # ===============================================================
14 # Creating a 512x512 cartesian mesh
15 # ===============================================================
16 #
17 # The size is the number of discrete values in a direction, and then
18 # corresponds to the number of cells in that direction.
19 size=8
20 #size=512
21
22 # The mesh is created using MEDCoupling. The code below creates a
23 # cartesian mesh as a sizexsize grid
24
25 # >>>
26 # WARNING: remember the problem of tics and spaces. The data values
27 # are considered as values defined on cells. With size values in a
28 # direction, we have to create size+1 mesh nodes in that direction.
29 # <<<
30
31 cmesh=MC.MEDCouplingCMesh.New();
32 cmesh.setName("512x512 cartesian mesh")
33
34 sizeX = size
35 nbNodesX = sizeX+1
36 stepX = 0.1
37 arrX = [float(i * stepX) for i in range(nbNodesX)]
38 print "Size of arrX = %d"%len(arrX)
39
40 coordsX=MC.DataArrayDouble.New()
41 coordsX.setValues(arrX,nbNodesX,1)
42
43 sizeY = size
44 nbNodesY = sizeY+1
45 stepY = 0.1
46 arrY=[float(i * stepY) for i in range(nbNodesY)]
47 coordsY=MC.DataArrayDouble.New()
48 coordsY.setValues(arrY,sizeY,1)
49
50 cmesh.setCoords(coordsX,coordsY)
51 print cmesh.getSpaceDimension()
52 #print cmesh
53
54 # WARN: In the current state of development of MEDLoader, only
55 # unstructured meshes are supported for writting function in med
56 # files. We just have to convert the cartesian mesh in an unstructured
57 # mesh before creating the field.
58 umesh=cmesh.buildUnstructured();
59 umesh.setName("512x512 unstructured mesh")
60
61 # This can be used to save the mesh only (can be visualize using
62 # SMESH).
63 meshFileName = "umesh.med"
64 ML.MEDLoader.WriteUMesh(meshFileName,umesh,True);
65
66 # Alternatively, you can use a MEDFileMesh to write the mesh in a
67 # file.
68 medFileCMesh = ML.MEDFileCMesh.New()
69 medFileCMesh.setMesh(cmesh)
70 medFileCMesh.setName(cmesh.getName())
71 meshFileName = "cmesh.med"
72 mode = 2
73 medFileCMesh.write(meshFileName,mode)
74
75 #
76 # ===============================================================
77 # Creating a scalar field on the 512x512 mesh
78 # ===============================================================
79 #
80 # For the simple test, we create a field that varies in space as
81 # field(x,y)=x+y where x and y are coordinates on the mesh
82
83 # --- Field on cells
84
85 # Create the field
86 field = MC.MEDCouplingFieldDouble.New(MC.ON_CELLS);
87 field.setName("AnalyticField_onCells");
88 field.setMesh(umesh);
89
90 nbComponents=1 # Only one single component for a scalar field
91 fillFunction="x+y"
92 field.fillFromAnalytic(nbComponents,fillFunction);
93
94 # The MEDLoader can be used to save all the stuff in a med file. You
95 # just have to specify the field and the MEDLoader will save the
96 # underlying mesh.
97 createFromScratch=True
98 ML.MEDLoader.WriteField("fieldtest.med",field,createFromScratch)
99
100 # --- Field on nodes
101
102 field = MC.MEDCouplingFieldDouble.New(MC.ON_NODES);
103 field.setName("AnalyticField_onNodes");
104 field.setMesh(umesh);
105 field.fillFromAnalytic(nbComponents,fillFunction);
106 createFromScratch=False
107 ML.MEDLoader.WriteField("fieldtest.med",field,createFromScratch)
108
109
110 #
111 # ===============================================================
112 # Creating a scalar field, working with numpy
113 # ===============================================================
114 #
115
116 # We start by creating a numpy matrix
117 import numpy
118 rows=[]
119 for irow in range(sizeY):
120     row = numpy.arange(irow*sizeY,irow*sizeY+sizeX,dtype='float64')
121     rows.append(row)
122
123 marray = numpy.vstack(rows)
124
125 # Then, we can reshape the matrix in a 1D vector that concatenate all
126 # the rows
127 data=marray.reshape(1,sizeX*sizeY)[0]
128 # Finally, we can create a simple list as required by the MEDCoupling
129 # DataArrayDouble. Note also the usage of float type because
130 # MEDCoupling works only with real numbers
131 listdata=list(data)
132
133 # Create the field using the list obtained from the numpy array
134 fieldWithNumpy = MC.MEDCouplingFieldDouble.New(MC.ON_CELLS);
135 fieldWithNumpy.setName("Numpy Field");
136 fieldWithNumpy.setMesh(umesh);
137
138 nbCells=sizeX*sizeY
139 dataArray=MC.DataArrayDouble.New();
140 dataArray.setValues(listdata,nbCells,nbComponents)
141 fieldWithNumpy.setArray(dataArray);
142
143 createFromScratch=False
144 ML.MEDLoader.WriteField("fieldtest.med",fieldWithNumpy,createFromScratch)
145
146