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 #========================================================================
39 #========================================================================
46 import MEDCoupling as mc
47 import MEDLoader as ml
49 # ==================================
50 PATH_HOMARD = os.getenv('HOMARD_ROOT_DIR')
51 # Repertoire des scripts utilitaires
52 REP_PYTHON = os.path.join(PATH_HOMARD, "bin", "salome", "test", "HOMARD")
53 REP_PYTHON = os.path.normpath(REP_PYTHON)
54 sys.path.append(REP_PYTHON)
55 from test_util import remove_dir
56 from test_util import test_results
57 # Repertoire des donnees du test
58 REP_DATA = os.path.join(PATH_HOMARD, "share", "salome", "homardsamples")
59 REP_DATA = os.path.normpath(REP_DATA)
60 # Repertoire des resultats
62 DIRCASE = os.path.join("/tmp", TEST_NAME)
63 if ( os.path.isdir(DIRCASE) ) :
67 DIRCASE = tempfile.mkdtemp()
68 # ==================================
72 from MEDCouplingRemapper import MEDCouplingRemapper
75 IPAR = iparameters.IParameters(salome.myStudy.GetCommonParameters("Interface Applicative", 1))
76 IPAR.append("AP_MODULES_LIST", "Homard")
78 #========================================================================
79 #========================================================================
88 # Creation of the mesh
89 # ====================
90 maillage_3d = ml.MEDCouplingUMesh(MESH_NAME, 2)
91 maillage_3d.setMeshDimension(3)
93 # Creation of the nodes
94 # ====================
100 delta_x = LG_X / float(NBCELL_X)
101 delta_y = LG_Y / float(NBCELL_Y)
102 delta_z = LG_Z / float(NBCELL_Z)
106 for kaux in range(nbno_z) :
108 for jaux in range(nbno_y) :
110 for iaux in range(nbno_x) :
111 coordinates.append(coo_x)
112 coordinates.append(coo_y)
113 coordinates.append(coo_z)
118 nbr_nodes = nbno_x*nbno_y*nbno_z
119 les_coords = ml.DataArrayDouble(coordinates, nbr_nodes, 3)
120 maillage_3d.setCoords(les_coords)
122 # Creation of the cells
123 # =====================
125 nbr_cell_3d = NBCELL_X*NBCELL_Y*NBCELL_Z
126 maillage_3d.allocateCells(nbr_cell_3d)
128 decala_z = nbno_x*nbno_y
129 # kaux = numero de la tranche en z
130 for kaux in range(1, nbno_z) :
132 #print ". Tranche en z numero %d" % kaux
133 decala = decala_z*(kaux-1)
134 # jaux = numero de la tranche en y
135 for jaux in range(1, nbno_y) :
137 #print ". Tranche en y numero %d" % jaux
138 # iaux = numero de la tranche en x
139 for iaux in range(1, nbno_x) :
141 #print ". Tranche en x numero %d" % iaux
143 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]
144 #if self.verbose_max :
145 #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) ) ) :
146 #print ". Maille %d : " % (iaux*jaux*kaux), laux
147 maillage_3d.insertNextCell(ml.NORM_HEXA8, 8, laux)
151 maillage_3d.finishInsertingCells()
153 # Agregation into a structure of MEDLoader
154 # ========================================
156 meshMEDFile3D = ml.MEDFileUMesh()
157 meshMEDFile3D.setName(MESH_NAME)
159 meshMEDFile3D.setMeshAtLevel(0, maillage_3d)
161 meshMEDFile3D.rearrangeFamilies()
167 ficmed = os.path.join(DIRCASE, 'maill.00.med')
168 #print "Ecriture du maillage dans le fichier", ficmed
169 meshMEDFile3D.write(ficmed, 2)
170 except Exception as eee:
172 raise Exception('MEDFileUMesh.write() failed. ' + str(eee))
178 #========================================================================
180 #========================================================================
181 def field_exec(niter):
183 Python script for MEDCoupling
191 ficmed = os.path.join(DIRCASE, 'maill.%02d.med' % niter)
192 meshMEDFileRead = ml.MEDFileMesh.New(ficmed)
193 mesh_read0 = meshMEDFileRead.getMeshAtLevel(0)
194 # Barycenter of the cells
195 # =======================
196 cg_hexa_ml = mesh_read0.computeIsoBarycenterOfNodesPerCell()
197 cg_hexa = cg_hexa_ml.toNumPyArray()
200 xyz_p = np.zeros(3, dtype=np.float)
201 xyz_p[0] = -0.20*float(1-niter) * LG_X
202 xyz_p[1] = -0.15*float(1-niter) * LG_Y
203 xyz_p[2] = -0.10*float(1-niter) * LG_Z
204 # Values of the field
205 # ===================
206 nbr_cell_3d = mesh_read0.getNumberOfCells()
207 valeur = mc.DataArrayDouble(nbr_cell_3d)
208 for num_mail in range(nbr_cell_3d) :
209 #ligne = "x = %f" % cg_hexa[num_mail][0]
210 #ligne += ", y = %f" % cg_hexa[num_mail][1]
211 #ligne += ", z = %f" % cg_hexa[num_mail][2]
213 distance = np.linalg.norm(cg_hexa[num_mail]-xyz_p)
214 valeur[num_mail] = 1.e0 / max ( 1.e-5, distance)
215 #print ". valeur", valeur
216 nparr = valeur.toNumPyArray()
217 print(". mini/maxi", nparr.min(), nparr.max())
219 # Creation of the field
220 # =====================
221 field = ml.MEDCouplingFieldDouble(ml.ON_CELLS, ml.ONE_TIME)
222 field.setArray(valeur)
223 field.setMesh(mesh_read0)
224 field.setName("DISTANCE")
226 fMEDFile_ch = ml.MEDFileField1TS()
227 fMEDFile_ch.setFieldNoProfileSBT(field) # No profile desired on the field, Sort By Type
228 fMEDFile_ch.write(ficmed, 0) # 0 to indicate that we *append* (and no overwrite) to the MED file
234 #========================================================================
235 #========================================================================
238 Python script for HOMARD
246 # Creation of the hypothese DISTANCE INVERSE
247 # ==========================================
248 hyponame = "DISTANCE INVERSE"
249 print("-------- Creation of the hypothesis", hyponame)
250 hypo_5 = HOMARD.CreateHypothesis(hyponame)
251 hypo_5.SetField('DISTANCE')
253 hypo_5.SetRefinThr(1, 0.020)
254 hypo_5.SetUnRefThr(1, 0.015)
255 print(hyponame, " : champ utilisé :", hypo_5.GetFieldName())
256 print(".. caractéristiques de l'adaptation :", hypo_5.GetField())
258 # Creation of the cases
259 # =====================
260 # Creation of the case
261 print("-------- Creation of the case", TEST_NAME)
262 mesh_file = os.path.join(DIRCASE, 'maill.00.med')
263 case_test_5 = HOMARD.CreateCase(TEST_NAME, 'MESH', mesh_file)
264 case_test_5.SetDirName(DIRCASE)
265 case_test_5.SetConfType(1)
266 case_test_5.SetExtType(1)
268 # Creation of the iterations
269 # ==========================
271 for niter in range(N_ITER_TEST_FILE) :
273 s_niterp1 = "%02d" % (niter + 1)
275 # Creation of the indicator
277 error, ficmed_indic = field_exec(niter)
282 # Creation of the iteration
284 iter_name = "I_" + TEST_NAME + "_" + s_niterp1
285 print("-------- Creation of the iteration", iter_name)
287 iter_test_5 = case_test_5.NextIteration(iter_name)
289 iter_test_5 = iter_test_5.NextIteration(iter_name)
290 iter_test_5.AssociateHypo(hyponame)
291 iter_test_5.SetFieldFile(ficmed_indic)
292 iter_test_5.SetMeshName(MESH_NAME+"_" + s_niterp1)
293 iter_test_5.SetMeshFile(os.path.join(DIRCASE, "maill."+s_niterp1+".med"))
294 error = iter_test_5.Compute(1, 2)
303 #========================================================================
310 raise Exception('Pb in mesh_exec')
311 except Exception as eee:
312 raise Exception('Pb in mesh_exec: ' + str(eee))
314 HOMARD = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
315 assert HOMARD is not None, "Impossible to load homard engine"
316 HOMARD.SetLanguageShort("fr")
318 # Exec of HOMARD-SALOME
321 ERROR = homard_exec()
323 raise Exception('Pb in homard_exec at iteration %d' %ERROR )
324 except Exception as eee:
325 raise Exception('Pb in homard_exec: ' + str(eee))
327 # Test of the results
329 N_REP_TEST_FILE = N_ITER_TEST_FILE
330 DESTROY_DIR = not DEBUG
331 test_results(REP_DATA, TEST_NAME, DIRCASE, N_ITER_TEST_FILE, N_REP_TEST_FILE, DESTROY_DIR)
333 if salome.sg.hasDesktop():
334 salome.sg.updateObjBrowser()
335 iparameters.getSession().restoreVisualState(1)