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
22 Copyright EDF-R&D 2010, 2015
27 #========================================================================
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 test_results
52 # Repertoire des resultats
53 dircase = tempfile.mkdtemp()
54 #dircase = "/scratch/D68518/Salome/resu"
55 # ==================================
60 from salome.geom import geomBuilder
61 from salome.smesh import smeshBuilder
62 from salome.StdMeshers import StdMeshersBuilder
64 from MEDLoader import MEDLoader
65 from MEDCouplingRemapper import MEDCouplingRemapper
68 ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters("Interface Applicative", 1))
69 ipar.append("AP_MODULES_LIST", "Homard")
71 #========================================================================
72 #========================================================================
73 def geom_smesh_exec(theStudy):
75 Python script for GEOM and SMESH
81 geompy = geomBuilder.New(theStudy)
85 BOX = geompy.MakeBoxDXDYDZ(DX, DY, DZ, "BOX")
87 # Creation of the mesh
88 # ====================
89 smesh = smeshBuilder.New(theStudy)
90 MESH = smesh.Mesh(BOX)
91 smesh.SetName(MESH.GetMesh(), 'MESH')
93 # Creation of the hypotheses
94 # ==========================
95 Regular_1D = MESH.Segment()
96 smesh.SetName(Regular_1D.GetAlgorithm(), 'Regular_1D')
97 Length = min(DX, DY, DZ) / 5.
98 Local_Length = Regular_1D.LocalLength(Length,None,1e-07)
99 smesh.SetName(Local_Length, 'Local Length')
101 Quadrangle_2D = MESH.Quadrangle(algo=smeshBuilder.QUADRANGLE)
102 smesh.SetName(Quadrangle_2D.GetAlgorithm(), 'Quadrangle_2D')
103 Quadrangle_Parameters = Quadrangle_2D.QuadrangleParameters(StdMeshersBuilder.QUAD_STANDARD,-1,[],[])
104 smesh.SetName(Quadrangle_Parameters, 'Quadrangle Parameters')
106 Hexa_3D = MESH.Hexahedron(algo=smeshBuilder.Hexa)
107 smesh.SetName(Hexa_3D.GetAlgorithm(), 'Hexa_3D')
112 isDone = MESH.Compute()
121 ficmed = os.path.join(dircase, 'maill.00.med')
122 MESH.ExportMED( ficmed, 0, SMESH.MED_V2_2, 1, None ,1)
124 raise Exception('ExportToMEDX() failed. '+e.message)
131 #========================================================================
133 #========================================================================
134 def field_exec(theStudy, niter):
136 Python script for MEDCoupling
144 ficmed = os.path.join(dircase, 'maill.%02d.med' % niter)
145 meshMEDFileRead = ml.MEDFileMesh.New(ficmed)
146 meshRead0 = meshMEDFileRead.getMeshAtLevel(0)
147 # Valeurs of the field
148 # ====================
149 nbNodes = meshRead0.getNumberOfNodes()
150 valeur = mc.DataArrayDouble(nbNodes)
151 for iaux, taux in enumerate(meshRead0.getCoords()) :
152 #ligne = "x = %f" % taux[0]
153 #ligne += ", y = %f" % taux[1]
154 #ligne += ", z = %f" % taux[2]
156 #distance = (taux[0]-DX*0.2)**2 + (taux[1]-DY*0.2)**2 + (taux[2]-DZ*0.4)**2
157 distance = min(abs(taux[0]-DX*0.4), abs(taux[1]-DY*0.2), abs(taux[2]-DZ*0.4))
158 valeur[iaux] = 1.e0 / max ( 1.e-5, np.sqrt(distance) )
159 #print ". valeur", valeur
160 nparr = valeur.toNumPyArray()
161 print ". mini/maxi", nparr.min(), nparr.max()
163 # Creation of the field
164 # =====================
165 field = ml.MEDCouplingFieldDouble(ml.ON_NODES, ml.ONE_TIME)
166 field.setArray(valeur)
167 field.setMesh(meshRead0)
168 field.setName("DISTANCE")
170 fMEDFile_ch = ml.MEDFileField1TS()
171 fMEDFile_ch.setFieldNoProfileSBT(field) # No profile desired on the field, Sort By Type
172 fMEDFile_ch.write(ficmed, 0) # 0 to indicate that we *append* (and no overwrite) to the MED file
178 #========================================================================
179 #========================================================================
180 def homard_exec(theStudy):
182 Python script for HOMARD
188 homard.SetCurrentStudy(theStudy)
190 # Creation of the zones
191 # =====================
193 epsilon = min(DX, DY, DZ) / 100.
194 # Creation of the box Zone_4_1
195 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)
197 # Creation of the sphere Zone_4_2
198 rayon = min(DX, DY, DZ) / 4.
199 Zone_4_2 = homard.CreateZoneSphere('Zone_4_2', DX/3., DY*0.3, DZ*0.6, rayon)
201 # Creation of the hypotheses
202 # ==========================
204 dico["1"] = "raffinement"
205 dico["-1"] = "deraffinement"
206 # Creation of the hypothesis Hypo_4_1
207 HypoName_1 = "Zone_1"
208 print "-------- Creation of the hypothesis", HypoName_1
209 Hypo_4_1 = homard.CreateHypothesis(HypoName_1)
210 Hypo_4_1.AddZone('Zone_4_1', 1)
211 Hypo_4_1.SetExtraOutput(2)
212 laux = Hypo_4_1.GetZones()
215 for iaux in range(nbzone) :
216 print HypoName_1, " : ", dico[laux[jaux+1]], "sur la zone", laux[jaux]
218 # Creation of the hypothesis Hypo_4_2
219 HypoName_2 = "Zone_2"
220 print "-------- Creation of the hypothesis", HypoName_2
221 Hypo_4_2 = homard.CreateHypothesis(HypoName_2)
222 Hypo_4_2.AddZone('Zone_4_2', 1)
223 Hypo_4_2.SetExtraOutput(2)
224 laux = Hypo_4_2.GetZones()
227 for iaux in range(nbzone) :
228 print HypoName_2, " : ", dico[laux[jaux+1]], "sur la zone", laux[jaux]
230 # Creation of the hypothesis DISTANCE INVERSE
231 HypoName_3 = "DISTANCE INVERSE"
232 print "-------- Creation of the hypothesis", HypoName_3
233 Hypo_4_3 = homard.CreateHypothesis(HypoName_3)
234 Hypo_4_3.SetField('DISTANCE')
235 Hypo_4_3.SetUseComp(0)
236 Hypo_4_3.SetRefinThr(1, 0.3)
237 Hypo_4_3.SetUnRefThr(1, 0.2)
238 Hypo_4_3.AddFieldInterp('DISTANCE')
239 Hypo_4_3.SetExtraOutput(2)
240 print HypoName_3, " : zones utilisées :", Hypo_4_3.GetZones()
241 print HypoName_3, " : champ utilisé :", Hypo_4_3.GetFieldName()
242 print HypoName_3, " : composantes utilisées :", Hypo_4_3.GetComps()
243 if ( len (Hypo_4_3.GetFieldName()) > 0 ) :
244 print ".. caractéristiques de l'adaptation :", Hypo_4_3.GetField()
245 print HypoName_3, " : champs interpolés :", Hypo_4_3.GetFieldInterps()
247 # Creation of the cases
248 # =====================
249 # Creation of the case
250 CaseName = "Case_" + Test_Name
251 print "-------- Creation of the case", CaseName
252 MeshFile = os.path.join(dircase, 'maill.00.med')
253 Case_test_4 = homard.CreateCase(CaseName, 'MESH', MeshFile)
254 Case_test_4.SetDirName(dircase)
255 Case_test_4.SetConfType(1)
257 # Creation of the iterations
258 # ==========================
259 # Creation of the iteration 1
260 IterName = "I_" + Test_Name + "_1"
261 print "-------- Creation of the iteration", IterName
262 Iter_test_4_1 = Case_test_4.NextIteration(IterName)
263 Iter_test_4_1.AssociateHypo(HypoName_1)
264 print ". Hypothese :", HypoName_1
265 Iter_test_4_1.SetMeshName('M1')
266 Iter_test_4_1.SetMeshFile(os.path.join(dircase, 'maill.01.med'))
267 error = Iter_test_4_1.Compute(1, 2)
272 # Creation of the iteration 2
273 IterName = "I_" + Test_Name + "_2"
274 print "-------- Creation of the iteration", IterName
275 Iter_test_4_2 = Iter_test_4_1.NextIteration(IterName)
276 Iter_test_4_2.AssociateHypo(HypoName_2)
277 print ". Hypothese :", HypoName_2
278 Iter_test_4_2.SetMeshName('M2')
279 Iter_test_4_2.SetMeshFile(os.path.join(dircase, 'maill.02.med'))
280 error = Iter_test_4_2.Compute(1, 2)
285 # Creation of the iteration 3
287 error = field_exec(theStudy, 2)
292 IterName = "I_" + Test_Name + "_3"
293 print "-------- Creation of the iteration", IterName
294 Iter_test_4_3 = Iter_test_4_2.NextIteration(IterName)
295 Iter_test_4_3.AssociateHypo(HypoName_3)
296 print ". Hypothese :", HypoName_3
297 Iter_test_4_3.SetMeshName('M3')
298 Iter_test_4_3.SetFieldFile(os.path.join(dircase, 'maill.02.med'))
299 Iter_test_4_3.SetMeshFile(os.path.join(dircase, 'maill.03.med'))
300 error = Iter_test_4_3.Compute(1, 2)
309 #========================================================================
314 error_main = geom_smesh_exec(salome.myStudy)
316 raise Exception('Pb in geom_smesh_exec')
318 raise Exception('Pb in geom_smesh_exec: '+e.message)
320 homard = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
321 assert homard is not None, "Impossible to load homard engine"
322 homard.SetLanguageShort("fr")
324 # Exec of HOMARD-SALOME
327 error_main = homard_exec(salome.myStudy)
329 raise Exception('Pb in homard_exec at iteration %d' %error_main )
331 raise Exception('Pb in homard_exec: '+e.message)
333 # Test of the results
335 n_rep_test_file = n_iter_test_file
336 test_results(Rep_Test, Test_Name, dircase, n_iter_test_file, n_rep_test_file)
338 if salome.sg.hasDesktop():
339 salome.sg.updateObjBrowser(1)
340 iparameters.getSession().restoreVisualState(1)