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