1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2021 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
25 __revision__ = "V2.05"
33 import medcoupling as mc
34 import MEDLoader as ml
36 # ==================================
37 PATH_HOMARD = os.getenv('HOMARD_ROOT_DIR')
38 # Repertoire des scripts utilitaires
39 REP_PYTHON = os.path.join(PATH_HOMARD, "bin", "salome", "test", "HOMARD")
40 REP_PYTHON = os.path.normpath(REP_PYTHON)
41 sys.path.append(REP_PYTHON)
42 from test_util import get_dir
43 from test_util import test_results
44 # ==================================
46 #========================================================================
58 # Répertoires pour ce test
59 REP_DATA, DIRCASE = get_dir(PATH_HOMARD, TEST_NAME, DEBUG)
60 #========================================================================
64 #========================================================================
66 """Python script for MED"""
71 # Creation of the mesh
72 # ====================
73 maillage_3d = ml.MEDCouplingUMesh(MESH_NAME, 2)
74 maillage_3d.setMeshDimension(3)
76 # Creation of the nodes
77 # ====================
83 delta_x = LG_X / float(NBCELL_X)
84 delta_y = LG_Y / float(NBCELL_Y)
85 delta_z = LG_Z / float(NBCELL_Z)
89 for _ in range(nbno_z) :
91 for _ in range(nbno_y) :
93 for _ in range(nbno_x) :
94 coordinates.append(coo_x)
95 coordinates.append(coo_y)
96 coordinates.append(coo_z)
101 nbno = nbno_x*nbno_y*nbno_z
102 les_coords = ml.DataArrayDouble(coordinates, nbno, 3)
103 maillage_3d.setCoords(les_coords)
105 # Creation of the cells
106 # =====================
108 nbr_cell_3d = NBCELL_X*NBCELL_Y*NBCELL_Z
109 maillage_3d.allocateCells(nbr_cell_3d)
111 decala_z = nbno_x*nbno_y
112 # kaux = numero de la tranche en z
113 for kaux in range(1, nbno_z) :
115 #print ". Tranche en z numero %d" % kaux
116 decala = decala_z*(kaux-1)
117 # jaux = numero de la tranche en y
118 for jaux in range(1, nbno_y) :
120 #print ". Tranche en y numero %d" % jaux
121 # iaux = numero de la tranche en x
122 for iaux in range(1, nbno_x) :
124 #print ". Tranche en x numero %d" % iaux
126 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]
128 if ( ( iaux==1 and jaux==1 and kaux==1 ) or ( iaux==(nbno_x-1) and jaux==(nbno_y-1) and kaux==(nbno_z-1) ) ) :
129 print (". Maille {} : {}".format((iaux*jaux*kaux),laux))
130 maillage_3d.insertNextCell(ml.NORM_HEXA8, 8, laux)
134 maillage_3d.finishInsertingCells()
136 # Agregation into a structure of MEDLoader
137 # ========================================
139 meshMEDFile3D = ml.MEDFileUMesh()
140 meshMEDFile3D.setName(MESH_NAME)
142 meshMEDFile3D.setMeshAtLevel(0, maillage_3d)
144 meshMEDFile3D.rearrangeFamilies()
149 ficmed = os.path.join(DIRCASE, 'maill.00.med')
150 #print "Ecriture du maillage dans le fichier", ficmed
151 meshMEDFile3D.write(ficmed, 2)
152 except IOError as eee:
154 raise Exception('MEDFileUMesh.write() failed. ' + str(eee))
160 #========================================================================
162 #========================================================================
163 def field_exec(niter):
165 Python script for MEDCoupling
173 ficmed = os.path.join(DIRCASE, 'maill.%02d.med' % niter)
174 meshMEDFileRead = ml.MEDFileMesh.New(ficmed)
175 mesh_read0 = meshMEDFileRead.getMeshAtLevel(0)
176 # Barycenter of the cells
177 # =======================
178 cg_hexa_ml = mesh_read0.computeIsoBarycenterOfNodesPerCell()
179 cg_hexa = cg_hexa_ml.toNumPyArray()
182 xyz_p = np.zeros(3, dtype=np.float)
183 xyz_p[0] = -0.20*float(1-niter) * LG_X
184 xyz_p[1] = -0.15*float(1-niter) * LG_Y
185 xyz_p[2] = -0.10*float(1-niter) * LG_Z
186 # Values of the field
187 # ===================
188 nbr_cell_3d = mesh_read0.getNumberOfCells()
189 valeur = mc.DataArrayDouble(nbr_cell_3d)
190 for num_mail in range(nbr_cell_3d) :
191 #ligne = "x = %f" % cg_hexa[num_mail][0]
192 #ligne += ", y = %f" % cg_hexa[num_mail][1]
193 #ligne += ", z = %f" % cg_hexa[num_mail][2]
195 distance = np.linalg.norm(cg_hexa[num_mail]-xyz_p)
196 valeur[num_mail] = 1.e0 / max ( 1.e-5, distance)
197 #print ". valeur", valeur
198 nparr = valeur.toNumPyArray()
199 print(". mini/maxi {}/{}".format(nparr.min(),nparr.max()))
201 # Creation of the field
202 # =====================
203 field = ml.MEDCouplingFieldDouble(ml.ON_CELLS, ml.ONE_TIME)
204 field.setArray(valeur)
205 field.setMesh(mesh_read0)
206 field.setName("DISTANCE")
208 fMEDFile_ch = ml.MEDFileField1TS()
209 fMEDFile_ch.setFieldNoProfileSBT(field) # No profile desired on the field, Sort By Type
210 fMEDFile_ch.write(ficmed, 0) # 0 to indicate that we *append* (and no overwrite) to the MED file
216 #========================================================================
218 #========================================================================
220 """Python script for HOMARD"""
225 # Creation of the hypothese DISTANCE INVERSE
226 # ==========================================
227 hyponame = "DISTANCE INVERSE"
228 print("-------- Creation of the hypothesis", hyponame)
229 hypo_5 = HOMARD.CreateHypothesis(hyponame)
230 hypo_5.SetField('DISTANCE')
232 hypo_5.SetRefinThr(1, 0.020)
233 hypo_5.SetUnRefThr(1, 0.015)
234 print(hyponame, " : champ utilisé :", hypo_5.GetFieldName())
235 print(".. caractéristiques de l'adaptation :", hypo_5.GetField())
237 # Creation of the cases
238 # =====================
239 print("-------- Creation of the case", TEST_NAME)
240 mesh_file = os.path.join(DIRCASE, 'maill.00.med')
241 case_test_5 = HOMARD.CreateCase(TEST_NAME, 'MESH', mesh_file)
242 case_test_5.SetDirName(DIRCASE)
243 case_test_5.SetConfType(1)
244 case_test_5.SetExtType(1)
246 # Creation of the iterations
247 # ==========================
249 for niter in range(N_ITER_TEST_FILE) :
251 s_niterp1 = "%02d" % (niter + 1)
253 # Creation of the indicator
255 error, ficmed_indic = field_exec(niter)
260 # Creation of the iteration
262 iter_name = "I_" + TEST_NAME + "_" + s_niterp1
263 print("-------- Creation of the iteration", iter_name)
265 iter_test_5 = case_test_5.NextIteration(iter_name)
267 iter_test_5 = iter_test_5.NextIteration(iter_name)
268 iter_test_5.AssociateHypo(hyponame)
269 iter_test_5.SetFieldFile(ficmed_indic)
270 iter_test_5.SetMeshName(MESH_NAME+"_" + s_niterp1)
271 iter_test_5.SetMeshFile(os.path.join(DIRCASE, "maill."+s_niterp1+".med"))
272 error = iter_test_5.Compute(1, 2)
281 #========================================================================
288 raise Exception('Pb in mesh_exec')
289 except RuntimeError as eee:
290 raise Exception('Pb in mesh_exec: '+str(eee.message))
292 HOMARD = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
293 assert HOMARD is not None, "Impossible to load homard engine"
294 HOMARD.SetLanguageShort("fr")
296 # Exec of HOMARD-SALOME
299 ERROR = homard_exec()
301 raise Exception('Pb in homard_exec at iteration %d' %ERROR )
302 except RuntimeError as eee:
303 raise Exception('Pb in homard_exec: '+str(eee.message))
305 # Test of the results
307 N_REP_TEST_FILE = N_ITER_TEST_FILE
308 DESTROY_DIR = not DEBUG
309 test_results(REP_DATA, TEST_NAME, DIRCASE, N_ITER_TEST_FILE, N_REP_TEST_FILE, DESTROY_DIR)
311 if salome.sg.hasDesktop():
312 salome.sg.updateObjBrowser()