G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsXml.hpp
Go to the documentation of this file.
1 
14 #include <sstream>
15 #include <gsCore/gsLinearAlgebra.h>
16 #include <gsIO/gsIOUtils.h>
17 
18 namespace gismo {
19 
20 namespace internal {
21 
22 /*
23 template<class Object>
24 std::string gsXml<Object>::tag ()
25 {
26  // Next line will produce compile-time error
27  // if gsXml is not specialized for Object
28  Object::Object_does_not_exist_ERROR;
29  return "";
30 }
31 */
32 
33 template<class T>
34 gsXmlNode * makeNode( const std::string & name,
35  const gsMatrix<T> & value, gsXmlTree & data,
36  bool transposed)
37 {
38  return data.allocate_node(rapidxml::node_element ,
39  data.allocate_string(name.c_str() ),
40  makeValue(value,data,transposed) );
41 }
42 
43 template<class T>
44 char * makeValue(const gsMatrix<T> & value, gsXmlTree & data,
45  bool transposed)
46 {
47  std::ostringstream oss;
48  // Set precision
49  oss << std::setprecision(data.getFloatPrecision()) << "\n";
50 
51  // Read/Write is RowMajor
52  if ( transposed )
53  for ( index_t j = 0; j< value.cols(); ++j)
54  {
55  for ( index_t i = 0; i< value.rows(); ++i)
56  oss << value(i,j) <<" ";
57  oss << "\n";
58  }
59  else
60  for ( index_t i = 0; i< value.rows(); ++i)
61  {
62  for ( index_t j = 0; j< value.cols(); ++j)
63  oss << value(i,j) <<" ";
64  oss << "\n";
65  }
66 
67  return data.allocate_string( oss.str().c_str() );
68 }
69 
70 template <class T>
71 void getMatrixFromXml(gsXmlNode* node, unsigned const& rows,
72  unsigned const& cols, gsMatrix<T>& result,
73  const std::string& base_type_flag) {
74  // Make sure that flag is in lower case for comparisons
75  std::string base_type_flag_;
76  base_type_flag_.reserve(base_type_flag.size());
77  std::transform(base_type_flag.cbegin(), base_type_flag.cend(),
78  std::back_inserter(base_type_flag_),
79  [](unsigned char c) { return std::tolower(c); });
80  if (base_type_flag_ == "ascii") {
81  std::istringstream str;
82  str.str(node->value());
83  result.resize(rows, cols);
84  for (unsigned i = 0; i < rows; ++i) // Read is RowMajor
85  for (unsigned j = 0; j < cols; ++j)
86  // if ( !(str >> result(i,j) ) )
87  if (!gsGetValue(str, result(i, j))) {
88  gsWarn << "XML Warning: Reading matrix of size " << rows
89  << "x" << cols << " failed.\n";
90  gsWarn << "Tag: " << node->name() << ", Matrix entry: ("
91  << i << ", " << j << ").\n";
92  return;
93  }
94  } else {
95  // Read the node-value as the given type and cast into the requested
96  // Matrix Scalar Type (T)
97  result.resize(rows, cols);
98  Base64::DecodeIntoGsType(node->value(), base_type_flag_, result);
99  }
100 }
101 
102 template<class T>
103 gsXmlNode * putMatrixToXml ( gsMatrix<T> const & mat, gsXmlTree & data, std::string name)
104 {
105  // Create XML tree node
106  gsXmlNode* new_node = internal::makeNode(name, mat, data);
107  return new_node;
108 }
109 
110 template<class T>
111 gsXmlNode * putSparseMatrixToXml ( gsSparseMatrix<T> const & mat,
112  gsXmlTree & data, std::string name)
113 {
114  typedef typename gsSparseMatrix<T>::InnerIterator cIter;
115 
116  std::ostringstream str;
117  str << std::setprecision(data.getFloatPrecision());
118  const index_t nCol = mat.cols();
119 
120  for (index_t j=0; j != nCol; ++j) // for all columns
121  for ( cIter it(mat,j); it; ++it ) // for all non-zeros in column
122  {
123  // Write the matrix entry
124  str <<it.index() <<" "<<j<<" " << it.value() << "\n";
125  }
126 
127  // Create XML tree node
128  gsXmlNode* new_node = internal::makeNode(name, str.str(), data);
129  return new_node;
130 }
131 
132 template<class T>
133 void getSparseEntriesFromXml ( gsXmlNode * node,
134  gsSparseEntries<T> & result )
135 {
136  result.clear();
137 
138  std::istringstream str;
139  str.str( node->value() );
140  index_t r,c;
141  T val;
142 
143  //while( (str >> r) && (str >> c) && (str >> val) )
144  while( (str >> r) && (str >> c) && ( gsGetValue(str,val)) )
145  result.add(r,c,val);
146 }
147 
148 }// end namespace internal
149 
150 }// end namespace gismo
Class that provides a container for triplets (i,j,value) to be filled in a sparse matrix...
Definition: gsSparseMatrix.h:33
static void DecodeIntoGsType(const std::string &base64_string, const std::string &base_type_flag_, gsMatrix< ScalarType > &result)
Decode a string and copy into requested gismo Type.
Definition: gsBase64.h:355
#define index_t
Definition: gsConfig.h:32
Input and output Utilities.
#define gsWarn
Definition: gsDebug.h:50
gsXmlNode * putSparseMatrixToXml(gsSparseMatrix< T > const &mat, gsXmlTree &data, std::string name="SparseMatrix")
Helper to insert sparse matrices into XML.
Definition: gsXml.hpp:111
gsXmlNode * makeNode(const std::string &name, gsXmlTree &data)
Helper to allocate XML node.
Definition: gsXml.cpp:54
char * makeValue(const std::string &value, gsXmlTree &data)
Helper to allocate XML value.
Definition: gsXml.cpp:32
gsXmlNode * putMatrixToXml(gsMatrix< T > const &mat, gsXmlTree &data, std::string name="Matrix")
Helper to insert matrices into XML.
Definition: gsXml.hpp:103
This is the main header file that collects wrappers of Eigen for linear algebra.
void getSparseEntriesFromXml(gsXmlNode *node, gsSparseEntries< T > &result)
Helper to fetch sparse entries.
Definition: gsXml.hpp:133
void getMatrixFromXml(gsXmlNode *node, unsigned const &rows, unsigned const &cols, gsMatrix< T > &result, const std::string &base_type_flag="ascii")
Helper to fetch matrices.
Definition: gsXml.hpp:71