]> SALOME platform Git repositories - tools/hxx2salome.git/blob - CppExamples/COMPO_CXX_SRC/src/CALCULATOR/CALCULATOR_CXX/Calculator.cxx
Salome HOME
GUI developement
[tools/hxx2salome.git] / CppExamples / COMPO_CXX_SRC / src / CALCULATOR / CALCULATOR_CXX / Calculator.cxx
1 #include "Calculator.hxx"
2 #include "MEDMEM_Field.hxx"
3 #include "MEDMEM_Mesh.hxx"
4 #include <string>
5
6 using namespace std;
7 using namespace MED_EN;
8
9 CALCULATOR::CALCULATOR()
10 {
11     string ressourceDir(getenv("MED_ROOT_DIR"));
12     ressourceDir+="/share/salome/resources/";
13     const string FileName(ressourceDir+"pointe.med");
14     const string FieldName("fieldcelldouble");
15     const string MeshName("maa1");
16     try 
17     {
18         myMesh_ = new MEDMEM::MESH(MEDMEM::MED_DRIVER,FileName.c_str(),MeshName.c_str());
19         cout<<"Lecture du Maillage Source : " << myMesh_->getName() << endl << flush; 
20         cout<<"Construction du support    : "<< endl << flush; 
21         MEDMEM::SUPPORT* fromSupport=new MEDMEM::SUPPORT(myMesh_,"XsupportX",MED_CELL);
22         myField_ = new MEDMEM::FIELD<double>(fromSupport,MEDMEM::MED_DRIVER,FileName.c_str(),FieldName.c_str());
23         cout<<"Lecture du champ Source    : " << myField_->getName() << endl << flush;
24         cout<<"Fin Constructeur TESTMED"<<endl<<flush;
25     } 
26     catch (MEDMEM::MEDEXCEPTION& ex)
27     {
28         cout << ex.what() ;
29     }
30 }
31
32 CALCULATOR::~CALCULATOR()
33 {
34     if (myMesh_)
35         delete myMesh_;
36     if (myField_)
37         delete myField_;
38 }
39
40 MEDMEM::FIELD<double>* CALCULATOR::createConstField(double value)
41 {
42     MEDMEM::FIELD<double>* myField=new MEDMEM::FIELD<double>(myField_->getSupport(),1); 
43     for(int i=0; i!=myField_->getNumberOfValues();++i)
44         myField->setValueIJ(i+1,1,value);
45     myField->setName("Const field");
46     return myField;
47 }
48
49 double CALCULATOR::norm2(const MEDMEM::FIELD<double>& field)
50 {
51     return field.norm2();
52 }
53
54 double CALCULATOR::normMax(const MEDMEM::FIELD<double>& field)
55 {
56     return field.normMax();
57 }
58
59 MEDMEM::FIELD<double>* CALCULATOR::add(const MEDMEM::FIELD<double>& field1, const MEDMEM::FIELD<double>& field2)
60 {
61     return new MEDMEM::FIELD<double>(field1+field2);
62 }
63
64 void CALCULATOR::printField(const MEDMEM::FIELD<double>* myField)
65 {
66     const MEDMEM::SUPPORT * mySupport = myField->getSupport();
67     std::cout << "\n------------------ Field "<< myField->getName() << " : " <<myField->getDescription() << "------------------" <<  std::endl ;
68     int NumberOfComponents = myField->getNumberOfComponents() ;
69     std::cout << "- Type : " << mySupport->getEntity() << std::endl;
70     std::cout << "- Nombre de composantes : "<< NumberOfComponents << std::endl ;
71     std::cout << "- Nombre de valeurs     : "<< myField->getNumberOfValues() << std::endl ;
72     for (int i=1; i<NumberOfComponents+1; i++) {
73         std::cout << "  - composante "<<i<<" :"<<std::endl ;
74         std::cout << "      - nom         : "<<myField->getComponentName(i)<< std::endl;
75         std::cout << "      - description : "<<myField->getComponentDescription(i) << std::endl;
76         std::cout << "      - units       : "<<myField->getMEDComponentUnit(i) << std::endl;
77     }
78     std::cout << "- iteration :" << std::endl ;
79     std::cout << "    - numero : " << myField->getIterationNumber()<< std::endl  ;
80     std::cout << "    - ordre  : " << myField->getOrderNumber()<< std::endl  ;
81     std::cout << "    - temps  : " << myField->getTime()<< std::endl  ;
82     std::cout << "- Type : " << myField->getValueType()<< std::endl;
83     
84     std::cout << "- Valeurs :"<<std::endl;
85     int NumberOf = mySupport->getNumberOfElements(MED_ALL_ELEMENTS);
86
87     //mySupport->isOnAllElements()
88     bool displayNode = mySupport->isOnAllElements() && mySupport->getEntity()==MED_NODE;
89     bool displayBary = mySupport->isOnAllElements() && mySupport->getEntity()==MED_CELL;
90     int dim_space = mySupport->getMesh()->getSpaceDimension();
91     const double * coord = mySupport->getMesh()->getCoordinates(MED_FULL_INTERLACE);
92
93     MEDMEM::FIELD<double>* barycenter(0);
94     if(displayBary)
95         barycenter = mySupport->getMesh()->getBarycenter(mySupport);
96
97     for (int i=1; i<NumberOf+1; i++) {
98         const double * value = myField->getValueI(MED_FULL_INTERLACE,i) ;
99         if(displayNode)
100         {
101             int N=(i-1)*dim_space;
102             std::cout << std::setw(5) << coord[N] << " " << std::setw(5) << coord[N+1]<<  " " << std::setw(5) << coord[N+2] << "  : " ;
103         }
104         if(displayBary)
105             std::cout << std::setw(5) << barycenter->getValueIJ(i,1) << " " << std::setw(5) << barycenter->getValueIJ(i,2) 
106                  <<  " " << std::setw(5) << barycenter->getValueIJ(i,3) << "  : " ;
107         for (int j=0; j<NumberOfComponents; j++)
108             std::cout << value[j]<< " ";
109         std::cout<<std::endl;
110     }
111     std::cout << std::endl;
112     std::cout << "Norme euclidienne : " << myField->norm2() << std::endl;
113     std::cout << "Norme max         : " << myField->normMax() << std::endl;
114     std::cout << "------------------------------------------------------------------------" << std::endl << std::endl << std::flush;
115 }
116