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