Salome HOME
Nouveau cas-test pour prendre en compte les particularités de Code_Saturne
[modules/homard.git] / src / tests / Test / test_5.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2016  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 Python script for HOMARD
22 Specific conditions for Code_Saturne
23 Test test_5
24 """
25 __revision__ = "V1.0"
26
27 #========================================================================
28 TEST_NAME = "test_5"
29 DEBUG = False
30 DEBUG = True
31 VERBOSE = True
32 N_ITER_TEST_FILE = 3
33 NBCELL_X = 10
34 NBCELL_Y = 10
35 NBCELL_Z = 10
36 LG_X = 360.
37 LG_Y = 240.
38 LG_Z = 160.
39 MESH_NAME = "MESH"
40 #========================================================================
41 import os
42 import tempfile
43 import sys
44 import numpy as np
45 import salome
46 import HOMARD
47 import MEDCoupling as mc
48 import MEDLoader as ml
49 #
50 # ==================================
51 PATH_HOMARD = os.getenv('HOMARD_ROOT_DIR')
52 # Repertoire des scripts utilitaires
53 REP_PYTHON = os.path.join(PATH_HOMARD, "bin", "salome", "test", "HOMARD")
54 REP_PYTHON = os.path.normpath(REP_PYTHON)
55 sys.path.append(REP_PYTHON)
56 from test_util import remove_dir
57 from test_util import test_results
58 # Repertoire des donnees du test
59 REP_DATA = os.path.join(PATH_HOMARD, "share", "salome", "homardsamples")
60 REP_DATA = os.path.normpath(REP_DATA)
61 # Repertoire des resultats
62 if DEBUG :
63   DIRCASE = os.path.join("/tmp", TEST_NAME)
64   if ( os.path.isdir(DIRCASE) ) :
65     remove_dir(DIRCASE)
66   os.mkdir(DIRCASE)
67 else :
68   DIRCASE = tempfile.mkdtemp()
69 # ==================================
70
71 salome.salome_init()
72 #
73 from MEDCouplingRemapper import MEDCouplingRemapper
74
75 import iparameters
76 IPAR = iparameters.IParameters(salome.myStudy.GetCommonParameters("Interface Applicative", 1))
77 IPAR.append("AP_MODULES_LIST", "Homard")
78 #
79 #========================================================================
80 #========================================================================
81 def mesh_exec(theStudy):
82   """
83 Python script for MED
84   """
85   error = 0
86 #
87   while not error :
88   #
89   # Creation of the mesh
90   # ====================
91     maillage_3d = ml.MEDCouplingUMesh(MESH_NAME, 2)
92     maillage_3d.setMeshDimension(3)
93   #
94   # Creation of the nodes
95   # ====================
96   #
97     nbno_x = NBCELL_X + 1
98     nbno_y = NBCELL_Y + 1
99     nbno_z = NBCELL_Z + 1
100 #
101     delta_x = LG_X / float(NBCELL_X)
102     delta_y = LG_Y / float(NBCELL_Y)
103     delta_z = LG_Z / float(NBCELL_Z)
104 #
105     coordinates = list()
106     coo_z = -0.5*LG_Z
107     for kaux in range(nbno_z) :
108       coo_y = -0.5*LG_Y
109       for jaux in range(nbno_y) :
110         coo_x = -0.5*LG_X
111         for iaux in range(nbno_x) :
112           coordinates.append(coo_x)
113           coordinates.append(coo_y)
114           coordinates.append(coo_z)
115           coo_x += delta_x
116         coo_y += delta_y
117       coo_z += delta_z
118 #
119     nbr_nodes = nbno_x*nbno_y*nbno_z
120     les_coords = ml.DataArrayDouble(coordinates, nbr_nodes, 3)
121     maillage_3d.setCoords(les_coords)
122   #
123   # Creation of the cells
124   # =====================
125   #
126     nbr_cell_3d = NBCELL_X*NBCELL_Y*NBCELL_Z
127     maillage_3d.allocateCells(nbr_cell_3d)
128 #
129     decala_z = nbno_x*nbno_y
130 #   kaux = numero de la tranche en z
131     for kaux in range(1, nbno_z) :
132 #
133       #print ". Tranche en z numero %d" % kaux
134       decala = decala_z*(kaux-1)
135 #     jaux = numero de la tranche en y
136       for jaux in range(1, nbno_y) :
137 #
138         #print ". Tranche en y numero %d" % jaux
139 #       iaux = numero de la tranche en x
140         for iaux in range(1, nbno_x) :
141 #
142           #print ". Tranche en x numero %d" % iaux
143           nref = decala+iaux-1
144           laux = [nref, nref+nbno_x, nref+1+nbno_x, nref+1, nref+decala_z, nref+nbno_x+decala_z, nref+1+nbno_x+decala_z, nref+1+decala_z]
145           #if self.verbose_max :
146             #if ( ( iaux==1 and jaux==1 and kaux==1 ) or ( iaux==(nbr_nodes_x-1) and jaux==(nbr_nodes_y-1) and kaux==(nbr_nodes_z-1) ) ) :
147               #print ". Maille %d : " % (iaux*jaux*kaux), laux
148           maillage_3d.insertNextCell(ml.NORM_HEXA8, 8, laux)
149 #
150         decala += nbno_x
151 #
152     maillage_3d.finishInsertingCells()
153   #
154   # Agregation into a structure of MEDLoader
155   # ========================================
156   #
157     meshMEDFile3D = ml.MEDFileUMesh()
158     meshMEDFile3D.setName(MESH_NAME)
159 #
160     meshMEDFile3D.setMeshAtLevel(0, maillage_3d)
161 #
162     meshMEDFile3D.rearrangeFamilies()
163   #
164   # MED exportation
165   # ===============
166   #
167     try:
168       ficmed = os.path.join(DIRCASE, 'maill.00.med')
169       #print "Ecriture du maillage dans le fichier", ficmed
170       meshMEDFile3D.write(ficmed, 2)
171     except Exception, eee:
172       error = 2
173       raise Exception('ExportToMEDX() failed. '+eee.message)
174   #
175     break
176   #
177   return error
178
179 #========================================================================
180 #
181 #========================================================================
182 def field_exec(theStudy, niter):
183   """
184 Python script for MEDCoupling
185   """
186   error = 0
187 #
188   while not error :
189   #
190   # The mesh
191   # ========
192     ficmed = os.path.join(DIRCASE, 'maill.%02d.med' % niter)
193     meshMEDFileRead = ml.MEDFileMesh.New(ficmed)
194     mesh_read0 = meshMEDFileRead.getMeshAtLevel(0)
195   # Barycenter of the cells
196   # =======================
197     cg_hexa_ml = mesh_read0.computeIsoBarycenterOfNodesPerCell()
198     cg_hexa = cg_hexa_ml.toNumPyArray()
199   # Target
200   # ======
201     xyz_p = np.zeros(3, dtype=np.float)
202     xyz_p[0] = -0.20*float(1-niter) * LG_X
203     xyz_p[1] = -0.15*float(1-niter) * LG_Y
204     xyz_p[2] = -0.10*float(1-niter) * LG_Z
205   # Values of the field
206   # ===================
207     nbr_cell_3d = mesh_read0.getNumberOfCells()
208     valeur = mc.DataArrayDouble(nbr_cell_3d)
209     for num_mail in range(nbr_cell_3d) :
210       #ligne   = "x = %f" % cg_hexa[num_mail][0]
211       #ligne  += ", y = %f" % cg_hexa[num_mail][1]
212       #ligne  += ", z = %f" % cg_hexa[num_mail][2]
213       #print ligne
214       distance = np.linalg.norm(cg_hexa[num_mail]-xyz_p)
215       valeur[num_mail] = 1.e0 / max ( 1.e-5, distance)
216     #print ". valeur", valeur
217     nparr = valeur.toNumPyArray()
218     print ". mini/maxi", nparr.min(), nparr.max()
219   #
220   # Creation of the field
221   # =====================
222     field = ml.MEDCouplingFieldDouble(ml.ON_CELLS, ml.ONE_TIME)
223     field.setArray(valeur)
224     field.setMesh(mesh_read0)
225     field.setName("DISTANCE")
226   #
227     fMEDFile_ch = ml.MEDFileField1TS()
228     fMEDFile_ch.setFieldNoProfileSBT(field)     # No profile desired on the field, Sort By Type
229     fMEDFile_ch.write(ficmed, 0) # 0 to indicate that we *append* (and no overwrite) to the MED file
230   #
231     break
232   #
233   return error, ficmed
234
235 #========================================================================
236 #========================================================================
237 def homard_exec(theStudy):
238   """
239 Python script for HOMARD
240   """
241   error = 0
242 #
243   while not error :
244   #
245     HOMARD.SetCurrentStudy(theStudy)
246   #
247   # Creation of the hypothese DISTANCE INVERSE
248   # ==========================================
249     hyponame = "DISTANCE INVERSE"
250     print "-------- Creation of the hypothesis", hyponame
251     hypo_5 = HOMARD.CreateHypothesis(hyponame)
252     hypo_5.SetField('DISTANCE')
253     hypo_5.SetUseComp(0)
254     hypo_5.SetRefinThr(1, 0.020)
255     hypo_5.SetUnRefThr(1, 0.015)
256     print hyponame, " : champ utilisé :", hypo_5.GetFieldName()
257     print ".. caractéristiques de l'adaptation :", hypo_5.GetField()
258   #
259   # Creation of the cases
260   # =====================
261     # Creation of the case
262     print "-------- Creation of the case", TEST_NAME
263     mesh_file = os.path.join(DIRCASE, 'maill.00.med')
264     case_test_5 = HOMARD.CreateCase(TEST_NAME, 'MESH', mesh_file)
265     case_test_5.SetDirName(DIRCASE)
266     case_test_5.SetConfType(1)
267     case_test_5.SetExtType(1)
268   #
269   # Creation of the iterations
270   # ==========================
271   #
272     for niter in range(N_ITER_TEST_FILE) :
273   #
274       s_niterp1 = "%02d" % (niter + 1)
275     #
276     # Creation of the indicator
277     #
278       error, ficmed_indic = field_exec(theStudy, niter)
279       if error :
280         error = 10
281         break
282     #
283     # Creation of the iteration
284     #
285       iter_name = "I_" + TEST_NAME + "_" + s_niterp1
286       print "-------- Creation of the iteration", iter_name
287       if ( niter == 0 ) :
288         iter_test_5 = case_test_5.NextIteration(iter_name)
289       else :
290         iter_test_5 = iter_test_5.NextIteration(iter_name)
291       iter_test_5.AssociateHypo(hyponame)
292       iter_test_5.SetFieldFile(ficmed_indic)
293       iter_test_5.SetMeshName(MESH_NAME+"_" + s_niterp1)
294       iter_test_5.SetMeshFile(os.path.join(DIRCASE, "maill."+s_niterp1+".med"))
295       error = iter_test_5.Compute(1, 2)
296       if error :
297         error = 20
298         break
299   #
300     break
301   #
302   return error
303
304 #========================================================================
305 #
306 # Geometry and Mesh
307 #
308 try :
309   ERROR = mesh_exec(salome.myStudy)
310   if ERROR :
311     raise Exception('Pb in mesh_exec')
312 except Exception, eee:
313   raise Exception('Pb in mesh_exec: '+eee.message)
314
315 HOMARD = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
316 assert HOMARD is not None, "Impossible to load homard engine"
317 HOMARD.SetLanguageShort("fr")
318 #
319 # Exec of HOMARD-SALOME
320 #
321 try :
322   ERROR = homard_exec(salome.myStudy)
323   if ERROR :
324     raise Exception('Pb in homard_exec at iteration %d' %ERROR )
325 except Exception, eee:
326   raise Exception('Pb in homard_exec: '+eee.message)
327 #
328 # Test of the results
329 #
330 N_REP_TEST_FILE = N_ITER_TEST_FILE
331 DESTROY_DIR = not DEBUG
332 test_results(REP_DATA, TEST_NAME, DIRCASE, N_ITER_TEST_FILE, N_REP_TEST_FILE, DESTROY_DIR)
333 #
334 if salome.sg.hasDesktop():
335   salome.sg.updateObjBrowser(1)
336   iparameters.getSession().restoreVisualState(1)
337