G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsXml.hpp
Go to the documentation of this file.
1
14#include <sstream>
16#include <gsIO/gsIOUtils.h>
17
18namespace gismo {
19
20namespace internal {
21
22/*
23template<class Object>
24std::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
33template<class T>
34gsXmlNode * 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
43template<class T>
44char * 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 if (value.cols()>1) 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
70template <class T>
71void 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
102template<class T>
103gsXmlNode * 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
110template<class T>
111gsXmlNode * 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
132template<class T>
133void 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
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
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition gsMatrix.h:41
Class that provides a container for triplets (i,j,value) to be filled in a sparse matrix.
Definition gsSparseMatrix.h:34
Sparse matrix class, based on gsEigen::SparseMatrix.
Definition gsSparseMatrix.h:139
#define index_t
Definition gsConfig.h:32
#define gsWarn
Definition gsDebug.h:50
Input and output Utilities.
This is the main header file that collects wrappers of Eigen for linear algebra.
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
gsXmlNode * putSparseMatrixToXml(gsSparseMatrix< T > const &mat, gsXmlTree &data, std::string name="SparseMatrix")
Helper to insert sparse matrices into XML.
Definition gsXml.hpp:111
gsXmlNode * putMatrixToXml(gsMatrix< T > const &mat, gsXmlTree &data, std::string name="Matrix")
Helper to insert matrices into XML.
Definition gsXml.hpp:103
char * makeValue(const std::string &value, gsXmlTree &data)
Helper to allocate XML value.
Definition gsXml.cpp:32
void getSparseEntriesFromXml(gsXmlNode *node, gsSparseEntries< T > &result)
Helper to fetch sparse entries.
Definition gsXml.hpp:133
gsXmlNode * makeNode(const std::string &name, gsXmlTree &data)
Helper to allocate XML node.
Definition gsXml.cpp:54
The G+Smo namespace, containing all definitions for the library.