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
22 Specific conditions for Code_Saturne
27 #========================================================================
40 #========================================================================
47 import MEDCoupling as mc
48 import MEDLoader as ml
50 # ==================================
51 PATH_HOMARD = os.getenv('HOMARD_ROOT_DIR')
52 # Repertoire des scripts utilitaires
53 REP_PYTHON = os.path.join(PATH_HOMARD, "bin", "salome", "test", "HOMARD")
54 REP_PYTHON = os.path.normpath(REP_PYTHON)
55 sys.path.append(REP_PYTHON)
56 from test_util import remove_dir
57 from test_util import test_results
58 # Repertoire des donnees du test
59 REP_DATA = os.path.join(PATH_HOMARD, "share", "salome", "homardsamples")
60 REP_DATA = os.path.normpath(REP_DATA)
61 # Repertoire des resultats
63 DIRCASE = os.path.join("/tmp", TEST_NAME)
64 if ( os.path.isdir(DIRCASE) ) :
68 DIRCASE = tempfile.mkdtemp()
69 # ==================================
73 from MEDCouplingRemapper import MEDCouplingRemapper
76 IPAR = iparameters.IParameters(salome.myStudy.GetCommonParameters("Interface Applicative", 1))
77 IPAR.append("AP_MODULES_LIST", "Homard")
79 #========================================================================
80 #========================================================================
81 def mesh_exec(theStudy):
89 # Creation of the mesh
90 # ====================
91 maillage_3d = ml.MEDCouplingUMesh(MESH_NAME, 2)
92 maillage_3d.setMeshDimension(3)
94 # Creation of the nodes
95 # ====================
101 delta_x = LG_X / float(NBCELL_X)
102 delta_y = LG_Y / float(NBCELL_Y)
103 delta_z = LG_Z / float(NBCELL_Z)
107 for kaux in range(nbno_z) :
109 for jaux in range(nbno_y) :
111 for iaux in range(nbno_x) :
112 coordinates.append(coo_x)
113 coordinates.append(coo_y)
114 coordinates.append(coo_z)
119 nbr_nodes = nbno_x*nbno_y*nbno_z
120 les_coords = ml.DataArrayDouble(coordinates, nbr_nodes, 3)
121 maillage_3d.setCoords(les_coords)
123 # Creation of the cells
124 # =====================
126 nbr_cell_3d = NBCELL_X*NBCELL_Y*NBCELL_Z
127 maillage_3d.allocateCells(nbr_cell_3d)
129 decala_z = nbno_x*nbno_y
130 # kaux = numero de la tranche en z
131 for kaux in range(1, nbno_z) :
133 #print ". Tranche en z numero %d" % kaux
134 decala = decala_z*(kaux-1)
135 # jaux = numero de la tranche en y
136 for jaux in range(1, nbno_y) :
138 #print ". Tranche en y numero %d" % jaux
139 # iaux = numero de la tranche en x
140 for iaux in range(1, nbno_x) :
142 #print ". Tranche en x numero %d" % iaux
144 laux = [nref, nref+nbno_x, nref+1+nbno_x, nref+1, nref+decala_z, nref+nbno_x+decala_z, nref+1+nbno_x+decala_z, nref+1+decala_z]
145 #if self.verbose_max :
146 #if ( ( iaux==1 and jaux==1 and kaux==1 ) or ( iaux==(nbr_nodes_x-1) and jaux==(nbr_nodes_y-1) and kaux==(nbr_nodes_z-1) ) ) :
147 #print ". Maille %d : " % (iaux*jaux*kaux), laux
148 maillage_3d.insertNextCell(ml.NORM_HEXA8, 8, laux)
152 maillage_3d.finishInsertingCells()
154 # Agregation into a structure of MEDLoader
155 # ========================================
157 meshMEDFile3D = ml.MEDFileUMesh()
158 meshMEDFile3D.setName(MESH_NAME)
160 meshMEDFile3D.setMeshAtLevel(0, maillage_3d)
162 meshMEDFile3D.rearrangeFamilies()
168 ficmed = os.path.join(DIRCASE, 'maill.00.med')
169 #print "Ecriture du maillage dans le fichier", ficmed
170 meshMEDFile3D.write(ficmed, 2)
171 except Exception, eee:
173 raise Exception('ExportToMEDX() failed. '+eee.message)
179 #========================================================================
181 #========================================================================
182 def field_exec(theStudy, niter):
184 Python script for MEDCoupling
192 ficmed = os.path.join(DIRCASE, 'maill.%02d.med' % niter)
193 meshMEDFileRead = ml.MEDFileMesh.New(ficmed)
194 mesh_read0 = meshMEDFileRead.getMeshAtLevel(0)
195 # Barycenter of the cells
196 # =======================
197 cg_hexa_ml = mesh_read0.computeIsoBarycenterOfNodesPerCell()
198 cg_hexa = cg_hexa_ml.toNumPyArray()
201 xyz_p = np.zeros(3, dtype=np.float)
202 xyz_p[0] = -0.20*float(1-niter) * LG_X
203 xyz_p[1] = -0.15*float(1-niter) * LG_Y
204 xyz_p[2] = -0.10*float(1-niter) * LG_Z
205 # Values of the field
206 # ===================
207 nbr_cell_3d = mesh_read0.getNumberOfCells()
208 valeur = mc.DataArrayDouble(nbr_cell_3d)
209 for num_mail in range(nbr_cell_3d) :
210 #ligne = "x = %f" % cg_hexa[num_mail][0]
211 #ligne += ", y = %f" % cg_hexa[num_mail][1]
212 #ligne += ", z = %f" % cg_hexa[num_mail][2]
214 distance = np.linalg.norm(cg_hexa[num_mail]-xyz_p)
215 valeur[num_mail] = 1.e0 / max ( 1.e-5, distance)
216 #print ". valeur", valeur
217 nparr = valeur.toNumPyArray()
218 print ". mini/maxi", nparr.min(), nparr.max()
220 # Creation of the field
221 # =====================
222 field = ml.MEDCouplingFieldDouble(ml.ON_CELLS, ml.ONE_TIME)
223 field.setArray(valeur)
224 field.setMesh(mesh_read0)
225 field.setName("DISTANCE")
227 fMEDFile_ch = ml.MEDFileField1TS()
228 fMEDFile_ch.setFieldNoProfileSBT(field) # No profile desired on the field, Sort By Type
229 fMEDFile_ch.write(ficmed, 0) # 0 to indicate that we *append* (and no overwrite) to the MED file
235 #========================================================================
236 #========================================================================
237 def homard_exec(theStudy):
239 Python script for HOMARD
245 HOMARD.SetCurrentStudy(theStudy)
247 # Creation of the hypothese DISTANCE INVERSE
248 # ==========================================
249 hyponame = "DISTANCE INVERSE"
250 print "-------- Creation of the hypothesis", hyponame
251 hypo_5 = HOMARD.CreateHypothesis(hyponame)
252 hypo_5.SetField('DISTANCE')
254 hypo_5.SetRefinThr(1, 0.020)
255 hypo_5.SetUnRefThr(1, 0.015)
256 print hyponame, " : champ utilisé :", hypo_5.GetFieldName()
257 print ".. caractéristiques de l'adaptation :", hypo_5.GetField()
259 # Creation of the cases
260 # =====================
261 # Creation of the case
262 print "-------- Creation of the case", TEST_NAME
263 mesh_file = os.path.join(DIRCASE, 'maill.00.med')
264 case_test_5 = HOMARD.CreateCase(TEST_NAME, 'MESH', mesh_file)
265 case_test_5.SetDirName(DIRCASE)
266 case_test_5.SetConfType(1)
267 case_test_5.SetExtType(1)
269 # Creation of the iterations
270 # ==========================
272 for niter in range(N_ITER_TEST_FILE) :
274 s_niterp1 = "%02d" % (niter + 1)
276 # Creation of the indicator
278 error, ficmed_indic = field_exec(theStudy, niter)
283 # Creation of the iteration
285 iter_name = "I_" + TEST_NAME + "_" + s_niterp1
286 print "-------- Creation of the iteration", iter_name
288 iter_test_5 = case_test_5.NextIteration(iter_name)
290 iter_test_5 = iter_test_5.NextIteration(iter_name)
291 iter_test_5.AssociateHypo(hyponame)
292 iter_test_5.SetFieldFile(ficmed_indic)
293 iter_test_5.SetMeshName(MESH_NAME+"_" + s_niterp1)
294 iter_test_5.SetMeshFile(os.path.join(DIRCASE, "maill."+s_niterp1+".med"))
295 error = iter_test_5.Compute(1, 2)
304 #========================================================================
309 ERROR = mesh_exec(salome.myStudy)
311 raise Exception('Pb in mesh_exec')
312 except Exception, eee:
313 raise Exception('Pb in mesh_exec: '+eee.message)
315 HOMARD = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
316 assert HOMARD is not None, "Impossible to load homard engine"
317 HOMARD.SetLanguageShort("fr")
319 # Exec of HOMARD-SALOME
322 ERROR = homard_exec(salome.myStudy)
324 raise Exception('Pb in homard_exec at iteration %d' %ERROR )
325 except Exception, eee:
326 raise Exception('Pb in homard_exec: '+eee.message)
328 # Test of the results
330 N_REP_TEST_FILE = N_ITER_TEST_FILE
331 DESTROY_DIR = not DEBUG
332 test_results(REP_DATA, TEST_NAME, DIRCASE, N_ITER_TEST_FILE, N_REP_TEST_FILE, DESTROY_DIR)
334 if salome.sg.hasDesktop():
335 salome.sg.updateObjBrowser(1)
336 iparameters.getSession().restoreVisualState(1)