1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2017 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 Utilitaires pour les tests
24 __revision__ = "V3.01"
27 import MEDLoader as ml
29 #========================================================================
30 #========================================================================
31 def remove_dir(directory) :
33 Empties, then removes a directory.
34 Copyright EDF-R&D 2013
37 l_aux = os.listdir(directory)
39 fic_a = os.path.join(directory, fic)
40 if os.path.isdir(fic_a) :
48 #========================================================================
49 #========================================================================
50 def test_results(rep_test, test_name, dircase, n_iter_test_file, n_rep_test_file, destroy_dir = True) :
53 rep_test: repertoire des tests
54 test_name: nom du test
55 dircase: repertoire des resultats du test
56 n_iter_test_file: numero de l'iteration a tester
57 n_rep_test_file: numero du repertoire de l'iteration a tester
58 destroy_dir: destruction du repertoire de calcul
59 Copyright EDF-R&D 2014
62 test_file_suff = "apad.%02d.bilan" % n_iter_test_file
63 rep_test_file = "I%02d" % n_rep_test_file
65 test_file = os.path.join(rep_test, test_name + "." + test_file_suff)
66 mess_error_ref = "\nReference file: " + test_file
68 # Existence du fichier de référence
71 file = open (test_file, "r")
72 les_lignes_ref = file.readlines()
75 mess_error = mess_error_ref + "\nThis file does not exist.\n"
77 raise Exception(mess_error)
79 # Existence du fichier de l'exécution courante
81 test_file = os.path.join(dircase, rep_test_file, test_file_suff)
82 if os.path.isfile (test_file) :
83 file = open (test_file, "r")
84 les_lignes = file.readlines()
87 mess_error = "\nResult file: " + test_file
88 mess_error += "\nThis file does not exist.\n"
90 raise Exception(mess_error)
92 # Nombre de lignes identiques
94 nblign = len(les_lignes_ref)
95 if ( len(les_lignes) != nblign ):
96 mess_error = mess_error_ref + "\nResult file: " + test_file
97 mess_error += "\nThe number of lines of the files are not the same.\n"
99 raise Exception(mess_error)
101 # Comparaison des lignes, à l'esception de la date
103 for num in range(nblign) :
104 if ( "creation" not in les_lignes_ref[num] ) :
105 if ( les_lignes_ref[num] != les_lignes[num] ) :
106 message_erreur = "\nRefe : " + les_lignes_ref[num]
107 message_erreur += "Test : " + les_lignes[num][:-1]
108 message_erreur += "\nThe test is different from the reference."
110 raise Exception(message_erreur)
112 # Destruction éventuelle du répertoire du calcul
119 #========================================================================
120 #========================================================================
122 def saveGeometry( xao_file, name, author="" ):
124 Save the geometry in a XAO file
127 from salome.geom import geomBuilder
128 geompy = geomBuilder.New(salome.myStudy)
131 # find an object having groups in GEOM component
132 component = salome.myStudy.FindObjectByPath("/Geometry")
133 it = salome.myStudy.NewChildIterator( component )
142 if ( go.GetName() == name ) :
143 subIt = salome.myStudy.NewChildIterator( so )
145 subSO = subIt.Value()
147 if not subSO.GetName(): continue # a reference
148 gr = subSO.GetObject()
149 if gr and geompy.ShapeIdToType( gr.GetType() ) == "GROUP":
150 l_groups.append( gr )
155 raise RuntimeError("Cant find a geometry object in the SALOME study with name = '%s'" % name)
157 # save the geom object in a XAO file
159 ok = geompy.ExportXAO( geomObj, l_groups, l_fields, author, xao_file, "" )
164 #========================================================================
165 #========================================================================
167 def repositionnement (rep_calc, fic_med_brut, fic_med_new, xao_file, verbose=False) :
170 Pilote le repositionnement des noeuds qui ont bougé
172 rep_calc : répertoire du calcul HOMARD qui est à traiter
173 fic_med_brut : fichier MED du calcul avec les coordonnées avant projection
174 fic_med_new : fichier MED du calcul avec les coordonnées après projection
175 xao_file : fichier XAO de la géométrie
178 ligne = "rep_calc = %s" % rep_calc
179 ligne += "\nfic_med_brut = %s" % fic_med_brut
180 ligne += "\nfic_med_new = %s" % fic_med_new
181 ligne += "\nxao_file = %s" % xao_file
188 # 1. l_fr = liste des fichiers des lignes/surfaces a suivre
189 # Les fichiers des numéros de groupes par frontière sont renommés selon le support
190 # à condition de ne pas être vide.
193 laux = os.listdir(rep_calc)
200 if ( fic[:5] == 'fort.' ) :
201 fic_fort = os.path.join(rep_calc, fic)
202 fichier = open (fic_fort, "r")
203 les_lignes = fichier.readlines()
207 for ligne in les_lignes[1:] :
208 laux1 = ligne.split()
209 if ( len(laux1) >= 3 ) :
213 if ( "1D" in les_lignes[0] ) :
214 nomfic_bis = "fr1D.%02d" % icpt_1D
217 nomfic_bis = "fr2D.%02d" % icpt_2D
219 fic_1 = os.path.join(rep_calc, nomfic_bis)
220 fichier = open (fic_1, "w")
221 for ligne in les_lignes[1:] :
222 if ( ( "1D" not in ligne ) and ( "2D" not in ligne ) ) :
225 #print "\t\tajout de %s" % fic_1
227 elif ( fic[-4:] == '.med' ) :
228 fic_hom_med = os.path.join(rep_calc, fic)
229 #print "\t\treperage de fic_hom_med =", fic_hom_med
231 # 2. Lancement du post-traitement si des noeuds sont concernés
236 print("l_fr =", l_fr)
237 print("fic_hom_med =", fic_hom_med)
240 # . prend le maillage brut dans le fichier fic_med_brut
241 # . prend la liste des noeuds à bouger pour chaque groupe concerné
242 # . prend la géométrie dans le fichier xao_file
243 # . retourne le maillage transformé dans le fichier fic_med_new
245 from FrontTrack import FrontTrack
247 ft.track( fic_med_brut, fic_med_new, l_fr, xao_file )
249 # 2.2. Transfert des coordonnées dans le fichier HOMARD MED
251 #if not fic_hom_med :
252 #message = "Impossible de trouver le fichier HOMARD MED dans %s" % rep_calc
256 #fic_coords = os.path.join(rep_calc, "coords")
257 #erreur, message = change_coords (fic_med_new, fic_coords, verbose)
258 ##erreur, message = change_coords_0 (fic_med_new, fic_hom_med, verbose)
262 # 2.3. Ménage de l'ancien fichier MED
264 if ( fic_med_brut != fic_med_new ) :
265 print("Suppression du fichier %s" % fic_med_new)
266 os.remove(fic_med_brut)
268 # 3. Renommage du fichier si aucun noeud n'est concerné
272 if ( fic_med_brut != fic_med_new ) :
273 os.rename(fic_med_brut, fic_med_new)
275 # 4. Mise à jour des coordonnées dans le fichier historique HOMARD/MED si des noeuds sont concernés
279 erreur, message = maj_coords (rep_calc, fic_med_new, verbose)
283 return erreur, message
285 #========================================================================
286 #========================================================================
288 def maj_coords (rep_calc, fic_med_calc, verbose=False) :
291 Met à jour les coordonnées du fichier de calcul vers le fichier HOMARD
293 rep_calc : répertoire du calcul HOMARD qui est à traiter
294 fic_med_calc : fichier MED du calcul avec les coordonnées après projection
295 xao_file : fichier XAO de la géométrie
298 ligne = "rep_calc = %s" % rep_calc
299 ligne += "\nfic_med_calc = %s" % fic_med_calc
306 # 1. Recherche des inforamtions permanentes dans le fichier de configuration
308 fic_conf = os.path.join(rep_calc, "HOMARD.Configuration")
309 fichier = open (fic_conf, "r")
310 les_lignes = fichier.readlines()
315 for ligne in les_lignes :
317 for saux in ( "HOMai", "CCNoM" ) :
318 if ( saux+"NP1" in ligne ) :
319 iaux = ligne.index(saux)
320 ligne0 += saux + "N__" + ligne[iaux+8:]
322 if ( "NumeIter" in ligne ) :
323 iaux = ligne.index("NumeIter")
324 saux = ligne[iaux+8:-1]
326 s_iter = "%02d" % iaux
327 ligne0 += "NumeIter %s\n" % s_iter
331 message = "Erreur dans le décodage de %s\n" % fic_conf
336 # 2. Création du fichier de configuration
338 fic_conf_sv = os.path.join(rep_calc, "HOMARD.Configuration.majc")
339 fichier = open (fic_conf_sv, "w")
342 ligne += "ModeHOMA 5\n"
343 fic = os.path.join(rep_calc, "Liste.%s.maj_coords.log" % s_iter)
344 ligne += "ListeStd %s\n" % fic
345 ligne += "CCMaiN__ %s\n" % fic_med_calc
346 ligne += "RepeTrav %s\n" % rep_calc
347 ligne += "RepeInfo %s\n" % rep_calc
348 ligne += "Action homa\n"
349 ligne += "CCAssoci med\n"
350 ligne += "EcriFiHO N_SANS_FRONTIERE\n"
351 ligne += "MessInfo 10\n"
357 # 3.1. Détermination de l'exécutable
359 if "HOMARD_REP_EXE_PRIVATE" in os.environ :
360 HOMARD_REP_EXE = os.environ["HOMARD_REP_EXE_PRIVATE"]
362 HOMARD_REP_EXE = os.environ["HOMARD_REP_EXE"]
364 if "HOMARD_EXE_PRIVATE" in os.environ :
365 HOMARD_EXE = os.environ["HOMARD_EXE_PRIVATE"]
367 HOMARD_EXE = os.environ["HOMARD_EXE"]
369 homard_exe = os.path.join(HOMARD_REP_EXE, HOMARD_EXE)
371 ligne = "homard_exe = %s" % homard_exe
374 if not os.path.isfile(homard_exe) :
375 message = "homard_exe = %s" % homard_exe
376 message += "\nCe fichier executable n'existe pas."
382 fic_conf = os.path.join(rep_calc, "HOMARD.Configuration")
383 shutil.copyfile(fic_conf_sv, fic_conf)
386 erreur = os.system (homard_exe)
390 return erreur, message
392 #========================================================================
393 #========================================================================