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