Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/med.git] / src / MEDSPLITTER / MEDSPLITTER_MESHCollectionMedXMLDriver.H
1 #ifndef MEDSPLITTER_MESHCOLLECTIONMEDXMLDRIVER_H
2 #define MEDSPLITTER_MESHCOLLECTIONMEDXMLDRIVER_H
3
4
5 #include <libxml/tree.h>
6 #include <libxml/parser.h>
7 #include <libxml/xpath.h>
8 #include <libxml/xpathInternals.h>
9
10
11
12 /*!reads a distributed field
13  * 
14  * \param fields vector of fields (one field per subdomain)
15  * \param fieldname name of the field
16  * \param itnumber number of iteration
17  * \param ordernumber internal number inside the iteration
18  * */
19 template <class T>
20 void MESHCollectionMedXMLDriver::_readFields(vector<MEDMEM::FIELD<T>* >& fields,char* fieldname, int itnumber, int ordernumber)
21 {
22         for (int i=0; i<m_collection->getMesh().size(); i++)
23                 {
24                         char filename[256];
25                         strcpy(filename,m_filename[i].c_str());
26                         cout << "maillage : " << filename << " champ : " << fieldname << endl;
27                         //              MEDMEM::FIELD<T>* field = new MEDMEM::FIELD<T>(MEDMEM::MED_DRIVER,filename,fieldname,itnumber,ordernumber);
28                         fields.push_back (new MEDMEM::FIELD<T>(MEDMEM::MED_DRIVER,filename,fieldname,itnumber,ordernumber));
29                 }
30 }
31
32
33 /*!writes a distributed field
34  * 
35  * \param fields vector of fields (one field per subdomain)
36  * \param fieldname name of the field
37  * */
38 template <class T>
39 void MESHCollectionMedXMLDriver::_writeFields(vector<MEDMEM::FIELD<T>* >& fields,char* fieldname)
40 {
41         xmlDocPtr master_doc=xmlParseFile(m_master_filename.c_str());
42         
43         if (!master_doc)    
44                 throw MEDEXCEPTION("MEDSPLITTER writeFields - Master File does not exist");
45         
46         //number of domains
47         
48         xmlXPathContextPtr xpathCtx = xmlXPathNewContext(master_doc);
49         xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(BAD_CAST "//mapping/mesh", xpathCtx);
50         //assuming there is only one mesh in the XML file
51         xmlNodePtr mesh_node= xpathObj->nodesetval->nodeTab[0];
52
53         //adds the field to the master file if necessary
54         bool exist_field =false;
55         xpathObj = xmlXPathEvalExpression(BAD_CAST "//mapping/mesh/field", xpathCtx);
56         //assuming there is only one mesh in the XML file
57         int field_nr =  xpathObj->nodesetval->nodeNr;
58         for (int i=0; i<field_nr; i++)
59                 {
60                         //field node has only one property
61                         if ( strcmp((const char*)xpathObj->nodesetval->nodeTab[i]->properties->children->content, fieldname)==0)
62                                 exist_field = true;
63                 }
64
65         xmlNodePtr field_node;
66         if (!exist_field)
67                 {
68                         field_node = xmlNewChild(mesh_node, 0, BAD_CAST "field",0);
69                         xmlNewProp(field_node,BAD_CAST "name",BAD_CAST fieldname);
70                 }
71         
72         
73         for (int i=0; i<m_collection->getMesh().size(); i++)
74                 {
75                         char filename[256];
76                         strcpy(filename,m_filename[i].c_str());
77                         int driverid = fields[i]->addDriver(MEDMEM::MED_DRIVER, filename, fieldname);
78                         fields[i]->write(driverid);
79
80                         //adds the partition to the master file if the field had not been 
81                         //added already
82                         if (!exist_field)
83                         {
84                           xmlNodePtr chunk_node= xmlNewChild(field_node,0,BAD_CAST "chunk",0);
85                           char id[8];
86                           sprintf(id,"%d",i+1);
87                           xmlNewProp(chunk_node,BAD_CAST "subdomain",BAD_CAST id);
88                           //xmlNewProp(chunk_node,BAD_CAST "name", BAD_CAST fieldname);
89                           //xmlNodePtr fieldname_node=
90                           xmlNewChild(chunk_node,0,BAD_CAST "name", BAD_CAST fieldname);
91                         }
92                 }
93   //Writing file
94         xmlKeepBlanksDefault(0);
95         xmlSaveFormatFileEnc(m_master_filename.c_str(), master_doc, "UTF-8", 1);
96   
97         //Clean up
98         xmlXPathFreeContext(xpathCtx); 
99   xmlFreeDoc(master_doc);
100   xmlCleanupParser();
101
102 }
103
104 #endif