1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2015 CEA/DEN, EDF R&D
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 Python script for HOMARD
26 #========================================================================
33 #========================================================================
42 import MEDCoupling as mc
43 import MEDLoader as ml
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
58 DIRCASE = os.path.join("/tmp", TEST_NAME)
59 if ( os.path.isdir(DIRCASE) ) :
63 DIRCASE = tempfile.mkdtemp()
64 # ==================================
69 from salome.geom import geomBuilder
70 from salome.smesh import smeshBuilder
71 from salome.StdMeshers import StdMeshersBuilder
73 from MEDLoader import MEDLoader
74 from MEDCouplingRemapper import MEDCouplingRemapper
77 IPAR = iparameters.IParameters(salome.myStudy.GetCommonParameters("Interface Applicative", 1))
78 IPAR.append("AP_MODULES_LIST", "Homard")
80 #========================================================================
81 #========================================================================
82 def geom_smesh_exec(theStudy):
84 Python script for GEOM and SMESH
90 geompy = geomBuilder.New(theStudy)
94 box_g = geompy.MakeBoxDXDYDZ(DX, DY, DZ, "BOX")
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')
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')
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')
115 hexa_3d = box_m.Hexahedron(algo=smeshBuilder.Hexa)
116 smesh.SetName(hexa_3d.GetAlgorithm(), 'Hexa_3D')
121 isDone = box_m.Compute()
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:
134 raise Exception('ExportToMEDX() failed. '+eee.message)
140 #========================================================================
142 #========================================================================
143 def field_exec(theStudy, niter):
145 Python script for MEDCoupling
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]
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()
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")
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
187 #========================================================================
188 #========================================================================
189 def homard_exec(theStudy):
191 Python script for HOMARD
197 HOMARD.SetCurrentStudy(theStudy)
199 # Creation of the zones
200 # =====================
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)
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)
210 # Creation of the hypotheses
211 # ==========================
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()
224 for iaux in range(nbzone) :
225 print hyponame_1, " : ", dico[laux[jaux+1]], "sur la zone", laux[jaux]
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()
236 for iaux in range(nbzone) :
237 print hyponame_2, " : ", dico[laux[jaux+1]], "sur la zone", laux[jaux]
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()
256 # Creation of the cases
257 # =====================
258 # Creation of the case
259 casename = "case_" + TEST_NAME
260 print "-------- Creation of the case", casename
261 mesh_file = os.path.join(DIRCASE, 'maill.00.med')
262 case_test_4 = HOMARD.CreateCase(casename, 'MESH', mesh_file)
263 case_test_4.SetDirName(DIRCASE)
265 # Creation of the iterations
266 # ==========================
267 # Creation of the iteration 1
268 iter_name = "I_" + TEST_NAME + "_1"
269 print "-------- Creation of the iteration", iter_name
270 iter_test_4_1 = case_test_4.NextIteration(iter_name)
271 iter_test_4_1.AssociateHypo(hyponame_1)
272 print ". Hypothese :", hyponame_1
273 iter_test_4_1.SetMeshName('M1')
274 iter_test_4_1.SetMeshFile(os.path.join(DIRCASE, 'maill.01.med'))
275 error = iter_test_4_1.Compute(1, 2)
280 # Creation of the iteration 2
281 iter_name = "I_" + TEST_NAME + "_2"
282 print "-------- Creation of the iteration", iter_name
283 iter_test_4_2 = iter_test_4_1.NextIteration(iter_name)
284 iter_test_4_2.AssociateHypo(hyponame_2)
285 print ". Hypothese :", hyponame_2
286 iter_test_4_2.SetMeshName('M2')
287 iter_test_4_2.SetMeshFile(os.path.join(DIRCASE, 'maill.02.med'))
288 error = iter_test_4_2.Compute(1, 2)
293 # Creation of the iteration 3
295 error = field_exec(theStudy, 2)
300 iter_name = "I_" + TEST_NAME + "_3"
301 print "-------- Creation of the iteration", iter_name
302 iter_test_4_3 = iter_test_4_2.NextIteration(iter_name)
303 iter_test_4_3.AssociateHypo(hyponame_3)
304 print ". Hypothese :", hyponame_3
305 iter_test_4_3.SetMeshName('M3')
306 iter_test_4_3.SetFieldFile(os.path.join(DIRCASE, 'maill.02.med'))
307 iter_test_4_3.SetMeshFile(os.path.join(DIRCASE, 'maill.03.med'))
308 error = iter_test_4_3.Compute(1, 2)
317 #========================================================================
322 ERROR = geom_smesh_exec(salome.myStudy)
324 raise Exception('Pb in geom_smesh_exec')
325 except Exception, eee:
326 raise Exception('Pb in geom_smesh_exec: '+eee.message)
328 HOMARD = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
329 assert HOMARD is not None, "Impossible to load homard engine"
330 HOMARD.SetLanguageShort("fr")
332 # Exec of HOMARD-SALOME
335 ERROR = homard_exec(salome.myStudy)
337 raise Exception('Pb in homard_exec at iteration %d' %ERROR )
338 except Exception, eee:
339 raise Exception('Pb in homard_exec: '+eee.message)
341 # Test of the results
343 N_REP_TEST_FILE = N_ITER_TEST_FILE
344 DESTROY_DIR = not DEBUG
345 test_results(REP_DATA, TEST_NAME, DIRCASE, N_ITER_TEST_FILE, N_REP_TEST_FILE, DESTROY_DIR)
347 if salome.sg.hasDesktop():
348 salome.sg.updateObjBrowser(1)
349 iparameters.getSession().restoreVisualState(1)