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