1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2016 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
24 __revision__ = "V3.03"
26 #========================================================================
33 #========================================================================
41 import MEDCoupling as mc
42 import MEDLoader as ml
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 # ==================================
60 from salome.geom import geomBuilder
61 from salome.smesh import smeshBuilder
62 from salome.StdMeshers import StdMeshersBuilder
64 from MEDCouplingRemapper import MEDCouplingRemapper
67 IPAR = iparameters.IParameters(salome.myStudy.GetCommonParameters("Interface Applicative", 1))
68 IPAR.append("AP_MODULES_LIST", "Homard")
70 #========================================================================
71 #========================================================================
72 def geom_smesh_exec(theStudy):
74 Python script for GEOM and SMESH
80 geompy = geomBuilder.New(theStudy)
84 box_g = geompy.MakeBoxDXDYDZ(DX, DY, DZ, "BOX")
86 # Creation of the mesh
87 # ====================
88 smesh = smeshBuilder.New(theStudy)
89 box_m = smesh.Mesh(box_g)
90 smesh.SetName(box_m.GetMesh(), 'MESH')
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')
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')
105 hexa_3d = box_m.Hexahedron(algo=smeshBuilder.Hexa)
106 smesh.SetName(hexa_3d.GetAlgorithm(), 'Hexa_3D')
111 isDone = box_m.Compute()
120 ficmed = os.path.join(DIRCASE, 'maill.00.med')
121 box_m.ExportMED( ficmed, 0, SMESH.MED_V2_2, 1, None, 1)
122 except IOError as eee:
124 raise Exception('ExportToMEDX() failed. '+str(eee.message))
130 #========================================================================
132 #========================================================================
133 def field_exec(theStudy, niter):
135 Python script for MEDCoupling
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]
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())
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")
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
177 #========================================================================
178 #========================================================================
179 def homard_exec(theStudy):
181 Python script for HOMARD
187 HOMARD.SetCurrentStudy(theStudy)
189 # Creation of the zones
190 # =====================
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)
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)
200 # Creation of the hypotheses
201 # ==========================
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
214 for _ in range(nbzone) :
215 print(hyponame_1, " : ", dico[laux[jaux+1]], "sur la zone", laux[jaux])
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
226 for _ in range(nbzone) :
227 print(hyponame_2, " : ", dico[laux[jaux+1]], "sur la zone", laux[jaux])
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())
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)
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)
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)
282 # Creation of the iteration 3
284 error = field_exec(theStudy, 2)
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)
306 #========================================================================
311 ERROR = geom_smesh_exec(salome.myStudy)
313 raise Exception('Pb in geom_smesh_exec')
314 except RuntimeError as eee:
315 raise Exception('Pb in geom_smesh_exec: '+str(eee.message))
317 HOMARD = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
318 assert HOMARD is not None, "Impossible to load homard engine"
319 HOMARD.SetLanguageShort("fr")
321 # Exec of HOMARD-SALOME
324 ERROR = homard_exec(salome.myStudy)
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))
330 # Test of the results
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)
336 if salome.sg.hasDesktop():
337 salome.sg.updateObjBrowser(True)
338 iparameters.getSession().restoreVisualState(1)