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