]> SALOME platform Git repositories - samples/component.git/blob - src/CalculatorComponent/CalculatorEngine.cxx
Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[samples/component.git] / src / CalculatorComponent / CalculatorEngine.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
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. 
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 //
22 //  File   : CalculatorEngine.cxx
23 //  Author : Laurent DADA, CEA
24 //  Module : CalculatorComponent
25 //  $Header$
26
27 #include <unistd.h>
28
29 #include <iostream>
30 #include <sstream>
31
32 #include "CalculatorEngine.hxx"
33 #include "MEDMEM_Support_i.hxx"
34 #include "SUPPORTClient.hxx"
35
36 #include "MEDMEM_define.hxx"
37 #include "MEDMEM_STRING.hxx"
38 #include "MEDMEM_Exception.hxx"
39 #include "MEDMEM_Unit.hxx"
40
41 #include "med.hxx"
42
43 #include "utilities.h"
44
45 using namespace MEDMEM;
46 using namespace std;
47
48 #ifndef MED_TAILLE_PNOM
49 #define MED_TAILLE_PNOM MED_TAILLE_PNOM21
50 #endif
51
52 typedef FIELD<double,MEDMEM::FullInterlace> TFieldDouble;
53 typedef FIELDTEMPLATE_I<double,MEDMEM::FullInterlace> TFieldDouble_i;
54
55 //================================================================================
56 // static PrintFieldValues - shows field contents
57 //================================================================================
58
59 static void PrintFieldValues (TFieldDouble * field, int until_index) 
60 {
61   const double * values = field -> getValue();
62   int nb_comp     = field -> getNumberOfComponents();
63   MESSAGE( "Field                    : " << field -> getName() );
64   MESSAGE( "    Description          : " << field -> getDescription() );
65   MESSAGE( "    Number of components : " << nb_comp );
66
67   for (int i=0 ; i<nb_comp ; i++) {
68     MESSAGE ( "       Component " << field -> getComponentName(i+1) << endl \
69              << "                   Unit        : " << field -> getMEDComponentUnit(i+1) << endl \
70              << "                   Description : " << field -> getComponentDescription(i+1) );
71   }
72   MESSAGE( "    First " << until_index << " values   : " );
73   
74   for (int j=0 ; j < until_index ; j++) {
75     for (int i=0;i<nb_comp;i++) MESSAGE( values[i+j*nb_comp] << " " );
76   }
77 }
78
79
80 //================================================================================
81 // Constructors
82 //================================================================================
83
84 CalculatorEngine::CalculatorEngine(CORBA::ORB_ptr orb,
85                                    PortableServer::POA_ptr poa,
86                                    PortableServer::ObjectId * contId, 
87                                    const char *instanceName,
88                                    const char *interfaceName) :
89                                    Engines_Component_i(orb, poa, contId, instanceName, interfaceName,true)
90 {
91   MESSAGE("CalculatorEngine::CalculatorEngine activate object instanceName("
92           << instanceName << ") interfaceName(" << interfaceName << ")" )
93     _thisObj = this ;
94   _id = _poa->activate_object(_thisObj);
95   SALOME_NamingService *NS = new SALOME_NamingService(orb);
96   _NS=NS;
97 }
98
99 CalculatorEngine::CalculatorEngine()
100 {
101 }
102
103 //================================================================================
104 // Destructors
105 //================================================================================
106
107 CalculatorEngine::~CalculatorEngine()
108 {
109   delete _NS;
110 }
111
112 static omni_mutex aPutToStudyMutex;
113 SALOME_MED::FIELDDOUBLE_ptr CalculatorEngine::PutToStudy(SALOME_MED::FIELDDOUBLE_ptr theField1,
114                                                          CORBA::Long theStudyId) {
115   omni_mutex_lock aLock(aPutToStudyMutex);
116
117   CORBA::Object_var anObj = _NS->Resolve("/myStudyManager");
118   SALOMEDS::StudyManager_var aManager  = SALOMEDS::StudyManager::_narrow(anObj);
119 //   SALOMEDS::ListOfOpenStudies_var aList = aManager->GetOpenStudies();
120 //   SALOMEDS::Study_var aStudy = aManager->GetStudyByName(aList[0]);
121   SALOMEDS::Study_var aStudy = aManager->GetStudyByID(theStudyId);
122   SALOMEDS::StudyBuilder_var aBuilder = aStudy->NewBuilder();
123
124   SALOMEDS::SComponent_var aComponent = aStudy->FindComponent("Calculator");
125   if (aComponent->_is_nil()) aComponent = aBuilder->NewComponent("Calculator");
126   SALOMEDS::GenericAttribute_var anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
127   SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
128   aName->SetValue("Calculator");
129   
130   SALOMEDS::SObject_var aSO = aBuilder->NewObject(aComponent);
131   anAttr = aBuilder->FindOrCreateAttribute(aSO, "AttributeName");
132   aName = SALOMEDS::AttributeName::_narrow(anAttr);
133   char aFieldName[10];
134   sprintf(aFieldName, "Field_%d", (int)aSO->Tag());
135   aName->SetValue(strdup(aFieldName));
136   
137   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
138   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
139   CORBA::ORB_var &orb = init(0,0);
140
141   string iorStr = orb->object_to_string(theField1);
142   anAttr = aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
143   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
144   anIOR->SetValue(iorStr.c_str());
145
146   return SALOME_MED::FIELDDOUBLE::_duplicate(theField1);
147 }
148
149
150 //=======================================================================================
151 //  Addition of 2 FieldNodeDouble (based on Med library)
152 //
153 //  We assume that :
154 //     - support
155 //     - number of components
156 //     - name, type, unit and description of components
157 //  are the same for the 2 fields.
158 //
159 //  Otherwise,
160 //  the result will be a copy of the first field.
161 //
162 //=======================================================================================
163
164 static omni_mutex anAddMutex;
165 SALOME_MED::FIELDDOUBLE_ptr CalculatorEngine::Add(SALOME_MED::FIELDDOUBLE_ptr FirstField,
166                                                   SALOME_MED::FIELDDOUBLE_ptr SecondField)
167 {
168   omni_mutex_lock aLock(anAddMutex);
169
170   ostringstream tmp;
171   bool          same_support = true;
172   
173   beginService("CalculatorEngine::Add");
174   
175   sendMessage(NOTIF_TRACE, "CalculatorEngine::Add is Computing");
176   
177   BEGIN_OF("CalculatorEngine::Add(SALOME_MED::FIELDDOUBLE_ptr FirstField,SALOME_MED::FIELDDOUBLE_ptr SecondField)");
178
179   SCRUTE(FirstField);
180   SCRUTE(SecondField);
181
182   CORBA::String_var field1_name        = FirstField -> getName();
183   CORBA::String_var field2_name        = SecondField -> getName();
184   CORBA::String_var field_description = FirstField -> getDescription();
185
186   string message = string("\n             CalculatorEngine::Add : ") +
187                    string("\n                               first field name  --> ") + string(field1_name.in()) +
188                    string("\n                               second field name --> ") + string(field2_name.in());
189
190   sendMessage(NOTIF_TRACE, message.c_str());
191
192   // Number of components
193   int nb_comp1  = FirstField -> getNumberOfComponents();
194   int nb_comp2  = SecondField -> getNumberOfComponents();
195   if (nb_comp1 != nb_comp2) same_support = false;
196    MESSAGE( "  nb_comp1 --> " << nb_comp1 <<endl );
197    MESSAGE( "  nb_comp2 --> " << nb_comp2 <<endl );
198
199
200   /* Name Description and Unit of each component of the field */
201
202   string * component_name = new string[nb_comp1];
203   string * component_unit = new string[nb_comp1];
204
205   for (int i=0 ; i<nb_comp1 ; i++) {
206     CORBA::String_var name = FirstField -> getComponentName(i+1);
207     CORBA::String_var unit = FirstField -> getComponentUnit(i+1);
208     component_name[i].assign(name.in());
209     component_unit[i].assign(unit.in());
210
211     if (same_support) {
212       CORBA::String_var name2 = SecondField -> getComponentName(i+1);
213       CORBA::String_var unit2 = SecondField -> getComponentUnit(i+1);
214       if ( (component_name[i] != name2.in()) || (component_unit[i] != unit2.in()))
215         same_support = false;
216     }
217   }
218
219   MESSAGE("CalculatorEngine::Add Number of entities in the Support of each fields");
220  
221   // Number of entities in the Support of the field
222
223   int len_value1 = 0;
224   int len_value2 = 0;
225   SALOME_MED::SUPPORT_var support1 = FirstField -> getSupport();
226   SALOME_MED::SUPPORT_var support2 = SecondField -> getSupport();
227
228   SCRUTE(support1);
229
230   SCRUTE(support2);
231
232   SALOME_MED::MESH_var mesh1 = support1 -> getMesh();
233   SALOME_MED::MESH_var mesh2 = support2 -> getMesh();
234
235   SCRUTE(mesh1);
236   SCRUTE(mesh2);
237
238   if (same_support && support1 != support2) same_support = false;
239
240   if (support1 -> isOnAllElements())
241     len_value1 = mesh1-> getNumberOfElements(support1 -> getEntity(),SALOME_MED::MED_ALL_ELEMENTS);
242   else
243     len_value1 = support1 -> getNumberOfElements(SALOME_MED::MED_ALL_ELEMENTS);
244
245   SCRUTE(len_value1);
246
247   if (support2 -> isOnAllElements())
248     len_value2 = mesh2-> getNumberOfElements(support2 -> getEntity(),SALOME_MED::MED_ALL_ELEMENTS);
249   else
250     len_value2 = support2 -> getNumberOfElements(SALOME_MED::MED_ALL_ELEMENTS);
251
252   if (same_support && len_value1 != len_value2) same_support = false;
253
254   SCRUTE(len_value2);
255
256   // Values of new field
257   SALOME_MED::double_array * first_value = FirstField -> getValue(SALOME_MED::MED_FULL_INTERLACE);
258   SALOME_MED::double_array * second_value;
259   if (same_support) {
260     second_value = SecondField -> getValue(SALOME_MED::MED_FULL_INTERLACE);
261     tmp << "\n             CalculatorEngine::Add : " ;
262     tmp << "\n                               Number of entities in the Support of first field  = " << len_value1 ;
263     tmp << "\n                               Number of entities in the Support of second field = " << len_value2 ;
264     message = tmp.str();
265
266     sendMessage(NOTIF_TRACE, message.c_str());
267
268   }
269
270   int totalLength = nb_comp1*len_value1;
271   double * new_value = new double[totalLength];
272   if (same_support) 
273     {
274       sendMessage(NOTIF_TRACE, "CalculatorEngine::Add - field1 and field2 have the same support");
275
276       for(int i=0 ; i<totalLength ; i++) 
277         new_value[i] = ((*first_value)[i]) + ((*second_value)[i]);
278     }
279   else {
280     for(int i=0 ; i<totalLength ; i++) 
281       new_value[i] = ((*first_value)[i]) ;
282   }
283   
284   // Creation of a Local Field
285   
286   MESSAGE("CalculatorEngine::Add Creation of the local field "<< totalLength);
287   
288   TFieldDouble * fieldloc =  new TFieldDouble();
289   fieldloc -> allocValue(nb_comp1,len_value1);
290   fieldloc  -> setValue(new_value);
291   fieldloc  -> setName("-new_Add-");
292   fieldloc  -> setDescription( field_description.in() );
293   fieldloc -> setComponentsNames(component_name);
294   fieldloc -> setMEDComponentsUnits(component_unit);
295
296   delete [] new_value;
297   delete first_value;
298   if (same_support) 
299     delete second_value;
300   delete [] component_name;
301   delete [] component_unit;
302   mesh1->Destroy();
303   mesh2->Destroy();
304   support2->Destroy();
305
306   // Control
307   int until_index = ( 20 > len_value1)? len_value1 : 20;
308   PrintFieldValues (fieldloc,until_index);
309
310   sendMessage(NOTIF_TRACE, "CalculatorEngine::Add - new field created");
311
312   // Export result
313   // Creation of the new CORBA field
314
315   MESSAGE("CalculatorEngine::Add Creation of the CORBA field");
316
317   SUPPORT *support1Clt=new SUPPORTClient(support1);
318   fieldloc->setSupport(support1Clt);
319   support1Clt->removeReference();
320
321   TFieldDouble_i * NewField = new TFieldDouble_i(fieldloc,true) ;
322   SALOME_MED::FIELDDOUBLE_ptr myFieldIOR = NewField->_this() ;
323
324   //sleep(4);
325
326   END_OF("CalculatorEngine::Add(SALOME_MED::FIELDDOUBLE_ptr FirstField,SALOME_MED::FIELDDOUBLE_ptr SecondField)");
327
328   endService("CalculatorEngine::Add");
329   return myFieldIOR;
330
331 }
332
333
334 //===============================================================================
335 //  Multiplication of a FieldNodeDouble by a double value (based on Med library)
336 //===============================================================================
337
338 static omni_mutex aMulMutex;
339 SALOME_MED::FIELDDOUBLE_ptr CalculatorEngine::Mul(SALOME_MED::FIELDDOUBLE_ptr OldField,
340                                                   CORBA::Double x1)
341 {
342   omni_mutex_lock aLock(aMulMutex);
343
344   beginService("CalculatorEngine::Mul");
345   BEGIN_OF("CalculatorEngine::Mul(SALOME_MED::FIELDDOUBLE_ptr OldField,CORBA::Double x1)");
346
347   SCRUTE(OldField);
348   SCRUTE(x1);
349
350   // Name and description of field
351   CORBA::String_var field_name        = OldField -> getName();
352   CORBA::String_var field_description = OldField -> getDescription();
353
354   // Number of components
355   int nb_comp  = OldField -> getNumberOfComponents();
356   string * component_name = new string[nb_comp];
357   string * component_unit = new string[nb_comp];
358
359   for (int i=0 ; i<nb_comp ; i++) {
360     CORBA::String_var name = OldField -> getComponentName(i+1);
361     component_name[i] = name.in();
362     CORBA::String_var unit = OldField -> getComponentUnit(i+1);
363     component_unit[i] = unit.in();
364   }
365
366   MESSAGE("CalculatorEngine::Mul Number of entities in the Support of the field");
367
368   // Number of entities in the Support of the field
369   int len_value = 0;
370
371   SALOME_MED::SUPPORT_var support = OldField -> getSupport();
372
373   SCRUTE(support);
374
375   SALOME_MED::MESH_var mesh = support -> getMesh();
376
377   SCRUTE(mesh);
378
379   if (support -> isOnAllElements())
380     len_value = mesh-> getNumberOfElements(support -> getEntity(),SALOME_MED::MED_ALL_ELEMENTS);
381   else
382     len_value = support -> getNumberOfElements(SALOME_MED::MED_ALL_ELEMENTS);
383
384   SCRUTE(len_value);
385
386   // Values of new field
387   SALOME_MED::double_array * old_value = OldField -> getValue(SALOME_MED::MED_FULL_INTERLACE);
388
389   int totalLength = nb_comp*len_value;
390   double * new_value = new double[totalLength];
391   for(int i=0 ; i<totalLength ; i++) {
392     new_value[i] = x1 * ((*old_value)[i]);
393   }
394
395   // Creation of a Local Field
396
397   MESSAGE("CalculatorEngine::Mul Creation of the local field "<< totalLength);
398
399   TFieldDouble * fieldloc =  new TFieldDouble();
400   fieldloc -> allocValue(nb_comp,len_value);
401   fieldloc  -> setValue(new_value);
402   fieldloc  -> setName("-new_Mul-");
403   fieldloc  -> setDescription(field_description.in());
404   fieldloc -> setComponentsNames(component_name);
405   fieldloc -> setMEDComponentsUnits(component_unit);
406
407   delete old_value;
408   delete [] new_value;
409   delete [] component_name;
410   delete [] component_unit;
411   mesh->Destroy();
412
413   // Creation of the new CORBA field
414
415   MESSAGE("CalculatorEngine::Mul Creation of the CORBA field");
416
417   SUPPORT *supportClt=new SUPPORTClient(support);
418   fieldloc->setSupport(supportClt);
419   supportClt->removeReference();
420   TFieldDouble_i * NewField = new TFieldDouble_i(fieldloc,true) ;
421   SALOME_MED::FIELDDOUBLE_ptr myFieldIOR = NewField->_this() ;
422
423   //sleep(4);
424
425   END_OF("CalculatorEngine::Mul(SALOME_MED::FIELDDOUBLE_ptr OldField,CORBA::Double x1)");
426
427   endService("CalculatorEngine::Mul");
428   return myFieldIOR;
429 }
430
431
432 //================================================================================
433 //  Build a constant field of double based on first field support (Med library)
434 //================================================================================
435
436 static omni_mutex aConstantMutex;
437 SALOME_MED::FIELDDOUBLE_ptr CalculatorEngine::Constant(SALOME_MED::FIELDDOUBLE_ptr FirstField,
438                                                        CORBA::Double x1)
439 {
440   omni_mutex_lock aLock(aConstantMutex);
441
442   beginService("CalculatorEngine::Const");
443   BEGIN_OF("CalculatorEngine::Constant(SALOME_MED::FIELDDOUBLE_ptr FirstField,CORBA::Double x1)");
444
445   // Name and description of field
446   CORBA::String_var field_name        = FirstField -> getName();
447   CORBA::String_var field_description = FirstField -> getDescription();
448
449   SALOME_MED::SUPPORT_var FirstSupport = FirstField -> getSupport();
450
451   // Number of components
452   int nb_comp  = FirstField -> getNumberOfComponents();
453
454   // Type, name, unit and description of components
455   string * component_name = new string[nb_comp];
456   string * component_unit = new string[nb_comp];
457
458   for (int i = 0 ; i<nb_comp ; i++) {
459     CORBA::String_var name = FirstField -> getComponentName(i+1);
460     component_name[i] = name.in();
461     CORBA::String_var unit = FirstField -> getComponentUnit(i+1);
462     component_unit[i] = unit.in();
463   }
464
465   MESSAGE("CalculatorEngine::Constant Number of entities in the Support of the field");
466
467   // Number of entities in the Support of the field
468   int len_value = 0;
469
470   SCRUTE(FirstSupport);
471
472   SALOME_MED::MESH_var mesh = FirstSupport -> getMesh();
473
474   SCRUTE(mesh);
475
476   if ( FirstSupport -> isOnAllElements() )
477     len_value = mesh -> getNumberOfElements(FirstSupport -> getEntity(),SALOME_MED::MED_ALL_ELEMENTS);
478   else
479     len_value = FirstSupport -> getNumberOfElements(SALOME_MED::MED_ALL_ELEMENTS);
480
481   // Values of new field
482   int totalLength = nb_comp*len_value;
483   double * new_value = new double[totalLength];
484
485   for(int i=0 ; i<totalLength ; i++) 
486     new_value[i] = x1 ;
487  
488
489   // Creation of a Local Field
490
491   MESSAGE("CalculatorEngine::Constant Creation of the local field "<< totalLength);
492
493   TFieldDouble * fieldloc =  new TFieldDouble();
494   fieldloc -> allocValue(nb_comp,len_value);
495   fieldloc  -> setValue(new_value);
496   fieldloc  -> setName("-new_Const_Field-");
497   fieldloc  -> setDescription(field_description.in());
498   fieldloc -> setComponentsNames(component_name);
499   fieldloc -> setMEDComponentsUnits(component_unit);
500
501   delete [] new_value;
502   delete [] component_name;
503   delete [] component_unit;
504   mesh->Destroy();
505
506   // Control
507   int until_index = ( 20 > len_value)? len_value : 20 ;
508   PrintFieldValues (fieldloc,until_index);
509
510   // Creation of the new CORBA field
511
512   MESSAGE("CalculatorEngine::Constant Creation of the CORBA field");
513
514   SUPPORT *supportClt=new SUPPORTClient(FirstSupport);
515   fieldloc->setSupport(supportClt);
516   supportClt->removeReference();
517   TFieldDouble_i * NewField = new TFieldDouble_i(fieldloc,true) ;
518
519   SALOME_MED::FIELDDOUBLE_ptr myFieldIOR = NewField->_this() ;
520   
521   //sleep(4);
522
523   endService("CalculatorEngine::Const");
524   
525   END_OF("CalculatorEngine::Constant(SALOME_MED::FIELDDOUBLE_ptr FirstField,CORBA::Double x1)");
526   
527   return myFieldIOR;
528   
529 }
530
531 //================================================================================
532 //  write a field in a MED file
533 //================================================================================
534
535 static omni_mutex aWriteMEDfileMutex;
536 void CalculatorEngine::writeMEDfile(SALOME_MED::FIELDDOUBLE_ptr field, const char *filename)
537 {
538   omni_mutex_lock aLock(aWriteMEDfileMutex);
539
540   beginService("CalculatorEngine::writeMEDfile");
541   MESSAGE("fichier d'impression du champ resultat:"<<filename);
542
543   // get support of the field
544
545   SALOME_MED::SUPPORT_ptr support = field -> getSupport();
546   
547   // get mesh from this support
548   
549   SALOME_MED::MESH_ptr mesh = support -> getMesh();
550   
551   // write the mesh
552   
553   int index_m = mesh->addDriver(SALOME_MED::MED_DRIVER,filename,mesh->getName());
554
555   mesh -> write(index_m,"");
556
557   //write the field
558   
559   const char * LOC = "CalculatorEngine::writeMEDfile ";
560   
561   MESSAGE("fichier :"<<filename);
562
563   med_2_1::med_idt _medIdt = med_2_1::MEDouvrir(const_cast <char *> (filename) , med_2_1::MED_ECRI);
564   SCRUTE(_medIdt);
565
566   if (_medIdt<0) return;
567   
568   int err ;
569
570   int component_count= field->getNumberOfComponents();
571   string   component_name(component_count*MED_TAILLE_PNOM,' ') ;
572   string   component_unit(component_count*MED_TAILLE_PNOM,' ') ;
573   
574   for (int i=0; i < component_count ; i++) {
575     component_name.replace(i*MED_TAILLE_PNOM,MED_TAILLE_PNOM,(field -> getComponentName(i+1)),0,MED_TAILLE_PNOM);
576     component_unit.replace(i*MED_TAILLE_PNOM,MED_TAILLE_PNOM,(field -> getComponentUnit(i+1)),0,MED_TAILLE_PNOM);
577   }
578   
579   MESSAGE("component_name=|"<<component_name<<"|");
580   MESSAGE("component_unit=|"<<component_unit<<"|");
581   
582   // already existing ?
583   
584   char * champName = new char[MED_TAILLE_NOM+1] ;
585   char * compName,  * compUnit  ;
586   bool Find = false ;
587
588   int n = med_2_1::MEDnChamp(_medIdt,0);
589   int nbComp ;
590
591   med_2_1::med_type_champ type ;
592
593   for (int i = 1;  i <= n;  i++) 
594     {
595       nbComp = med_2_1::MEDnChamp(_medIdt,i);
596       compName = new char[MED_TAILLE_PNOM*nbComp+1];
597       compUnit = new char[MED_TAILLE_PNOM*nbComp+1];
598
599       err = med_2_1::MEDchampInfo(_medIdt,i,champName,&type,compName,compUnit,nbComp);
600       if (err == 0)
601         if (strcmp(champName, field->getName())==0) { // Found !
602           Find = true ;
603           break ;
604         }
605
606       delete[] compName ;
607       delete[] compUnit ;
608
609     }
610   if (Find) {
611     // the same ?
612     if (nbComp != component_count)
613       throw MEDEXCEPTION ( LOCALIZED (STRING(LOC)
614                                      <<": Field exist in file, but number of component are different : " \
615                                      <<nbComp<<" in file and "<<component_count<<" in CORBA object."
616                                      )
617                          );
618     // component name and unit
619     MESSAGE(LOC<<" Component name in file : "<<compName);
620     MESSAGE(LOC<<" Component name in CORBA object : "<<component_name);
621     MESSAGE(LOC<<" Component unit in file : "<<compUnit);
622     MESSAGE(LOC<<" Component unit in CORBA object : "<<component_unit);
623     delete[] compName ;
624     delete[] compUnit ;
625     
626   } else { 
627     // Verify the field doesn't exist
628     
629     string dataGroupName =  "/CHA/"+ string(field->getName());
630
631     MESSAGE(LOC << "|" << dataGroupName << "|" );
632
633     med_2_1::med_idt gid =  H5Gopen(_medIdt, dataGroupName.c_str() );
634     
635     if ( gid < 0 ) {
636       // create field :
637       err=med_2_1::MEDchampCr(_medIdt, 
638                              const_cast <char*> (field->getName()),
639                              med_2_1::MED_REEL64,
640                              const_cast <char*> ( component_name.c_str() ),
641                              const_cast <char*> ( component_unit.c_str() ),
642                              component_count);
643       if ( err < 0 )
644         throw MEDEXCEPTION( LOCALIZED (STRING(LOC) 
645                                        << ": Error MEDchampCr : "<<err
646                                        )
647                            );
648     }
649         else H5Gclose(gid);
650   }
651   
652   SALOME_MED::SUPPORT_ptr mySupport = field -> getSupport();
653   SCRUTE(mySupport);
654   
655   if (! mySupport->isOnAllElements())
656     throw MEDEXCEPTION ( LOCALIZED (STRING(LOC) 
657                                    <<": Field must be on all entity"
658                                     )
659                         );
660   
661   SALOME_MED::MESH_ptr myMesh = mySupport -> getMesh();
662   SCRUTE(mesh);
663
664   SALOME_MED::medGeometryElement_array* Types = mySupport->getTypes() ;
665
666   int NumberOfType = Types->length() ;
667
668   for ( int i = 0; i < NumberOfType; i++ ) 
669     {
670       int NumberOfElements   = mySupport->getNumberOfElements ( (*Types)[i] ) ;
671       //Temporarily commented to avoid compilation errors:int NumberOfGaussPoint = mySupport->getNumberOfGaussPoint ( (*Types)[i] ) ;
672       int NumberOfGaussPoint = 1; //Temporary line instead of the above one
673
674       MESSAGE( " " << field->getName() );
675       MESSAGE( " " << NumberOfElements );
676       //!!!tmp commented:MESSAGE( " " << NumberOfGaussPoint );
677       MESSAGE( "Attention! NumberOfGaussPoint is invalid: " << NumberOfGaussPoint ); //tmp line!!!
678       MESSAGE( " " << (int) (convertIdlEntToMedEnt(mySupport->getEntity())) );
679       MESSAGE( " " << (int)(convertIdlEltToMedElt((*Types)[i])) );
680       MESSAGE( " " << field->getIterationNumber() );
681       MESSAGE( " " << field->getTime() );
682       MESSAGE( " " << field->getOrderNumber() );
683       MESSAGE( "MEDchampEcr :" << myMesh->getName() );
684
685       SALOME_MED::double_array * value = field->getValue( SALOME_MED::MED_FULL_INTERLACE ) ;
686       double *locvalue = new double[NumberOfElements];
687       for (int k = 0; k < NumberOfElements; k++) 
688         locvalue[k] = (*value) [k];
689
690       err=med_2_1::MEDchampEcr(_medIdt, 
691                               const_cast <char*> (myMesh->getName()) , 
692                               const_cast <char*> (field->getName()),
693                               (unsigned char*)locvalue, 
694                               med_2_1::MED_FULL_INTERLACE,
695                               NumberOfElements,
696                               NumberOfGaussPoint,
697                               MED_ALL, 
698                               MED_NOPFL, 
699                               med_2_1::MED_REMP,  
700                               (med_2_1::med_entite_maillage)convertIdlEntToMedEnt(mySupport->getEntity()),
701                               (med_2_1::med_geometrie_element)(convertIdlEltToMedElt((*Types)[i])),
702                               field->getIterationNumber(),
703                               "        ",
704                               field->getTime(),
705                               field->getOrderNumber()
706                               );
707       delete locvalue;
708       
709       if (err < MED_VALID )
710         throw MEDEXCEPTION(LOCALIZED( STRING(LOC)
711                                      <<": Error in writing Field "<< field->getName() <<", type "<<(*Types)[i]
712                                      )
713                            );
714     }
715   END_OF( LOC );
716   
717   SCRUTE( err );
718   if (err < 0 ) return ;
719   
720   med_2_1::MEDfermer(_medIdt) ;
721   
722   endService("CalculatorEngine::writeMEDfile");
723   return ;
724   
725 }
726
727 extern "C"
728 {
729   //===========================================================================
730   // CalculatorEngine_factory
731   //===========================================================================
732   PortableServer::ObjectId * CalculatorEngine_factory
733      (CORBA::ORB_ptr orb,
734       PortableServer::POA_ptr poa, 
735       PortableServer::ObjectId * contId,
736       const char *instanceName,
737       const char *interfaceName)
738   {
739     MESSAGE("CalculatorEngine_factory CalculatorEngine ("
740             << instanceName << "," << interfaceName << ")");
741     CalculatorEngine * myCalculator 
742       = new CalculatorEngine(orb, poa, contId, instanceName, interfaceName);
743     return myCalculator->getId() ;
744   }
745 }
746
747