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 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
55 dircase = os.path.join("/tmp", Test_Name)
56 if ( os.path.isdir(dircase) ) :
60 dircase = tempfile.mkdtemp()
61 # ==================================
66 from salome.geom import geomBuilder
67 from salome.smesh import smeshBuilder
68 from salome.StdMeshers import StdMeshersBuilder
70 from MEDLoader import MEDLoader
71 from MEDCouplingRemapper import MEDCouplingRemapper
74 ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters("Interface Applicative", 1))
75 ipar.append("AP_MODULES_LIST", "Homard")
77 #========================================================================
78 #========================================================================
79 def geom_smesh_exec(theStudy):
81 Python script for GEOM and SMESH
87 geompy = geomBuilder.New(theStudy)
91 BOX = geompy.MakeBoxDXDYDZ(DX, DY, DZ, "BOX")
93 # Creation of the mesh
94 # ====================
95 smesh = smeshBuilder.New(theStudy)
96 MESH = smesh.Mesh(BOX)
97 smesh.SetName(MESH.GetMesh(), 'MESH')
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')
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')
112 Hexa_3D = MESH.Hexahedron(algo=smeshBuilder.Hexa)
113 smesh.SetName(Hexa_3D.GetAlgorithm(), 'Hexa_3D')
118 isDone = MESH.Compute()
127 ficmed = os.path.join(dircase, 'maill.00.med')
128 MESH.ExportMED( ficmed, 0, SMESH.MED_V2_2, 1, None ,1)
130 raise Exception('ExportToMEDX() failed. '+e.message)
137 #========================================================================
139 #========================================================================
140 def field_exec(theStudy, niter):
142 Python script for MEDCoupling
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]
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()
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")
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
184 #========================================================================
185 #========================================================================
186 def homard_exec(theStudy):
188 Python script for HOMARD
194 homard.SetCurrentStudy(theStudy)
196 # Creation of the zones
197 # =====================
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)
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)
207 # Creation of the hypotheses
208 # ==========================
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()
221 for iaux in range(nbzone) :
222 print HypoName_1, " : ", dico[laux[jaux+1]], "sur la zone", laux[jaux]
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()
233 for iaux in range(nbzone) :
234 print HypoName_2, " : ", dico[laux[jaux+1]], "sur la zone", laux[jaux]
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()
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)
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)
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)
290 # Creation of the iteration 3
292 error = field_exec(theStudy, 2)
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)
314 #========================================================================
319 error_main = geom_smesh_exec(salome.myStudy)
321 raise Exception('Pb in geom_smesh_exec')
323 raise Exception('Pb in geom_smesh_exec: '+e.message)
325 homard = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
326 assert homard is not None, "Impossible to load homard engine"
327 homard.SetLanguageShort("fr")
329 # Exec of HOMARD-SALOME
332 error_main = homard_exec(salome.myStudy)
334 raise Exception('Pb in homard_exec at iteration %d' %error_main )
336 raise Exception('Pb in homard_exec: '+e.message)
338 # Test of the results
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)
344 if salome.sg.hasDesktop():
345 salome.sg.updateObjBrowser(1)
346 iparameters.getSession().restoreVisualState(1)