]> SALOME platform Git repositories - modules/med.git/blob - src/MEDSPLITTER/MEDSPLITTER_MESHCollectionMedXMLDriver.H
Salome HOME
Fix problem of make distcheck
[modules/med.git] / src / MEDSPLITTER / MEDSPLITTER_MESHCollectionMedXMLDriver.H
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #ifndef MEDSPLITTER_MESHCOLLECTIONMEDXMLDRIVER_H
20 #define MEDSPLITTER_MESHCOLLECTIONMEDXMLDRIVER_H
21
22
23 #include <libxml/tree.h>
24 #include <libxml/parser.h>
25 #include <libxml/xpath.h>
26 #include <libxml/xpathInternals.h>
27
28
29
30 /*!reads a distributed field
31  * 
32  * \param fields vector of fields (one field per subdomain)
33  * \param fieldname name of the field
34  * \param itnumber number of iteration
35  * \param ordernumber internal number inside the iteration
36  * */
37 template <class T>
38 void MESHCollectionMedXMLDriver::_readFields(vector<MEDMEM::FIELD<T>* >& fields,char* fieldname, int itnumber, int ordernumber)
39 {
40   for (unsigned i=0; i<_collection->getMesh().size(); i++)
41   {
42     char filename[256];
43     strcpy(filename,_filename[i].c_str());
44     cout << "maillage : " << filename << " champ : " << fieldname << endl;
45     //    MEDMEM::FIELD<T>* field = new MEDMEM::FIELD<T>(MEDMEM::MED_DRIVER,filename,fieldname,itnumber,ordernumber);
46     fields.push_back (new MEDMEM::FIELD<T>(MEDMEM::MED_DRIVER,filename,fieldname,itnumber,ordernumber));
47   }
48 }
49
50
51 /*!writes a distributed field
52  * 
53  * \param fields vector of fields (one field per subdomain)
54  * \param fieldname name of the field
55  * */
56 template <class T>
57 void MESHCollectionMedXMLDriver::_writeFields(vector<MEDMEM::FIELD<T>* >& fields,char* fieldname)
58 {
59   xmlDocPtr master_doc=xmlParseFile(_master_filename.c_str());
60
61   if (!master_doc)    
62     throw MEDEXCEPTION("MEDSPLITTER writeFields - Master File does not exist");
63
64   //number of domains
65
66   xmlXPathContextPtr xpathCtx = xmlXPathNewContext(master_doc);
67   xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(BAD_CAST "//mapping/mesh", xpathCtx);
68   //assuming there is only one mesh in the XML file
69   xmlNodePtr mesh_node= xpathObj->nodesetval->nodeTab[0];
70
71   //adds the field to the master file if necessary
72   bool exist_field =false;
73   xpathObj = xmlXPathEvalExpression(BAD_CAST "//mapping/mesh/field", xpathCtx);
74   //assuming there is only one mesh in the XML file
75   int field_nr =  xpathObj->nodesetval->nodeNr;
76   for (int i=0; i<field_nr; i++)
77   {
78     //field node has only one property
79     if ( strcmp((const char*)xpathObj->nodesetval->nodeTab[i]->properties->children->content, fieldname)==0)
80       exist_field = true;
81   }
82
83   xmlNodePtr field_node;
84   if (!exist_field)
85   {
86     field_node = xmlNewChild(mesh_node, 0, BAD_CAST "field",0);
87     xmlNewProp(field_node,BAD_CAST "name",BAD_CAST fieldname);
88   }
89
90
91   for (unsigned i=0; i<_collection->getMesh().size(); i++)
92   {
93     char filename[256];
94     strcpy(filename,_filename[i].c_str());
95     int driverid = fields[i]->addDriver(MEDMEM::MED_DRIVER, filename, fieldname);
96     fields[i]->write(driverid);
97
98     //adds the partition to the master file if the field had not been 
99     //added already
100     if (!exist_field)
101     {
102       xmlNodePtr chunk_node= xmlNewChild(field_node,0,BAD_CAST "chunk",0);
103       char id[8];
104       sprintf(id,"%d",i+1);
105       xmlNewProp(chunk_node,BAD_CAST "subdomain",BAD_CAST id);
106       //xmlNewProp(chunk_node,BAD_CAST "name", BAD_CAST fieldname);
107       //xmlNodePtr fieldname_node=
108       xmlNewChild(chunk_node,0,BAD_CAST "name", BAD_CAST fieldname);
109     }
110   }
111   //Writing file
112   xmlKeepBlanksDefault(0);
113   xmlSaveFormatFileEnc(_master_filename.c_str(), master_doc, "UTF-8", 1);
114
115   //Clean up
116   xmlXPathFreeContext(xpathCtx); 
117   xmlFreeDoc(master_doc);
118   //xmlCleanupParser();
119
120 }
121
122 #endif