Salome HOME
Merge branch V7_3_1_BR
[modules/homard.git] / src / HOMARD_I / HOMARD_Cas_i.cxx
1 // Copyright (C) 2011-2014  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // Remarques :
21 // L'ordre de description des fonctions est le meme dans tous les fichiers
22 // HOMARD_aaaa.idl, HOMARD_aaaa.hxx, HOMARD_aaaa.cxx, HOMARD_aaaa_i.hxx, HOMARD_aaaa_i.cxx :
23 // 1. Les generalites : Name, Delete, DumpPython, Dump, Restore
24 // 2. Les caracteristiques
25 // 3. Le lien avec les autres structures
26 //
27 // Quand les 2 fonctions Setxxx et Getxxx sont presentes, Setxxx est decrit en premier
28 //
29
30 #include "HOMARD_Cas_i.hxx"
31 #include "HOMARD_Gen_i.hxx"
32 #include "HOMARD_Cas.hxx"
33 #include "HOMARD_DriverTools.hxx"
34 #include "HOMARD.hxx"
35
36 #include "utilities.h"
37 #include <vector>
38 #include <sys/stat.h>
39
40 #ifdef WIN32
41 #include <direct.h>
42 #endif
43
44 //=============================================================================
45 /*!
46  *  standard constructor
47  */
48 //=============================================================================
49 HOMARD_Cas_i::HOMARD_Cas_i()
50 {
51   MESSAGE( "Default constructor, not for use" );
52   ASSERT( 0 );
53 }
54
55 //=============================================================================
56 /*!
57  *  standard constructor
58  */
59 //=============================================================================
60 HOMARD_Cas_i::HOMARD_Cas_i( CORBA::ORB_ptr orb,
61                             HOMARD::HOMARD_Gen_var engine )
62 {
63   MESSAGE( "HOMARD_Cas_i" );
64   _gen_i = engine;
65   _orb = orb;
66   myHomardCas = new ::HOMARD_Cas();
67   ASSERT( myHomardCas );
68 }
69
70 //=============================================================================
71 /*!
72  *  standard destructor
73  */
74 //=============================================================================
75 HOMARD_Cas_i::~HOMARD_Cas_i()
76 {
77 }
78 //=============================================================================
79 //=============================================================================
80 // Generalites
81 //=============================================================================
82 //=============================================================================
83 void HOMARD_Cas_i::SetName( const char* Name )
84 {
85   ASSERT( myHomardCas );
86   myHomardCas->SetName( Name );
87 }
88 //=============================================================================
89 char* HOMARD_Cas_i::GetName()
90 {
91   ASSERT( myHomardCas );
92   return CORBA::string_dup( myHomardCas->GetName().c_str() );
93 }
94 //=============================================================================
95 CORBA::Long  HOMARD_Cas_i::Delete( CORBA::Long Option )
96 {
97   ASSERT( myHomardCas );
98   char* CaseName = GetName() ;
99   MESSAGE ( "Delete : destruction du cas " << CaseName << ", Option = " << Option );
100   return _gen_i->DeleteCase(CaseName, Option) ;
101 }
102 //=============================================================================
103 char* HOMARD_Cas_i::GetDumpPython()
104 {
105   ASSERT( myHomardCas );
106   return CORBA::string_dup( myHomardCas->GetDumpPython().c_str() );
107 }
108 //=============================================================================
109 std::string HOMARD_Cas_i::Dump() const
110 {
111   return HOMARD::Dump( *myHomardCas );
112 }
113 //=============================================================================
114 bool HOMARD_Cas_i::Restore( const std::string& stream )
115 {
116   return HOMARD::Restore( *myHomardCas, stream );
117 }
118 //=============================================================================
119 //=============================================================================
120 // Caracteristiques
121 //=============================================================================
122 //=============================================================================
123 void HOMARD_Cas_i::SetDirName( const char* NomDir )
124 {
125   ASSERT( myHomardCas );
126   int codret ;
127   // A. recuperation du nom ; on ne fait rien si c'est le meme
128   char* oldrep = GetDirName() ;
129   if ( strcmp(oldrep,NomDir) == 0 )
130   {
131    return ;
132   }
133   MESSAGE ( "SetDirName : passage de oldrep = "<< oldrep << " a NomDir = "<<NomDir);
134   // B. controle de l'usage du repertoire
135   char* CaseName = GetName() ;
136   char* casenamedir = _gen_i->VerifieDir(NomDir) ;
137   if ( ( std::string(casenamedir).size() > 0 ) & ( strcmp(CaseName,casenamedir)!=0 ) )
138   {
139     INFOS ( "Le repertoire " << NomDir << " est deja utilise pour le cas "<< casenamedir );
140     SALOME::ExceptionStruct es;
141     es.type = SALOME::BAD_PARAM;
142     std::string text ;
143     text = "The directory " + std::string(NomDir) + " is already used for the case " + std::string(casenamedir) ;
144     es.text = CORBA::string_dup(text.c_str());
145     throw SALOME::SALOME_Exception(es);
146   }
147   // C. Changement/creation du repertoire
148   codret = myHomardCas->SetDirName( NomDir );
149   if ( codret != 0 )
150   {
151     SALOME::ExceptionStruct es;
152     es.type = SALOME::BAD_PARAM;
153     std::string text ;
154     if ( codret == 1 ) { text = "The directory for the case cannot be modified because some iterations are already defined." ; }
155     else               { text = "The directory for the case cannot be reached." ; }
156     es.text = CORBA::string_dup(text.c_str());
157     throw SALOME::SALOME_Exception(es);
158   }
159   // D. En cas de reprise, deplacement du point de depart
160   if ( GetState() != 0 )
161   {
162     MESSAGE ( "etat : " << GetState() ) ;
163     // D.1. Nom local du repertoire de l'iteration de depart dans le repertoire actuel du cas
164     HOMARD::HOMARD_Iteration_ptr Iter = GetIter0() ;
165     char* DirNameIter = Iter->GetDirNameLoc() ;
166     MESSAGE ( "SetDirName : nom actuel pour le repertoire de l iteration, DirNameIter = "<< DirNameIter);
167     // D.2. Recherche d'un nom local pour l'iteration de depart dans le futur repertoire du cas
168     char* nomDirIter = _gen_i->CreateDirNameIter(NomDir, 0 );
169     MESSAGE ( "SetDirName : nom futur pour le repertoire de l iteration, nomDirIter = "<< nomDirIter);
170     // D.3. Creation du futur repertoire local pour l'iteration de depart
171     std::string nomDirIterTotal ;
172     nomDirIterTotal = std::string(NomDir) + "/" + std::string(nomDirIter) ;
173 #ifndef WIN32
174     if (mkdir(nomDirIterTotal.c_str(), S_IRWXU|S_IRGRP|S_IXGRP) != 0)
175 #else
176     if (_mkdir(nomDirIterTotal.c_str()) != 0)
177 #endif
178     {
179       MESSAGE ( "nomDirIterTotal : " << nomDirIterTotal ) ;
180       SALOME::ExceptionStruct es;
181       es.type = SALOME::BAD_PARAM;
182       std::string text = "The directory for the starting iteration cannot be created." ;
183       es.text = CORBA::string_dup(text.c_str());
184       throw SALOME::SALOME_Exception(es);
185     }
186     // D.4. Deplacement du contenu du repertoire
187     std::string oldnomDirIterTotal ;
188     oldnomDirIterTotal = std::string(oldrep) + "/" + std::string(DirNameIter) ;
189     std::string commande = "mv " + std::string(oldnomDirIterTotal) + "/*" + " " + std::string(nomDirIterTotal) ;
190     codret = system(commande.c_str()) ;
191     if ( codret != 0 )
192     {
193       SALOME::ExceptionStruct es;
194       es.type = SALOME::BAD_PARAM;
195       std::string text = "The starting point for the case cannot be moved into the new directory." ;
196       es.text = CORBA::string_dup(text.c_str());
197       throw SALOME::SALOME_Exception(es);
198     }
199     commande = "rm -rf " + std::string(oldnomDirIterTotal) ;
200     codret = system(commande.c_str()) ;
201     if ( codret != 0 )
202     {
203       SALOME::ExceptionStruct es;
204       es.type = SALOME::BAD_PARAM;
205       std::string text = "The starting point for the case cannot be deleted." ;
206       es.text = CORBA::string_dup(text.c_str());
207       throw SALOME::SALOME_Exception(es);
208     }
209     // D.5. Memorisation du nom du repertoire de l'iteration
210     Iter->SetDirNameLoc(nomDirIter) ;
211   }
212   return ;
213 }
214 //=============================================================================
215 char* HOMARD_Cas_i::GetDirName()
216 {
217   ASSERT( myHomardCas );
218   return CORBA::string_dup( myHomardCas->GetDirName().c_str() );
219 }
220 //=============================================================================
221 CORBA::Long HOMARD_Cas_i::GetState()
222 {
223   ASSERT( myHomardCas );
224 // Nom de l'iteration initiale
225   char* Iter0Name = GetIter0Name() ;
226   HOMARD::HOMARD_Iteration_ptr Iter = _gen_i->GetIteration(Iter0Name) ;
227   int state = Iter->GetNumber() ;
228   return state ;
229 }
230 //=============================================================================
231 CORBA::Long HOMARD_Cas_i::GetNumberofIter()
232 {
233   ASSERT( myHomardCas );
234   return myHomardCas->GetNumberofIter();
235 }
236 //=============================================================================
237 void HOMARD_Cas_i::SetConfType( CORBA::Long ConfType )
238 {
239   ASSERT( myHomardCas );
240   myHomardCas->SetConfType( ConfType );
241 }
242 //=============================================================================
243 CORBA::Long HOMARD_Cas_i::GetConfType()
244 {
245   ASSERT( myHomardCas );
246   return myHomardCas->GetConfType();
247 }
248 //=============================================================================
249 void HOMARD_Cas_i::SetBoundingBox( const HOMARD::extrema& LesExtrema )
250 {
251   ASSERT( myHomardCas );
252   std::vector<double> VExtrema;
253   ASSERT( LesExtrema.length() == 10 );
254   VExtrema.resize( LesExtrema.length() );
255   for ( int i = 0; i < LesExtrema.length(); i++ )
256   {
257     VExtrema[i] = LesExtrema[i];
258   }
259   myHomardCas->SetBoundingBox( VExtrema );
260 }
261 //=============================================================================
262 HOMARD::extrema* HOMARD_Cas_i::GetBoundingBox()
263 {
264   ASSERT(myHomardCas );
265   HOMARD::extrema_var aResult = new HOMARD::extrema();
266   std::vector<double> LesExtremes = myHomardCas->GetBoundingBox();
267   ASSERT( LesExtremes.size() == 10 );
268   aResult->length( 10 );
269   for ( int i = 0; i < LesExtremes.size(); i++ )
270   {
271     aResult[i] = LesExtremes[i];
272   }
273   return aResult._retn();
274 }
275 //=============================================================================
276 void HOMARD_Cas_i::AddGroup( const char* Group)
277 {
278   ASSERT( myHomardCas );
279   myHomardCas->AddGroup( Group );
280 }
281 //=============================================================================
282 void HOMARD_Cas_i::SetGroups( const HOMARD::ListGroupType& ListGroup )
283 {
284   ASSERT( myHomardCas );
285   std::list<std::string> ListString ;
286   for ( int i = 0; i < ListGroup.length(); i++ )
287   {
288     ListString.push_back(std::string(ListGroup[i]));
289   }
290   myHomardCas->SetGroups( ListString );
291 }
292 //=============================================================================
293 HOMARD::ListGroupType* HOMARD_Cas_i::GetGroups()
294 {
295   ASSERT(myHomardCas );
296   const std::list<std::string>& ListString = myHomardCas->GetGroups();
297   HOMARD::ListGroupType_var aResult = new HOMARD::ListGroupType();
298   aResult->length( ListString.size() );
299   std::list<std::string>::const_iterator it;
300   int i = 0;
301   for ( it = ListString.begin(); it != ListString.end(); it++ )
302   {
303     aResult[i++] = CORBA::string_dup( (*it).c_str() );
304   }
305   return aResult._retn();
306 }
307 //=============================================================================
308 void HOMARD_Cas_i::AddBoundaryGroup( const char* BoundaryName, const char* Group)
309 {
310   MESSAGE ("AddBoundaryGroup : BoundaryName = "<< BoundaryName << ", Group = " << Group );
311   ASSERT( myHomardCas );
312   // A. La liste des frontiere+groupes
313   const std::list<std::string>& ListBoundaryGroup = myHomardCas->GetBoundaryGroup();
314   std::list<std::string>::const_iterator it;
315   // B. La frontiere
316   // B.1. La frontiere est-elle deja enregistree pour ce cas ?
317   bool existe = false ;
318   for ( it = ListBoundaryGroup.begin(); it != ListBoundaryGroup.end(); it++ )
319   {
320 //     MESSAGE ("..  Frontiere : "<< *it );
321     if ( *it == BoundaryName ) { existe = true ; }
322     it++ ;
323   }
324   // B.2. Pour une nouvelle frontiere, publication dans l'arbre d'etudes sous le cas
325   if ( !existe )
326   {
327     char* CaseName = GetName() ;
328     MESSAGE ( "AddBoundaryGroup : insertion de la frontiere dans l'arbre de " << CaseName );
329     _gen_i->PublishBoundaryUnderCase(CaseName, BoundaryName) ;
330   }
331   // C. Le groupe est-il deja enregistre pour une frontiere de ce cas ?
332   for ( it = ListBoundaryGroup.begin(); it != ListBoundaryGroup.end(); it++ )
333   {
334     std::string boun = *it ;
335     it++ ;
336 //     MESSAGE ("..  Group : "<< *it );
337     if ( *it == Group )
338     { INFOS ("Frontiere " << boun << " Un groupe est deja associe " << Group ) ;
339       SALOME::ExceptionStruct es;
340       es.type = SALOME::BAD_PARAM;
341       es.text = "Invalid AddBoundaryGroup";
342       throw SALOME::SALOME_Exception(es);
343       return ;
344     }
345   }
346   // D. Enregistrement du couple (frontiere,groupe) dans la reference du cas
347   myHomardCas->AddBoundaryGroup( BoundaryName, Group );
348 }
349 //=============================================================================
350 HOMARD::ListBoundaryGroupType* HOMARD_Cas_i::GetBoundaryGroup()
351 {
352   MESSAGE ("GetBoundaryGroup");
353   ASSERT(myHomardCas );
354   const std::list<std::string>& ListBoundaryGroup = myHomardCas->GetBoundaryGroup();
355   HOMARD::ListBoundaryGroupType_var aResult = new HOMARD::ListBoundaryGroupType();
356   aResult->length( ListBoundaryGroup.size() );
357   std::list<std::string>::const_iterator it;
358   int i = 0;
359   for ( it = ListBoundaryGroup.begin(); it != ListBoundaryGroup.end(); it++ )
360   {
361     aResult[i++] = CORBA::string_dup( (*it).c_str() );
362   }
363   return aResult._retn();
364 }
365 //=============================================================================
366 void HOMARD_Cas_i::SupprBoundaryGroup()
367 {
368   MESSAGE ("SupprBoundaryGroup");
369   ASSERT(myHomardCas );
370   myHomardCas->SupprBoundaryGroup();
371 }
372 //=============================================================================
373 void HOMARD_Cas_i::SetPyram( CORBA::Long Pyram )
374 {
375   MESSAGE ("SetPyram, Pyram = " << Pyram );
376   ASSERT( myHomardCas );
377   myHomardCas->SetPyram( Pyram );
378 }
379 //=============================================================================
380 CORBA::Long HOMARD_Cas_i::GetPyram()
381 {
382   MESSAGE ("GetPyram");
383   ASSERT( myHomardCas );
384   return myHomardCas->GetPyram();
385 }
386 //=============================================================================
387 void HOMARD_Cas_i::MeshInfo(CORBA::Long Qual, CORBA::Long Diam, CORBA::Long Conn, CORBA::Long Tail, CORBA::Long Inte)
388 {
389   MESSAGE ( "MeshInfo : information sur le maillage initial du cas" );
390   ASSERT( myHomardCas );
391 //
392 // Nom de l'iteration
393   char* IterName = GetIter0Name() ;
394   CORBA::Long etatMenage = -1 ;
395   CORBA::Long modeHOMARD = 7 ;
396   CORBA::Long Option1 = 1 ;
397   CORBA::Long Option2 = 1 ;
398   if ( Qual != 0 ) { modeHOMARD = modeHOMARD*5 ; }
399   if ( Diam != 0 ) { modeHOMARD = modeHOMARD*19 ; }
400   if ( Conn != 0 ) { modeHOMARD = modeHOMARD*11 ; }
401   if ( Tail != 0 ) { modeHOMARD = modeHOMARD*13 ; }
402   if ( Inte != 0 ) { modeHOMARD = modeHOMARD*3 ; }
403   CORBA::Long codret = _gen_i->Compute(IterName, etatMenage, modeHOMARD, Option1, Option2) ;
404   MESSAGE ( "MeshInfo : codret = " << codret );
405   return ;
406 }
407 //=============================================================================
408 //=============================================================================
409 // Liens avec les autres structures
410 //=============================================================================
411 //=============================================================================
412 char* HOMARD_Cas_i::GetIter0Name()
413 {
414   ASSERT( myHomardCas );
415   return CORBA::string_dup( myHomardCas->GetIter0Name().c_str() );
416 }
417 //=============================================================================
418 HOMARD::HOMARD_Iteration_ptr HOMARD_Cas_i::GetIter0()
419 {
420 // Nom de l'iteration initiale
421   char* Iter0Name = GetIter0Name() ;
422   MESSAGE ( "GetIter0 : Iter0Name      = " << Iter0Name );
423   return _gen_i->GetIteration(Iter0Name) ;
424 }
425 //=============================================================================
426 HOMARD::HOMARD_Iteration_ptr HOMARD_Cas_i::NextIteration( const char* IterName )
427 {
428 // Nom de l'iteration parent
429   char* NomIterParent = GetIter0Name() ;
430   MESSAGE ( "NextIteration : IterName      = " << IterName );
431   MESSAGE ( "NextIteration : NomIterParent = " << NomIterParent );
432   return _gen_i->CreateIteration(IterName, NomIterParent) ;
433 }
434 //=============================================================================
435 HOMARD::HOMARD_Iteration_ptr HOMARD_Cas_i::LastIteration( )
436 {
437   HOMARD::HOMARD_Iteration_ptr Iter ;
438   HOMARD::listeIterFilles_var ListeIterFilles ;
439   char* IterName ;
440 // Iteration initiale du cas
441   IterName = GetIter0Name() ;
442 // On va explorer la descendance de cette iteration initiale
443 // jusqu'a trouver celle qui n'a pas de filles
444   int nbiterfilles = 1 ;
445   while ( nbiterfilles == 1 )
446   {
447 // L'iteration associee
448 //     MESSAGE ( ".. IterName = " << IterName );
449     Iter = _gen_i->GetIteration(IterName) ;
450 // Les filles de cette iteration
451     ListeIterFilles = Iter->GetIterations() ;
452     nbiterfilles = ListeIterFilles->length() ;
453 //     MESSAGE ( ".. nbiterfilles = " << nbiterfilles );
454 // S'il y a au moins 2 filles, arret : on ne sait pas faire
455     VERIFICATION( nbiterfilles <= 1 ) ;
456 // S'il y a une fille unique, on recupere le nom de la fille et on recommence
457     if ( nbiterfilles == 1 )
458     { IterName = ListeIterFilles[0] ; }
459   }
460 //
461   return Iter ;
462 }
463 //=============================================================================
464 void HOMARD_Cas_i::AddIteration( const char* NomIteration )
465 {
466   ASSERT( myHomardCas );
467   myHomardCas->AddIteration( NomIteration );
468 }
469 //=============================================================================
470 //=============================================================================
471 // YACS
472 //=============================================================================
473 //=============================================================================
474 //=============================================================================
475 // Creation d'un schema YACS
476 // YACSName : nom du schema
477 // ScriptFile : nom du fichier contenant le script de lancement du calcul
478 // DirName : le repertoire de lancement des calculs du sch?ma
479 // MeshFile : nom du fichier contenant le maillage pour le premier calcul
480 //=============================================================================
481 HOMARD::HOMARD_YACS_ptr HOMARD_Cas_i::CreateYACSSchema( const char* YACSName, const char* ScriptFile, const char* DirName, const char* MeshFile )
482 {
483 // Nom du cas
484   const char* CaseName = GetName() ;
485   MESSAGE ( "CreateYACSSchema : Schema YACS pour le cas " << YACSName);
486   MESSAGE ( "nomCas     : " << CaseName);
487   MESSAGE ( "ScriptFile : " << ScriptFile);
488   MESSAGE ( "DirName    : " << DirName);
489   MESSAGE ( "MeshFile   : " << MeshFile);
490   return _gen_i->CreateYACSSchema(YACSName, CaseName, ScriptFile, DirName, MeshFile) ;
491 }