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