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