]> SALOME platform Git repositories - samples/component.git/blob - src/CalculatorComponent/CalculatorEngine.cxx
Salome HOME
Merge from V6_main_20120808 08Aug12
[samples/component.git] / src / CalculatorComponent / CalculatorEngine.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : CalculatorEngine.cxx
24 //  Author : Laurent DADA, CEA
25 //  Module : CalculatorComponent
26 //  $Header$
27 //
28 #include <unistd.h>
29
30 #include <iostream>
31 #include <sstream>
32
33 #include "CalculatorEngine.hxx"
34 #include "MEDMEM_Support_i.hxx"
35 #include "SUPPORTClient.hxx"
36
37 #include "MEDMEM_define.hxx"
38 #include "MEDMEM_STRING.hxx"
39 #include "MEDMEM_Exception.hxx"
40 #include "MEDMEM_Unit.hxx"
41
42 #include "utilities.h"
43
44 using namespace MEDMEM;
45 using namespace std;
46
47 #ifndef MED_TAILLE_PNOM
48 #define MED_TAILLE_PNOM MED_SNAME_SIZE
49 #endif
50
51 typedef FIELD<double,MEDMEM::FullInterlace> TFieldDouble;
52 typedef FIELDTEMPLATE_I<double,MEDMEM::FullInterlace> TFieldDouble_i;
53
54 //================================================================================
55 // static PrintFieldValues - shows field contents
56 //================================================================================
57
58 static void PrintFieldValues (TFieldDouble * field, int until_index) 
59 {
60   const double * values = field -> getValue();
61   int nb_comp     = field -> getNumberOfComponents();
62   MESSAGE( "Field                    : " << field -> getName() );
63   MESSAGE( "    Description          : " << field -> getDescription() );
64   MESSAGE( "    Number of components : " << nb_comp );
65
66   for (int i=0 ; i<nb_comp ; i++) {
67     MESSAGE ( "       Component " << field -> getComponentName(i+1) << endl \
68              << "                   Unit        : " << field -> getMEDComponentUnit(i+1) << endl \
69              << "                   Description : " << field -> getComponentDescription(i+1) );
70   }
71   MESSAGE( "    First " << until_index << " values   : " );
72   
73   for (int j=0 ; j < until_index ; j++) {
74     for (int i=0;i<nb_comp;i++) MESSAGE( values[i+j*nb_comp] << " " );
75   }
76 }
77
78
79 //================================================================================
80 // Constructors
81 //================================================================================
82
83 CalculatorEngine::CalculatorEngine(CORBA::ORB_ptr orb,
84                                    PortableServer::POA_ptr poa,
85                                    PortableServer::ObjectId * contId, 
86                                    const char *instanceName,
87                                    const char *interfaceName) :
88                                    Engines_Component_i(orb, poa, contId, instanceName, interfaceName,true)
89 {
90   MESSAGE("CalculatorEngine::CalculatorEngine activate object instanceName("
91           << instanceName << ") interfaceName(" << interfaceName << ")" )
92     _thisObj = this ;
93   _id = _poa->activate_object(_thisObj);
94   SALOME_NamingService *NS = new SALOME_NamingService(orb);
95   _NS=NS;
96 }
97
98 CalculatorEngine::CalculatorEngine()
99 {
100 }
101
102 //================================================================================
103 // Destructors
104 //================================================================================
105
106 CalculatorEngine::~CalculatorEngine()
107 {
108   delete _NS;
109 }
110
111 static omni_mutex aPutToStudyMutex;
112 SALOME_MED::FIELDDOUBLE_ptr CalculatorEngine::PutToStudy(SALOME_MED::FIELDDOUBLE_ptr theField1,
113                                                          CORBA::Long theStudyId) {
114   omni_mutex_lock aLock(aPutToStudyMutex);
115
116   CORBA::Object_var anObj = _NS->Resolve("/myStudyManager");
117   SALOMEDS::StudyManager_var aManager  = SALOMEDS::StudyManager::_narrow(anObj);
118 //   SALOMEDS::ListOfOpenStudies_var aList = aManager->GetOpenStudies();
119 //   SALOMEDS::Study_var aStudy = aManager->GetStudyByName(aList[0]);
120   SALOMEDS::Study_var aStudy = aManager->GetStudyByID(theStudyId);
121   SALOMEDS::StudyBuilder_var aBuilder = aStudy->NewBuilder();
122
123   SALOMEDS::SComponent_var aComponent = aStudy->FindComponent("Calculator");
124   if (aComponent->_is_nil()) aComponent = aBuilder->NewComponent("Calculator");
125   SALOMEDS::GenericAttribute_var anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
126   SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
127   aName->SetValue("Calculator");
128   
129   SALOMEDS::SObject_var aSO = aBuilder->NewObject(aComponent);
130   anAttr = aBuilder->FindOrCreateAttribute(aSO, "AttributeName");
131   aName = SALOMEDS::AttributeName::_narrow(anAttr);
132   char aFieldName[10];
133   sprintf(aFieldName, "Field_%d", (int)aSO->Tag());
134   aName->SetValue(strdup(aFieldName));
135   
136   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
137   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
138   CORBA::ORB_var &orb = init(0,0);
139
140   string iorStr = orb->object_to_string(theField1);
141   anAttr = aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
142   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
143   anIOR->SetValue(iorStr.c_str());
144
145   return SALOME_MED::FIELDDOUBLE::_duplicate(theField1);
146 }
147
148
149 //=======================================================================================
150 //  Addition of 2 FieldNodeDouble (based on Med library)
151 //
152 //  We assume that :
153 //     - support
154 //     - number of components
155 //     - name, type, unit and description of components
156 //  are the same for the 2 fields.
157 //
158 //  Otherwise,
159 //  the result will be a copy of the first field.
160 //
161 //=======================================================================================
162
163 static omni_mutex anAddMutex;
164 SALOME_MED::FIELDDOUBLE_ptr CalculatorEngine::Add(SALOME_MED::FIELDDOUBLE_ptr FirstField,
165                                                   SALOME_MED::FIELDDOUBLE_ptr SecondField)
166 {
167   omni_mutex_lock aLock(anAddMutex);
168
169   ostringstream tmp;
170   bool          same_support = true;
171   
172   beginService("CalculatorEngine::Add");
173   
174   sendMessage(NOTIF_TRACE, "CalculatorEngine::Add is Computing");
175   
176   const char* LOC = "CalculatorEngine::Add(SALOME_MED::FIELDDOUBLE_ptr FirstField,SALOME_MED::FIELDDOUBLE_ptr SecondField)";
177   BEGIN_OF(LOC);
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::GMESH_var mesh1 = support1 -> getMesh();
233   SALOME_MED::GMESH_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_TYPES::ListOfDouble * first_value = FirstField -> getValue(SALOME_MED::MED_FULL_INTERLACE);
258   SALOME_TYPES::ListOfDouble * 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->UnRegister();
303   mesh2->UnRegister();
304   support2->UnRegister();
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(LOC);
327
328   endService("CalculatorEngine::Add");
329   return myFieldIOR;
330 }
331
332
333 //===============================================================================
334 //  Multiplication of a FieldNodeDouble by a double value (based on Med library)
335 //===============================================================================
336
337 static omni_mutex aMulMutex;
338 SALOME_MED::FIELDDOUBLE_ptr CalculatorEngine::Mul(SALOME_MED::FIELDDOUBLE_ptr OldField,
339                                                   CORBA::Double x1)
340 {
341   omni_mutex_lock aLock(aMulMutex);
342
343   beginService("CalculatorEngine::Mul");
344   const char* LOC = "CalculatorEngine::Mul(SALOME_MED::FIELDDOUBLE_ptr OldField,CORBA::Double x1)";
345   BEGIN_OF(LOC);
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::GMESH_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_TYPES::ListOfDouble * 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->UnRegister();
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(LOC);
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   const char* LOC = "CalculatorEngine::Constant(SALOME_MED::FIELDDOUBLE_ptr FirstField,CORBA::Double x1)";
444   BEGIN_OF(LOC);
445
446   // Name and description of field
447   CORBA::String_var field_name        = FirstField -> getName();
448   CORBA::String_var field_description = FirstField -> getDescription();
449
450   SALOME_MED::SUPPORT_var FirstSupport = FirstField -> getSupport();
451
452   // Number of components
453   int nb_comp  = FirstField -> getNumberOfComponents();
454
455   // Type, name, unit and description of components
456   string * component_name = new string[nb_comp];
457   string * component_unit = new string[nb_comp];
458
459   for (int i = 0 ; i<nb_comp ; i++) {
460     CORBA::String_var name = FirstField -> getComponentName(i+1);
461     component_name[i] = name.in();
462     CORBA::String_var unit = FirstField -> getComponentUnit(i+1);
463     component_unit[i] = unit.in();
464   }
465
466   MESSAGE("CalculatorEngine::Constant Number of entities in the Support of the field");
467
468   // Number of entities in the Support of the field
469   int len_value = 0;
470
471   SCRUTE(FirstSupport);
472
473   SALOME_MED::GMESH_var mesh = FirstSupport -> getMesh();
474
475   SCRUTE(mesh);
476
477   if ( FirstSupport -> isOnAllElements() )
478     len_value = mesh -> getNumberOfElements(FirstSupport -> getEntity(),SALOME_MED::MED_ALL_ELEMENTS);
479   else
480     len_value = FirstSupport -> getNumberOfElements(SALOME_MED::MED_ALL_ELEMENTS);
481
482   // Values of new field
483   int totalLength = nb_comp*len_value;
484   double * new_value = new double[totalLength];
485
486   for(int i=0 ; i<totalLength ; i++) 
487     new_value[i] = x1 ;
488  
489
490   // Creation of a Local Field
491
492   MESSAGE("CalculatorEngine::Constant Creation of the local field "<< totalLength);
493
494   TFieldDouble * fieldloc =  new TFieldDouble();
495   fieldloc -> allocValue(nb_comp,len_value);
496   fieldloc  -> setValue(new_value);
497   fieldloc  -> setName("-new_Const_Field-");
498   fieldloc  -> setDescription(field_description.in());
499   fieldloc -> setComponentsNames(component_name);
500   fieldloc -> setMEDComponentsUnits(component_unit);
501
502   delete [] new_value;
503   delete [] component_name;
504   delete [] component_unit;
505   mesh->UnRegister();
506
507   // Control
508   int until_index = ( 20 > len_value)? len_value : 20 ;
509   PrintFieldValues (fieldloc,until_index);
510
511   // Creation of the new CORBA field
512
513   MESSAGE("CalculatorEngine::Constant Creation of the CORBA field");
514
515   SUPPORT *supportClt=new SUPPORTClient(FirstSupport);
516   fieldloc->setSupport(supportClt);
517   supportClt->removeReference();
518   TFieldDouble_i * NewField = new TFieldDouble_i(fieldloc,true) ;
519
520   SALOME_MED::FIELDDOUBLE_ptr myFieldIOR = NewField->_this() ;
521   
522   //sleep(4);
523
524   endService("CalculatorEngine::Const");
525   
526   END_OF(LOC);
527   
528   return myFieldIOR;
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   const char * LOC = "CalculatorEngine::writeMEDfile ";
541
542   beginService("CalculatorEngine::writeMEDfile");
543   BEGIN_OF(LOC);
544   MESSAGE("fichier d'impression du champ resultat:"<<filename);
545
546   // get support of the field
547
548   SALOME_MED::SUPPORT_ptr support = field -> getSupport();
549   
550   // get mesh from this support
551   
552   SALOME_MED::GMESH_ptr mesh = support -> getMesh();
553   
554   // write the mesh
555   
556   int index_m = mesh->addDriver(SALOME_MED::MED_DRIVER,filename,mesh->getName());
557
558   mesh -> write(index_m,"");
559
560   //write the field
561   
562   MESSAGE("fichier :"<<filename);
563
564   med_2_3::med_idt _medIdt = med_2_3::MEDfileOpen(const_cast <char *> (filename) , med_2_3::MED_ACC_RDWR);
565   SCRUTE(_medIdt);
566
567   if (_medIdt<0) return;
568   
569   int err ;
570
571   int component_count= field->getNumberOfComponents();
572   string   component_name(component_count*MED_TAILLE_PNOM,' ') ;
573   string   component_unit(component_count*MED_TAILLE_PNOM,' ') ;
574   
575   for (int i=0; i < component_count ; i++) {
576     component_name.replace(i*MED_TAILLE_PNOM,MED_TAILLE_PNOM,(field -> getComponentName(i+1)),0,MED_TAILLE_PNOM);
577     component_unit.replace(i*MED_TAILLE_PNOM,MED_TAILLE_PNOM,(field -> getComponentUnit(i+1)),0,MED_TAILLE_PNOM);
578   }
579   
580   MESSAGE("component_name=|"<<component_name<<"|");
581   MESSAGE("component_unit=|"<<component_unit<<"|");
582   
583   // already existing ?
584   
585   char * champName  = new char[MED_NAME_SIZE+1] ;
586   char * meshName   = new char[MED_NAME_SIZE+1] ;
587   char * compDtunit = new char[MED_SNAME_SIZE+1];
588   char * compName,  * compUnit;
589   bool Find = false ;
590
591   int n = med_2_3::MEDnField(_medIdt);
592   
593   med_2_3::med_int nbComp ;
594   med_2_3::med_field_type type ;
595   med_2_3::med_bool _local;
596
597   for (int i = 1;  i <= n;  i++) 
598   {
599     nbComp = med_2_3::MEDnField(_medIdt);
600     compName   = new char[MED_TAILLE_PNOM*nbComp+1];
601     compUnit   = new char[MED_TAILLE_PNOM*nbComp+1];
602     
603     err = med_2_3::MEDfieldInfo(_medIdt,i,champName,meshName,&_local,&type,compName,compUnit,compDtunit,&nbComp);
604     if (err == 0) {
605       if (strcmp(champName, field->getName())==0) { // Found !
606         Find = true ;
607         break ;
608       }
609     }
610     delete[] compName ;
611     delete[] compUnit ;
612   }
613   
614   
615   if (Find)
616   {
617     // the same ?
618     if (nbComp != component_count)
619       throw MEDEXCEPTION ( LOCALIZED (STRING(LOC)
620                           <<": Field exist in file, but number of component are different : " \
621                           <<nbComp<<" in file and "<<component_count<<" in CORBA object."
622                           )
623                           );
624                           
625     // component name and unit
626     MESSAGE(LOC<<" Component name in file : "<<compName);
627     MESSAGE(LOC<<" Component name in CORBA object : "<<component_name);
628     MESSAGE(LOC<<" Component unit in file : "<<compUnit);
629     MESSAGE(LOC<<" Component unit in CORBA object : "<<component_unit);
630     delete[] compName ;
631     delete[] compUnit ;
632
633   }
634   else
635   {
636     // Verify the field doesn't exist
637     string dataGroupName =  "/CHA/"+ string(field->getName());
638     
639     MESSAGE(LOC << "|" << dataGroupName << "|" );
640     
641     med_2_3::med_idt gid =  H5Gopen(_medIdt, dataGroupName.c_str() );
642     
643     if ( gid < 0 )
644     {
645       // create field :
646       err=med_2_3::MEDfieldCr(_medIdt,
647                               const_cast <char*> (field->getName()),
648                               med_2_3::MED_FLOAT64,
649                               component_count,
650                               const_cast <char*> ( component_name.c_str() ),
651                               const_cast <char*> ( component_unit.c_str() ),
652                               compDtunit,
653                               meshName);
654       if ( err < 0 )
655         throw MEDEXCEPTION( LOCALIZED (STRING(LOC) << ": Error MEDfieldCr : "<<err ));
656     }
657     else
658       H5Gclose(gid);
659   }
660   
661   SALOME_MED::SUPPORT_ptr mySupport = field -> getSupport();
662   SCRUTE(mySupport);
663   
664   if (! mySupport->isOnAllElements())
665     throw MEDEXCEPTION ( LOCALIZED (STRING(LOC) 
666                         <<": Field must be on all entity"
667                         )
668                         );
669   SALOME_MED::MESH_ptr myMesh = mySupport -> getMesh()->convertInMESH();
670   SCRUTE(mesh);
671
672   SALOME_MED::medGeometryElement_array* Types = mySupport->getTypes() ;
673
674   int NumberOfType = Types->length() ;
675
676   for ( int i = 0; i < NumberOfType; i++ ) 
677   {
678     int NumberOfElements   = mySupport->getNumberOfElements ( (*Types)[i] ) ;
679     //Temporarily commented to avoid compilation errors:int NumberOfGaussPoint = mySupport->getNumberOfGaussPoint ( (*Types)[i] ) ;
680     int NumberOfGaussPoint = 1; //Temporary line instead of the above one
681
682     MESSAGE( " " << field->getName() );
683     MESSAGE( " " << NumberOfElements );
684     //!!!tmp commented:MESSAGE( " " << NumberOfGaussPoint );
685     MESSAGE( "Attention! NumberOfGaussPoint is invalid: " << NumberOfGaussPoint ); //tmp line!!!
686     MESSAGE( " " << (int) (convertIdlEntToMedEnt(mySupport->getEntity())) );
687     MESSAGE( " " << (int)(convertIdlEltToMedElt((*Types)[i])) );
688     MESSAGE( " " << field->getIterationNumber() );
689     MESSAGE( " " << field->getTime() );
690     MESSAGE( " " << field->getOrderNumber() );
691     MESSAGE( "MEDchampEcr :" << myMesh->getName() );
692
693     SALOME_TYPES::ListOfDouble * value = field->getValue( SALOME_MED::MED_FULL_INTERLACE ) ;
694     double *locvalue = new double[NumberOfElements];
695     for (int k = 0; k < NumberOfElements; k++) 
696       locvalue[k] = (*value) [k];
697     
698     err=med_2_3::MEDfieldValueWithProfileWr(_medIdt,
699                                             const_cast <char*> (field->getName()),
700                                             MED_NO_DT,
701                                             MED_NO_IT,
702                                             field->getTime(),
703                                             (med_2_3::med_entity_type)convertIdlEntToMedEnt(mySupport->getEntity()),
704                                             (med_2_3::med_geometry_type)(convertIdlEltToMedElt((*Types)[i])),
705                                             med_2_3::MED_COMPACT_PFLMODE,
706                                             MED_ALLENTITIES_PROFILE,
707                                             " ",
708                                             med_2_3::MED_FULL_INTERLACE,
709                                             SALOME_MED::MED_ALL_ELEMENTS,
710                                             1,
711                                             (unsigned char*)locvalue
712                                             );
713
714     delete locvalue;
715     
716     if (err < MED_VALID )
717       throw MEDEXCEPTION(LOCALIZED( STRING(LOC)
718       <<": Error in writing Field "<< field->getName() <<", type "<<(*Types)[i]
719       )
720       );
721   }
722   END_OF(LOC);
723
724   SCRUTE( err );
725   if (err < 0 ) return;
726
727   med_2_3::MEDfileClose(_medIdt);
728
729   endService("CalculatorEngine::writeMEDfile");
730   return;
731 }
732
733 extern "C"
734 {
735   //===========================================================================
736   // CalculatorEngine_factory
737   //===========================================================================
738   PortableServer::ObjectId * CalculatorEngine_factory
739      (CORBA::ORB_ptr orb,
740       PortableServer::POA_ptr poa, 
741       PortableServer::ObjectId * contId,
742       const char *instanceName,
743       const char *interfaceName)
744   {
745     MESSAGE("CalculatorEngine_factory CalculatorEngine ("
746             << instanceName << "," << interfaceName << ")");
747     CalculatorEngine * myCalculator 
748       = new CalculatorEngine(orb, poa, contId, instanceName, interfaceName);
749     return myCalculator->getId() ;
750   }
751 }