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
25 __revision__ = "V2.03"
27 #========================================================================
39 #========================================================================
45 import MEDCoupling as mc
46 import MEDLoader as ml
48 # ==================================
49 PATH_HOMARD = os.getenv('HOMARD_ROOT_DIR')
50 # Repertoire des scripts utilitaires
51 REP_PYTHON = os.path.join(PATH_HOMARD, "bin", "salome", "test", "HOMARD")
52 REP_PYTHON = os.path.normpath(REP_PYTHON)
53 sys.path.append(REP_PYTHON)
54 from test_util import get_dir
55 from test_util import test_results
56 # ==================================
57 # Répertoires pour ce test
58 REP_DATA, DIRCASE = get_dir(PATH_HOMARD, TEST_NAME, DEBUG)
59 # ==================================
63 from MEDCouplingRemapper import MEDCouplingRemapper
66 IPAR = iparameters.IParameters(salome.myStudy.GetCommonParameters("Interface Applicative", 1))
67 IPAR.append("AP_MODULES_LIST", "Homard")
69 #========================================================================
70 #========================================================================
71 def mesh_exec(theStudy):
79 # Creation of the mesh
80 # ====================
81 maillage_3d = ml.MEDCouplingUMesh(MESH_NAME, 2)
82 maillage_3d.setMeshDimension(3)
84 # Creation of the nodes
85 # ====================
91 delta_x = LG_X / float(NBCELL_X)
92 delta_y = LG_Y / float(NBCELL_Y)
93 delta_z = LG_Z / float(NBCELL_Z)
97 for _ in range(nbno_z) :
99 for _ in range(nbno_y) :
101 for _ in range(nbno_x) :
102 coordinates.append(coo_x)
103 coordinates.append(coo_y)
104 coordinates.append(coo_z)
109 nbr_nodes = nbno_x*nbno_y*nbno_z
110 les_coords = ml.DataArrayDouble(coordinates, nbr_nodes, 3)
111 maillage_3d.setCoords(les_coords)
113 # Creation of the cells
114 # =====================
116 nbr_cell_3d = NBCELL_X*NBCELL_Y*NBCELL_Z
117 maillage_3d.allocateCells(nbr_cell_3d)
119 decala_z = nbno_x*nbno_y
120 # kaux = numero de la tranche en z
121 for kaux in range(1, nbno_z) :
123 #print ". Tranche en z numero %d" % kaux
124 decala = decala_z*(kaux-1)
125 # jaux = numero de la tranche en y
126 for jaux in range(1, nbno_y) :
128 #print ". Tranche en y numero %d" % jaux
129 # iaux = numero de la tranche en x
130 for iaux in range(1, nbno_x) :
132 #print ". Tranche en x numero %d" % iaux
134 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]
135 #if self.verbose_max :
136 #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) ) ) :
137 #print ". Maille %d : " % (iaux*jaux*kaux), laux
138 maillage_3d.insertNextCell(ml.NORM_HEXA8, 8, laux)
142 maillage_3d.finishInsertingCells()
144 # Agregation into a structure of MEDLoader
145 # ========================================
147 meshMEDFile3D = ml.MEDFileUMesh()
148 meshMEDFile3D.setName(MESH_NAME)
150 meshMEDFile3D.setMeshAtLevel(0, maillage_3d)
152 meshMEDFile3D.rearrangeFamilies()
158 ficmed = os.path.join(DIRCASE, 'maill.00.med')
159 #print "Ecriture du maillage dans le fichier", ficmed
160 meshMEDFile3D.write(ficmed, 2)
161 except IOError as eee:
163 raise Exception('ExportToMEDX() failed. '+str(eee.message))
169 #========================================================================
171 #========================================================================
172 def field_exec(theStudy, niter):
174 Python script for MEDCoupling
182 ficmed = os.path.join(DIRCASE, 'maill.%02d.med' % niter)
183 meshMEDFileRead = ml.MEDFileMesh.New(ficmed)
184 mesh_read0 = meshMEDFileRead.getMeshAtLevel(0)
185 # Barycenter of the cells
186 # =======================
187 cg_hexa_ml = mesh_read0.computeIsoBarycenterOfNodesPerCell()
188 cg_hexa = cg_hexa_ml.toNumPyArray()
191 xyz_p = np.zeros(3, dtype=np.float)
192 xyz_p[0] = -0.20*float(1-niter) * LG_X
193 xyz_p[1] = -0.15*float(1-niter) * LG_Y
194 xyz_p[2] = -0.10*float(1-niter) * LG_Z
195 # Values of the field
196 # ===================
197 nbr_cell_3d = mesh_read0.getNumberOfCells()
198 valeur = mc.DataArrayDouble(nbr_cell_3d)
199 for num_mail in range(nbr_cell_3d) :
200 #ligne = "x = %f" % cg_hexa[num_mail][0]
201 #ligne += ", y = %f" % cg_hexa[num_mail][1]
202 #ligne += ", z = %f" % cg_hexa[num_mail][2]
204 distance = np.linalg.norm(cg_hexa[num_mail]-xyz_p)
205 valeur[num_mail] = 1.e0 / max ( 1.e-5, distance)
206 #print ". valeur", valeur
207 nparr = valeur.toNumPyArray()
208 print(". mini/maxi", nparr.min(), nparr.max())
210 # Creation of the field
211 # =====================
212 field = ml.MEDCouplingFieldDouble(ml.ON_CELLS, ml.ONE_TIME)
213 field.setArray(valeur)
214 field.setMesh(mesh_read0)
215 field.setName("DISTANCE")
217 fMEDFile_ch = ml.MEDFileField1TS()
218 fMEDFile_ch.setFieldNoProfileSBT(field) # No profile desired on the field, Sort By Type
219 fMEDFile_ch.write(ficmed, 0) # 0 to indicate that we *append* (and no overwrite) to the MED file
225 #========================================================================
226 #========================================================================
227 def homard_exec(theStudy):
229 Python script for HOMARD
235 HOMARD.SetCurrentStudy(theStudy)
237 # Creation of the hypothese DISTANCE INVERSE
238 # ==========================================
239 hyponame = "DISTANCE INVERSE"
240 print("-------- Creation of the hypothesis", hyponame)
241 hypo_5 = HOMARD.CreateHypothesis(hyponame)
242 hypo_5.SetField('DISTANCE')
244 hypo_5.SetRefinThr(1, 0.020)
245 hypo_5.SetUnRefThr(1, 0.015)
246 print(hyponame, " : champ utilisé :", hypo_5.GetFieldName())
247 print(".. caractéristiques de l'adaptation :", hypo_5.GetField())
249 # Creation of the cases
250 # =====================
251 # Creation of the case
252 print("-------- Creation of the case", TEST_NAME)
253 mesh_file = os.path.join(DIRCASE, 'maill.00.med')
254 case_test_5 = HOMARD.CreateCase(TEST_NAME, 'MESH', mesh_file)
255 case_test_5.SetDirName(DIRCASE)
256 case_test_5.SetConfType(1)
257 case_test_5.SetExtType(1)
259 # Creation of the iterations
260 # ==========================
262 for niter in range(N_ITER_TEST_FILE) :
264 s_niterp1 = "%02d" % (niter + 1)
266 # Creation of the indicator
268 error, ficmed_indic = field_exec(theStudy, niter)
273 # Creation of the iteration
275 iter_name = "I_" + TEST_NAME + "_" + s_niterp1
276 print("-------- Creation of the iteration", iter_name)
278 iter_test_5 = case_test_5.NextIteration(iter_name)
280 iter_test_5 = iter_test_5.NextIteration(iter_name)
281 iter_test_5.AssociateHypo(hyponame)
282 iter_test_5.SetFieldFile(ficmed_indic)
283 iter_test_5.SetMeshName(MESH_NAME+"_" + s_niterp1)
284 iter_test_5.SetMeshFile(os.path.join(DIRCASE, "maill."+s_niterp1+".med"))
285 error = iter_test_5.Compute(1, 2)
294 #========================================================================
299 ERROR = mesh_exec(salome.myStudy)
301 raise Exception('Pb in mesh_exec')
302 except RuntimeError as eee:
303 raise Exception('Pb in mesh_exec: '+str(eee.message))
305 HOMARD = salome.lcc.FindOrLoadComponent('FactoryServer', 'HOMARD')
306 assert HOMARD is not None, "Impossible to load homard engine"
307 HOMARD.SetLanguageShort("fr")
309 # Exec of HOMARD-SALOME
312 ERROR = homard_exec(salome.myStudy)
314 raise Exception('Pb in homard_exec at iteration %d' %ERROR )
315 except RuntimeError as eee:
316 raise Exception('Pb in homard_exec: '+str(eee.message))
318 # Test of the results
320 N_REP_TEST_FILE = N_ITER_TEST_FILE
321 DESTROY_DIR = not DEBUG
322 test_results(REP_DATA, TEST_NAME, DIRCASE, N_ITER_TEST_FILE, N_REP_TEST_FILE, DESTROY_DIR)
324 if salome.sg.hasDesktop():
325 salome.sg.updateObjBrowser(True)
326 iparameters.getSession().restoreVisualState(1)