Salome HOME
coquilles
[modules/homard.git] / src / tests / Test / test_4.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2015  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 Test test_4
23 """
24 __revision__ = "V2.1"
25
26 #========================================================================
27 TEST_NAME = "test_4"
28 DEBUG = False
29 N_ITER_TEST_FILE = 3
30 DX = 600.
31 DY = 400.
32 DZ = 200.
33 #========================================================================
34 import os
35 import tempfile
36 import sys
37 import numpy as np
38 import salome
39 import GEOM
40 import SMESH
41 import HOMARD
42 import MEDCoupling as mc
43 import MEDLoader as ml
44 #
45 # ==================================
46 PATH_HOMARD = os.getenv('HOMARD_ROOT_DIR')
47 # Repertoire des scripts utilitaires
48 REP_PYTHON = os.path.join(PATH_HOMARD, "bin", "salome", "test", "HOMARD")
49 REP_PYTHON = os.path.normpath(REP_PYTHON)
50 sys.path.append(REP_PYTHON)
51 from test_util import remove_dir
52 from test_util import test_results
53 # Repertoire des donnees du test
54 REP_DATA = os.path.join(PATH_HOMARD, "share", "salome", "homardsamples")
55 REP_DATA = os.path.normpath(REP_DATA)
56 # Repertoire des resultats
57 if DEBUG :
58   DIRCASE = os.path.join("/tmp", TEST_NAME)
59   if ( os.path.isdir(DIRCASE) ) :
60     remove_dir(DIRCASE)
61   os.mkdir(DIRCASE)
62 else :
63   DIRCASE = tempfile.mkdtemp()
64 # ==================================
65
66 salome.salome_init()
67
68 import SALOMEDS
69 from salome.geom import geomBuilder
70 from salome.smesh import smeshBuilder
71 from salome.StdMeshers import StdMeshersBuilder
72 #
73 from MEDLoader import MEDLoader
74 from MEDCouplingRemapper import MEDCouplingRemapper
75
76 import iparameters
77 IPAR = iparameters.IParameters(salome.myStudy.GetCommonParameters("Interface Applicative", 1))
78 IPAR.append("AP_MODULES_LIST", "Homard")
79 #
80 #========================================================================
81 #========================================================================
82 def geom_smesh_exec(theStudy):
83   """
84 Python script for GEOM and SMESH
85   """
86   error = 0
87 #
88   while not error :
89   #
90     geompy = geomBuilder.New(theStudy)
91   #
92   # Creation of the box
93   # ===================
94     box_g = geompy.MakeBoxDXDYDZ(DX, DY, DZ, "BOX")
95
96   # Creation of the mesh
97   # ====================
98     smesh = smeshBuilder.New(theStudy)
99     box_m = smesh.Mesh(box_g)
100     smesh.SetName(box_m.GetMesh(), 'MESH')
101   #
102   # Creation of the hypotheses
103   # ==========================
104     regular_1d = box_m.Segment()
105     smesh.SetName(regular_1d.GetAlgorithm(), 'Regular_1D')
106     length = min(DX, DY, DZ) / 5.
107     local_length = regular_1d.LocalLength(length, None, 1e-07)
108     smesh.SetName(local_length, 'Local Length')
109   #
110     quadrangle_2d = box_m.Quadrangle(algo=smeshBuilder.QUADRANGLE)
111     smesh.SetName(quadrangle_2d.GetAlgorithm(), 'Quadrangle_2D')
112     quadrangle_parameters = quadrangle_2d.QuadrangleParameters(StdMeshersBuilder.QUAD_STANDARD, -1, [], [])
113     smesh.SetName(quadrangle_parameters, 'Quadrangle Parameters')
114   #
115     hexa_3d = box_m.Hexahedron(algo=smeshBuilder.Hexa)
116     smesh.SetName(hexa_3d.GetAlgorithm(), 'Hexa_3D')
117   #
118   # Computation
119   # ===========
120   #
121     isDone = box_m.Compute()
122     if not isDone :
123       error = 1
124       break
125   #
126   # MED exportation
127   # ===============
128   #
129     try:
130       ficmed = os.path.join(DIRCASE, 'maill.00.med')
131       box_m.ExportMED( ficmed, 0, SMESH.MED_V2_2, 1, None, 1)
132     except Exception, eee:
133       error = 2
134       raise Exception('ExportToMEDX() failed. '+eee.message)
135   #
136     break
137   #
138   return error
139
140 #========================================================================
141 #
142 #========================================================================
143 def field_exec(theStudy, niter):
144   """
145 Python script for MEDCoupling
146   """
147   error = 0
148 #
149   while not error :
150   #
151   # The mesh
152   # ========
153     ficmed = os.path.join(DIRCASE, 'maill.%02d.med' % niter)
154     meshMEDFileRead = ml.MEDFileMesh.New(ficmed)
155     meshRead0 = meshMEDFileRead.getMeshAtLevel(0)
156   # Valeurs of the field
157   # ====================
158     nbNodes = meshRead0.getNumberOfNodes()
159     valeur = mc.DataArrayDouble(nbNodes)
160     for iaux, taux in enumerate(meshRead0.getCoords()) :
161       #ligne   = "x = %f" % taux[0]
162       #ligne  += ", y = %f" % taux[1]
163       #ligne  += ", z = %f" % taux[2]
164       #print ligne
165       #distance = (taux[0]-DX*0.2)**2 + (taux[1]-DY*0.2)**2 + (taux[2]-DZ*0.4)**2
166       distance = min(abs(taux[0]-DX*0.4), abs(taux[1]-DY*0.2), abs(taux[2]-DZ*0.4))
167       valeur[iaux] = 1.e0 / max ( 1.e-5, np.sqrt(distance) )
168     #print ". valeur", valeur
169     nparr = valeur.toNumPyArray()
170     print ". mini/maxi", nparr.min(), nparr.max()
171   #
172   # Creation of the field
173   # =====================
174     field = ml.MEDCouplingFieldDouble(ml.ON_NODES, ml.ONE_TIME)
175     field.setArray(valeur)
176     field.setMesh(meshRead0)
177     field.setName("DISTANCE")
178   #
179     fMEDFile_ch = ml.MEDFileField1TS()
180     fMEDFile_ch.setFieldNoProfileSBT(field)     # No profile desired on the field, Sort By Type
181     fMEDFile_ch.write(ficmed, 0) # 0 to indicate that we *append* (and no overwrite) to the MED file
182   #
183     break
184   #
185   return error
186
187 #========================================================================
188 #========================================================================
189 def homard_exec(theStudy):
190   """
191 Python script for HOMARD
192   """
193   error = 0
194 #
195   while not error :
196   #
197     HOMARD.SetCurrentStudy(theStudy)
198   #
199   # Creation of the zones
200   # =====================
201   #
202     epsilon = min(DX, DY, DZ) / 100.
203   # Creation of the box zone_4_1
204     zone_4_1 = HOMARD.CreateZoneBox('Zone_4_1', -epsilon, DX/3.+epsilon, DY/4.-epsilon, 3.*DY/4.+epsilon, 4.*DZ/5.-epsilon, DZ+epsilon)
205
206   # Creation of the sphere zone_4_2
207     rayon = min(DX, DY, DZ) / 4.
208     zone_4_2 = HOMARD.CreateZoneSphere('Zone_4_2', DX/3., DY*0.3, DZ*0.6, rayon)
209   #
210   # Creation of the hypotheses
211   # ==========================
212     dico = {}
213     dico["1"] = "raffinement"
214     dico["-1"] = "deraffinement"
215   # Creation of the hypothesis hypo_4_1
216     hyponame_1 = "Zone_1"
217     print "-------- Creation of the hypothesis", hyponame_1
218     hypo_4_1 = HOMARD.CreateHypothesis(hyponame_1)
219     hypo_4_1.AddZone('Zone_4_1', 1)
220     hypo_4_1.SetExtraOutput(2)
221     laux = hypo_4_1.GetZones()
222     nbzone = len(laux)/2
223     jaux = 0
224     for iaux in range(nbzone) :
225       print hyponame_1, " : ", dico[laux[jaux+1]], "sur la zone", laux[jaux]
226       jaux += 2
227   # Creation of the hypothesis hypo_4_2
228     hyponame_2 = "Zone_2"
229     print "-------- Creation of the hypothesis", hyponame_2
230     hypo_4_2 = HOMARD.CreateHypothesis(hyponame_2)
231     hypo_4_2.AddZone('Zone_4_2', 1)
232     hypo_4_2.SetExtraOutput(2)
233     laux = hypo_4_2.GetZones()
234     nbzone = len(laux)/2
235     jaux = 0
236     for iaux in range(nbzone) :
237       print hyponame_2, " : ", dico[laux[jaux+1]], "sur la zone", laux[jaux]
238       jaux += 2
239   # Creation of the hypothesis DISTANCE INVERSE
240     hyponame_3 = "DISTANCE INVERSE"
241     print "-------- Creation of the hypothesis", hyponame_3
242     hypo_4_3 = HOMARD.CreateHypothesis(hyponame_3)
243     hypo_4_3.SetField('DISTANCE')
244     hypo_4_3.SetUseComp(0)
245     hypo_4_3.SetRefinThr(1, 0.3)
246     hypo_4_3.SetUnRefThr(1, 0.2)
247     hypo_4_3.AddFieldInterp('DISTANCE')
248     hypo_4_3.SetExtraOutput(2)
249     print hyponame_3, " : zones utilisées :", hypo_4_3.GetZones()
250     print hyponame_3, " : champ utilisé :", hypo_4_3.GetFieldName()
251     print hyponame_3, " : composantes utilisées :", hypo_4_3.GetComps()
252     if ( len (hypo_4_3.GetFieldName()) > 0 ) :
253       print ".. caractéristiques de l'adaptation :", hypo_4_3.GetField()
254     print hyponame_3, " : champs interpolés :", hypo_4_3.GetFieldInterps()
255   #
256   # Creation of the cases
257   # =====================
258     # Creation of the case
259     print "-------- Creation of the case", TEST_NAME
260     mesh_file = os.path.join(DIRCASE, 'maill.00.med')
261     case_test_4 = HOMARD.CreateCase(TEST_NAME, 'MESH', mesh_file)
262     case_test_4.SetDirName(DIRCASE)
263   #
264   # Creation of the iterations
265   # ==========================
266   # Creation of the iteration 1
267     iter_name = "I_" + TEST_NAME + "_1"
268     print "-------- Creation of the iteration", iter_name
269     iter_test_4_1 = case_test_4.NextIteration(iter_name)
270     iter_test_4_1.AssociateHypo(hyponame_1)
271     print ". Hypothese :", hyponame_1
272     iter_test_4_1.SetMeshName('M1')
273     iter_test_4_1.SetMeshFile(os.path.join(DIRCASE, 'maill.01.med'))
274     error = iter_test_4_1.Compute(1, 2)
275     if error :
276       error = 1
277       break
278
279   # Creation of the iteration 2
280     iter_name = "I_" + TEST_NAME + "_2"
281     print "-------- Creation of the iteration", iter_name
282     iter_test_4_2 = iter_test_4_1.NextIteration(iter_name)
283     iter_test_4_2.AssociateHypo(hyponame_2)
284     print ". Hypothese :", hyponame_2
285     iter_test_4_2.SetMeshName('M2')
286     iter_test_4_2.SetMeshFile(os.path.join(DIRCASE, 'maill.02.med'))
287     error = iter_test_4_2.Compute(1, 2)
288     if error :
289       error = 2
290       break
291
292   # Creation of the iteration 3
293   #
294     error = field_exec(theStudy, 2)
295     if error :
296       error = 30
297       break
298   #
299     iter_name = "I_" + TEST_NAME + "_3"
300     print "-------- Creation of the iteration", iter_name
301     iter_test_4_3 = iter_test_4_2.NextIteration(iter_name)
302     iter_test_4_3.AssociateHypo(hyponame_3)
303     print ". Hypothese :", hyponame_3
304     iter_test_4_3.SetMeshName('M3')
305     iter_test_4_3.SetFieldFile(os.path.join(DIRCASE, 'maill.02.med'))
306     iter_test_4_3.SetMeshFile(os.path.join(DIRCASE, 'maill.03.med'))
307     error = iter_test_4_3.Compute(1, 2)
308     if error :
309       error = 3
310       break
311   #
312     break
313   #
314   return error
315
316 #========================================================================
317 #
318 # Geometry and Mesh
319 #
320 try :
321   ERROR = geom_smesh_exec(salome.myStudy)
322   if ERROR :
323     raise Exception('Pb in geom_smesh_exec')
324 except Exception, eee:
325   raise Exception('Pb in geom_smesh_exec: '+eee.message)
326
327 HOMARD = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
328 assert HOMARD is not None, "Impossible to load homard engine"
329 HOMARD.SetLanguageShort("fr")
330 #
331 # Exec of HOMARD-SALOME
332 #
333 try :
334   ERROR = homard_exec(salome.myStudy)
335   if ERROR :
336     raise Exception('Pb in homard_exec at iteration %d' %ERROR )
337 except Exception, eee:
338   raise Exception('Pb in homard_exec: '+eee.message)
339 #
340 # Test of the results
341 #
342 N_REP_TEST_FILE = N_ITER_TEST_FILE
343 DESTROY_DIR = not DEBUG
344 test_results(REP_DATA, TEST_NAME, DIRCASE, N_ITER_TEST_FILE, N_REP_TEST_FILE, DESTROY_DIR)
345 #
346 if salome.sg.hasDesktop():
347   salome.sg.updateObjBrowser(1)
348   iparameters.getSession().restoreVisualState(1)
349