1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2019 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__ = "V4.04"
28 import MEDLoader as ml
30 #========================================================================
31 #========================================================================
32 def get_dir(path_homard, test_name, debug=False) :
34 Get directories for the test.
38 # Répertoire des données du test
39 rep_data = os.path.join(path_homard, "share", "salome", "homardsamples")
40 rep_data = os.path.normpath(rep_data)
42 # Répertoire des résultats
44 dircase = os.path.join("/tmp", test_name)
45 if ( os.path.isdir(dircase) ) :
49 dircase = tempfile.mkdtemp(prefix=test_name+"_")
51 return rep_data, dircase
53 #========================================================================
54 #========================================================================
55 def get_dir_tutorial(path_homard) :
57 Get directory for the tutorial.
61 # Répertoire des données du tutorial
62 data_tutorial = os.path.join(path_homard, "share", "doc", "salome", "gui", "HOMARD", "fr", "_downloads")
63 data_tutorial = os.path.normpath(data_tutorial)
66 #========================================================================
67 #========================================================================
68 def remove_dir(directory) :
70 Empties, then removes a directory.
71 Copyright EDF-R&D 2013
74 l_aux = os.listdir(directory)
76 fic_a = os.path.join(directory, fic)
77 if os.path.isdir(fic_a) :
85 #========================================================================
86 #========================================================================
87 def test_results(rep_test, test_name, dircase, n_iter_test_file, n_rep_test_file, destroy_dir = True) :
90 rep_test: repertoire des tests
91 test_name: nom du test
92 dircase: repertoire des resultats du test
93 n_iter_test_file: numero de l'iteration a tester
94 n_rep_test_file: numero du repertoire de l'iteration a tester
95 destroy_dir: destruction du repertoire de calcul
96 Copyright EDF-R&D 2014
99 test_file_suff = "apad.%02d.bilan" % n_iter_test_file
100 rep_test_file = "I%02d" % n_rep_test_file
102 # Existence du fichier de référence
104 test_file = os.path.join(rep_test, test_name + "." + test_file_suff)
105 mess_error_ref = "\nReference file: " + test_file
106 #print ("test_file = %s" % test_file)
108 with open (test_file, "r") as fichier :
109 les_lignes_ref = fichier.readlines()
111 mess_error = mess_error_ref + "\nThis file does not exist.\n"
113 raise Exception(mess_error)
115 # Existence du fichier de l'exécution courante
117 test_file = os.path.join(dircase, rep_test_file, test_file_suff)
118 if os.path.isfile (test_file) :
119 with open (test_file, "r") as fichier :
120 les_lignes = fichier.readlines()
122 mess_error = "\nResult file: " + test_file
123 mess_error += "\nThis file does not exist.\n"
125 raise Exception(mess_error)
127 # Nombre de lignes identiques
129 nblign = len(les_lignes_ref)
130 if ( len(les_lignes) != nblign ):
131 mess_error = mess_error_ref + "\nResult file: " + test_file
132 mess_error += "\nThe number of lines of the files are not the same.\n"
134 raise Exception(mess_error)
136 # Comparaison des lignes, à l'esception de la date
138 for num in range(nblign) :
139 if ( "creation" not in les_lignes_ref[num] ) :
140 if ( les_lignes_ref[num] != les_lignes[num] ) :
141 message_erreur = "\nRefe : " + les_lignes_ref[num]
142 message_erreur += "Test : " + les_lignes[num][:-1]
143 message_erreur += "\nThe test is different from the reference."
145 raise Exception(message_erreur)
147 # Destruction éventuelle du répertoire du calcul
154 #========================================================================
155 #========================================================================
157 def saveGeometry( xao_file, name, author="" ):
159 Save the geometry in a XAO file
162 from salome.geom import geomBuilder
163 geompy = geomBuilder.New()
166 # find an object having groups in GEOM component
167 component = salome.myStudy.FindObjectByPath("/Geometry")
168 it = salome.myStudy.NewChildIterator( component )
177 if ( go.GetName() == name ) :
178 subIt = salome.myStudy.NewChildIterator( so )
180 subSO = subIt.Value()
182 if not subSO.GetName(): continue # a reference
183 gr = subSO.GetObject()
184 if gr and geompy.ShapeIdToType( gr.GetType() ) == "GROUP":
185 l_groups.append( gr )
190 raise RuntimeError("Cant find a geometry object in the SALOME study with name = '%s'" % name)
192 # save the geom object in a XAO file
194 ok = geompy.ExportXAO( geomObj, l_groups, l_fields, author, xao_file, "" )
199 #========================================================================
200 #========================================================================
202 def repositionnement (rep_calc, fic_med_brut, fic_med_new, xao_file, menage=True, verbose=False) :
205 Pilote le repositionnement des noeuds qui ont bougé
207 rep_calc : répertoire du calcul HOMARD qui est à traiter
208 fic_med_brut : fichier MED du calcul avec les coordonnées avant projection
209 fic_med_new : fichier MED du calcul avec les coordonnées après projection
210 xao_file : fichier XAO de la géométrie
211 menage : Suppression du fichier fic_med_brut
214 ligne = "rep_calc = %s" % rep_calc
215 ligne += "\nfic_med_brut = %s" % fic_med_brut
216 ligne += "\nfic_med_new = %s" % fic_med_new
217 ligne += "\nxao_file = %s" % xao_file
218 ligne += "\nmenage = %d" % menage
225 # 1. l_fr = liste des fichiers des lignes/surfaces a suivre
228 laux = os.listdir(rep_calc)
235 if ( fic[:2] == 'fr' ) :
236 l_fr.append(os.path.join(rep_calc, fic))
237 elif ( fic[-4:] == '.med' ) :
238 fic_hom_med = os.path.join(rep_calc, fic)
239 #print "\t\treperage de fic_hom_med =", fic_hom_med
241 # 2. Lancement du post-traitement si des noeuds sont concernés
246 print("l_fr =", l_fr)
247 print("fic_hom_med =", fic_hom_med)
250 # . prend le maillage brut dans le fichier fic_med_brut
251 # . prend la liste des noeuds à bouger pour chaque groupe concerné
252 # . prend la géométrie dans le fichier xao_file
253 # . retourne le maillage transformé dans le fichier fic_med_new
255 from FrontTrack import FrontTrack
257 ft.track( fic_med_brut, fic_med_new, l_fr, xao_file )
259 # 2.2. Transfert des coordonnées dans le fichier HOMARD MED
261 #if not fic_hom_med :
262 #message = "Impossible de trouver le fichier HOMARD MED dans %s" % rep_calc
266 #fic_coords = os.path.join(rep_calc, "coords")
267 #erreur, message = change_coords (fic_med_new, fic_coords, verbose)
268 ##erreur, message = change_coords_0 (fic_med_new, fic_hom_med, verbose)
272 # 2.3. Ménage éventuel de l'ancien fichier MED
276 if ( fic_med_brut != fic_med_new ) :
278 print("Suppression du fichier %s" % fic_med_brut)
279 os.remove(fic_med_brut)
281 # 3. Renommage du fichier si aucun noeud n'est concerné
285 if ( fic_med_brut != fic_med_new ) :
286 os.rename(fic_med_brut, fic_med_new)
288 # 4. Mise à jour des coordonnées dans le fichier historique HOMARD/MED si des noeuds sont concernés
292 erreur, message = maj_coords (rep_calc, fic_med_new, verbose)
296 return erreur, message
298 #========================================================================
299 #========================================================================
301 def maj_coords (rep_calc, fic_med_calc, verbose=False) :
304 Met à jour les coordonnées du fichier de calcul vers le fichier HOMARD
306 rep_calc : répertoire du calcul HOMARD qui est à traiter
307 fic_med_calc : fichier MED du calcul avec les coordonnées après projection
308 xao_file : fichier XAO de la géométrie
311 ligne = "rep_calc = %s" % rep_calc
312 ligne += "\nfic_med_calc = %s" % fic_med_calc
319 # 1. Recherche des inforamtions permanentes dans le fichier de configuration
321 fic_conf = os.path.join(rep_calc, "HOMARD.Configuration")
322 with open (fic_conf, "r") as fichier :
323 les_lignes = fichier.readlines()
327 for ligne in les_lignes :
329 for saux in ( "HOMai", "CCNoM" ) :
330 if ( saux+"NP1" in ligne ) :
331 iaux = ligne.index(saux)
332 ligne0 += saux + "N__" + ligne[iaux+8:]
334 if ( "NumeIter" in ligne ) :
335 iaux = ligne.index("NumeIter")
336 saux = ligne[iaux+8:-1]
338 s_iter = "%02d" % iaux
339 ligne0 += "NumeIter %s\n" % s_iter
343 message = "Erreur dans le décodage de %s\n" % fic_conf
348 # 2. Création du fichier de configuration
350 fic_conf_sv = os.path.join(rep_calc, "HOMARD.Configuration.majc")
352 with open (fic_conf_sv, "w") as fichier :
355 ligne += "ModeHOMA 5\n"
356 fic = os.path.join(rep_calc, "Liste.%s.maj_coords.log" % s_iter)
357 ligne += "ListeStd %s\n" % fic
358 ligne += "CCMaiN__ %s\n" % fic_med_calc
359 ligne += "RepeTrav %s\n" % rep_calc
360 ligne += "RepeInfo %s\n" % rep_calc
361 ligne += "Action homa\n"
362 ligne += "CCAssoci med\n"
363 ligne += "EcriFiHO N_SANS_FRONTIERE\n"
364 ligne += "MessInfo 10\n"
369 # 3.1. Détermination de l'exécutable
371 if "HOMARD_REP_EXE_PRIVATE" in os.environ :
372 HOMARD_REP_EXE = os.environ["HOMARD_REP_EXE_PRIVATE"]
374 HOMARD_REP_EXE = os.environ["HOMARD_REP_EXE"]
376 if "HOMARD_EXE_PRIVATE" in os.environ :
377 HOMARD_EXE = os.environ["HOMARD_EXE_PRIVATE"]
379 HOMARD_EXE = os.environ["HOMARD_EXE"]
381 homard_exe = os.path.join(HOMARD_REP_EXE, HOMARD_EXE)
383 ligne = "homard_exe = %s" % homard_exe
386 if not os.path.isfile(homard_exe) :
387 message = "homard_exe = %s" % homard_exe
388 message += "\nCe fichier executable n'existe pas."
394 fic_conf = os.path.join(rep_calc, "HOMARD.Configuration")
395 shutil.copyfile(fic_conf_sv, fic_conf)
398 erreur = os.system (homard_exe)
402 return erreur, message
404 #========================================================================
405 #========================================================================