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