Salome HOME
Merge V8_4_BR branch.
[modules/homard.git] / src / tests / Test / test_util.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2017  CEA/DEN, EDF R&D
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 """
21 Python script for HOMARD
22 Utilitaires pour les tests
23 """
24 __revision__ = "V3.01"
25
26 import os
27 import MEDLoader as ml
28 import shutil
29 #========================================================================
30 #========================================================================
31 def remove_dir(directory) :
32   """
33 Empties, then removes a directory.
34 Copyright EDF-R&D 2013
35   """
36 #
37   l_aux = os.listdir(directory)
38   for fic in l_aux :
39     fic_a = os.path.join(directory, fic)
40     if os.path.isdir(fic_a) :
41       remove_dir(fic_a)
42     else :
43       os.remove(fic_a)
44   os.rmdir(directory)
45 #
46   return
47 #
48 #========================================================================
49 #========================================================================
50 def test_results(rep_test, test_name, dircase, n_iter_test_file, n_rep_test_file, destroy_dir = True) :
51   """
52 Test of the result
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
60   """
61   #
62   test_file_suff = "apad.%02d.bilan" % n_iter_test_file
63   rep_test_file = "I%02d" % n_rep_test_file
64   #
65   test_file = os.path.join(rep_test, test_name + "." + test_file_suff)
66   mess_error_ref = "\nReference file: " + test_file
67 #
68 # Existence du fichier de référence
69 #
70   try :
71     file = open (test_file, "r")
72     les_lignes_ref = file.readlines()
73     file.close()
74   except :
75     mess_error = mess_error_ref + "\nThis file does not exist.\n"
76     destroy_dir = False
77     raise Exception(mess_error)
78 #
79 # Existence du fichier de l'exécution courante
80 #
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()
85     file.close()
86   else :
87     mess_error  = "\nResult file: " + test_file
88     mess_error += "\nThis file does not exist.\n"
89     destroy_dir = False
90     raise Exception(mess_error)
91 #
92 # Nombre de lignes identiques
93 #
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"
98     destroy_dir = False
99     raise Exception(mess_error)
100 #
101 # Comparaison des lignes, à l'esception de la date
102 #
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."
109         destroy_dir = False
110         raise Exception(message_erreur)
111 #
112 # Destruction éventuelle du répertoire du calcul
113 #
114   if destroy_dir:
115     remove_dir(dircase)
116 #
117   return
118 #
119 #========================================================================
120 #========================================================================
121 #
122 def saveGeometry( xao_file, name, author="" ):
123   """
124   Save the geometry in a XAO file
125   """
126   import salome
127   from salome.geom import geomBuilder
128   geompy = geomBuilder.New(salome.myStudy)
129   error = 0
130
131   # find an object having groups in GEOM component
132   component = salome.myStudy.FindObjectByPath("/Geometry")
133   it = salome.myStudy.NewChildIterator( component )
134   geomObj = None
135   l_groups = list()
136   while it.More():
137     so = it.Value()
138     it.Next()
139     go = so.GetObject()
140     #
141     if go:
142       if ( go.GetName() == name ) :
143         subIt = salome.myStudy.NewChildIterator( so )
144         while subIt.More():
145           subSO = subIt.Value()
146           subIt.Next()
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 )
151         geomObj = go
152         break
153
154   if not geomObj:
155     raise RuntimeError("Cant find a geometry object in the SALOME study with name = '%s'" % name)
156
157   # save the geom object in a XAO file
158   l_fields = list()
159   ok = geompy.ExportXAO( geomObj, l_groups, l_fields, author, xao_file, "" )
160   error = not ok
161
162   return error
163 #
164 #========================================================================
165 #========================================================================
166 #
167 def repositionnement (rep_calc, fic_med_brut, fic_med_new, xao_file, verbose=False) :
168 #
169   """
170 Pilote le repositionnement des noeuds qui ont bougé
171 Entrées :
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
176   """
177   if verbose :
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
182     print(ligne)
183
184   message = ""
185   erreur = 0
186   while not erreur :
187 #
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.
191 #
192     fic_hom_med = None
193     laux = os.listdir(rep_calc)
194     #print laux
195     l_fr = list()
196     icpt_1D = 0
197     icpt_2D = 0
198     for fic in laux :
199       #print "\t" + fic
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()
204         fichier.close()
205         os.remove(fic_fort)
206         a_faire = False
207         for ligne in les_lignes[1:] :
208           laux1 = ligne.split()
209           if ( len(laux1) >= 3 ) :
210             a_faire = True
211             break
212         if a_faire :
213           if ( "1D" in les_lignes[0] ) :
214             nomfic_bis = "fr1D.%02d" % icpt_1D
215             icpt_1D += 1
216           else :
217             nomfic_bis = "fr2D.%02d" % icpt_2D
218             icpt_2D += 1
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 ) ) :
223               fichier.write(ligne)
224           fichier.close()
225           #print "\t\tajout de %s" % fic_1
226           l_fr.append(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
230 #
231 # 2. Lancement du post-traitement si des noeuds sont concernés
232 #
233     if l_fr :
234 #
235       if verbose :
236         print("l_fr =", l_fr)
237         print("fic_hom_med =", fic_hom_med)
238 #
239 # 2.1.  La fonction :
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
244 #
245       from FrontTrack import FrontTrack
246       ft = FrontTrack()
247       ft.track( fic_med_brut, fic_med_new, l_fr, xao_file )
248 #
249 # 2.2. Transfert des coordonnées dans le fichier HOMARD MED
250 #
251       #if not fic_hom_med :
252         #message = "Impossible de trouver le fichier HOMARD MED dans %s" % rep_calc
253         #erreur = 12
254         #break
255 ##
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)
259       #if erreur :
260         #break
261 #
262 # 2.3. Ménage de l'ancien fichier MED
263 #
264       if ( fic_med_brut != fic_med_new ) :
265         print("Suppression du fichier %s" % fic_med_new)
266         os.remove(fic_med_brut)
267 #
268 # 3. Renommage du fichier si aucun noeud n'est concerné
269 #
270     else :
271 #
272       if ( fic_med_brut != fic_med_new ) :
273         os.rename(fic_med_brut, fic_med_new)
274 #
275 # 4. Mise à jour des coordonnées dans le fichier historique HOMARD/MED si des noeuds sont concernés
276 #
277     if l_fr :
278 #
279       erreur, message = maj_coords (rep_calc, fic_med_new, verbose)
280 #
281     break
282 #
283   return erreur, message
284 #
285 #========================================================================
286 #========================================================================
287 #
288 def maj_coords (rep_calc, fic_med_calc, verbose=False) :
289 #
290   """
291 Met à jour les coordonnées du fichier de calcul vers le fichier HOMARD
292 Entrées :
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
296   """
297   if verbose :
298     ligne =    "rep_calc     = %s" % rep_calc
299     ligne += "\nfic_med_calc = %s" % fic_med_calc
300     print(ligne)
301
302   message = ""
303   erreur = 0
304   while not erreur :
305 #
306 # 1. Recherche des inforamtions permanentes dans le fichier de configuration
307 #
308     fic_conf = os.path.join(rep_calc, "HOMARD.Configuration")
309     fichier = open (fic_conf, "r")
310     les_lignes = fichier.readlines()
311     fichier.close()
312 #
313     ligne0 = ""
314     icpt = 0
315     for ligne in les_lignes :
316       #print "\t" + ligne
317       for saux in ( "HOMai", "CCNoM" ) :
318         if ( saux+"NP1" in ligne ) :
319           iaux = ligne.index(saux)
320           ligne0 += saux + "N__" + ligne[iaux+8:]
321           icpt += 1
322       if ( "NumeIter" in ligne ) :
323         iaux = ligne.index("NumeIter")
324         saux = ligne[iaux+8:-1]
325         iaux = int(saux) + 1
326         s_iter = "%02d" % iaux
327         ligne0 += "NumeIter %s\n" % s_iter
328         icpt += 1
329 #
330     if ( icpt != 3 ) :
331       message = "Erreur dans le décodage de %s\n" % fic_conf
332       message+= ligne0
333       erreur = 1
334       break
335 #
336 # 2. Création du fichier de configuration
337 #
338     fic_conf_sv = os.path.join(rep_calc, "HOMARD.Configuration.majc")
339     fichier = open (fic_conf_sv, "w")
340 #
341     ligne = ligne0
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"
352 #
353     fichier.write(ligne)
354     fichier.close()
355 #
356 # 3. Mise à jour
357 # 3.1. Détermination de l'exécutable
358 #
359     if "HOMARD_REP_EXE_PRIVATE" in os.environ :
360       HOMARD_REP_EXE  = os.environ["HOMARD_REP_EXE_PRIVATE"]
361     else :
362       HOMARD_REP_EXE  = os.environ["HOMARD_REP_EXE"]
363 #
364     if "HOMARD_EXE_PRIVATE" in os.environ :
365       HOMARD_EXE  = os.environ["HOMARD_EXE_PRIVATE"]
366     else :
367       HOMARD_EXE  = os.environ["HOMARD_EXE"]
368 #
369     homard_exe = os.path.join(HOMARD_REP_EXE, HOMARD_EXE)
370     if verbose :
371       ligne = "homard_exe = %s" % homard_exe
372       print(ligne)
373 #
374     if not os.path.isfile(homard_exe) :
375       message = "homard_exe = %s" % homard_exe
376       message += "\nCe fichier executable n'existe pas."
377       erreur = 31
378       break
379 #
380 # 3.2. Lancement
381 #
382     fic_conf = os.path.join(rep_calc, "HOMARD.Configuration")
383     shutil.copyfile(fic_conf_sv, fic_conf)
384 #
385     os.chdir(rep_calc)
386     erreur = os.system (homard_exe)
387 #
388     break
389 #
390   return erreur, message
391 #
392 #========================================================================
393 #========================================================================
394 #