Skip to content
Snippets Groups Projects
Commit 9c409998 authored by Pietro Incardona's avatar Pietro Incardona
Browse files

Adding VTK writer for grids

parent 219f53f0
No related branches found
No related tags found
No related merge requests found
......@@ -35,64 +35,6 @@ public:
/*! \brief This class specialize functions in the case the type T
* has not defined attributes
*
* In C++ partial specialization of a function is not allowed so we have to
* encapsulate this function in a class
*
* \tparam has_attributes parameter that specialize the function in case the vertex
* define or not attributes name
*
* \tparam i id of the property we are going to write
*
*/
template<typename ele_g, typename St, unsigned int i>
class prop_output_g<false,St,ele_g,i>
{
public:
/*! \brief Given a Graph return the point data header for a typename T
*
* \tparam T type to write
* \param n_node number of the node
*
*/
static std::string get_point_property_header()
{
//! vertex node output string
std::string v_out;
// Check if T is a supported format
// for now we support only scalar of native type
std::string type = getType<typename boost::fusion::result_of::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<i>>::type>();
// if the type is not supported return
if (type.size() == 0)
{return v_out;}
// Create point data properties
v_out += "SCALARS " + get_attributes() + " " + type + "\n";
// Default lookup table
v_out += "LOOKUP_TABLE default\n";
// return the vertex list
return v_out;
}
/*! \brief Get the attributes name
*
*/
static std::string get_attributes()
{
return std::string("attr" + std::to_string(i));
}
};
/*! \brief this class is a functor for "for_each" algorithm
*
* This class is a functor for "for_each" algorithm. For each
......@@ -180,7 +122,7 @@ class VTKWriter<pair,VECTOR_GRIDS>
std::string v_out;
// write the number of vertex
v_out += "VERTICES " + std::to_string(get_total()) + " " + std::to_string(vg.size() * 2) + "\n";
v_out += "VERTICES " + std::to_string(get_total()) + " " + std::to_string(get_total() * 2) + "\n";
// return the vertex properties string
return v_out;
......@@ -235,7 +177,10 @@ class VTKWriter<pair,VECTOR_GRIDS>
p = it.get().toPoint();
p = p * vg.get(i).spacing + vg.get(i).offset;
v_out << p.toString() << "\n";
if (pair::first::dims == 2)
v_out << p.toString() << " 0.0" << "\n";
else
v_out << p.toString() << "\n";
// increment the iterator and counter
++it;
......
/*
* VTKWriter_grids_util.hpp
*
* Created on: Aug 10, 2015
* Author: i-bird
*/
#ifndef SRC_VTKWRITER_GRIDS_UTIL_HPP_
#define SRC_VTKWRITER_GRIDS_UTIL_HPP_
/*! \brief This class specialize functions in the case the type T
* has or not defined attributes
*
* In C++ partial specialization of a function is not allowed so we have to
* encapsulate this function in a class
*
* \tparam has_attributes parameter that specialize the function in case the grid
* define or not attributes name
*
* \tparam Grid type we are processing
* \tparam i the property we are going to write
*
*/
template<bool has_attributes, typename St, typename ele_g, unsigned int i>
class prop_output_g
{
public:
/*! \brief For each vertex set the value
*
* \tparam i vertex property to print
*
*/
static std::string get_point_data(const openfpm::vector< ele_g > & vg)
{
//! vertex node output string
std::string v_out;
for (size_t k = 0 ; k < vg.size() ; k++)
{
//! Get a vertex iterator
auto it = vg.get(k).g.getIterator();
// if there is the next element
while (it.isNext())
{
// Print the property
v_out += std::to_string(vg.get(k).get_o(it.get()).template get<i>()) + "\n";
// increment the iterator and counter
++it;
}
}
return v_out;
}
/*! \brief Given a Graph return the point data header for a typename T
*
* \tparam T type to write
* \param n_node number of the node
*
*/
static std::string get_point_property_header(const std::string & oprp)
{
//! vertex node output string
std::string v_out;
// Check if T is a supported format
// for now we support only scalar of native type
std::string type = getType<typename boost::fusion::result_of::at<typename ele_g::value_type::type,boost::mpl::int_<i>>::type>();
// if the type is not supported return
if (type.size() == 0)
{return v_out;}
// Create point data properties
v_out += "SCALARS " + get_attributes(oprp) + " " + type + "\n";
// Default lookup table
v_out += "LOOKUP_TABLE default\n";
// return the vertex list
return v_out;
}
/*! \brief Get the attributes name
*
*/
static std::string get_attributes(const std::string & out)
{
return ele_g::value_type::attributes::name[i] + out;
}
};
/*! \brief This class specialize functions in the case the type T
* has not defined attributes
*
* In C++ partial specialization of a function is not allowed so we have to
* encapsulate this function in a class
*
* \tparam has_attributes parameter that specialize the function in case the vertex
* define or not attributes name
*
* \tparam i id of the property we are going to write
*
*/
template<typename ele_g, typename St, unsigned int i>
class prop_output_g<false,St,ele_g,i>
{
public:
/*! \brief Get the vtk properties header appending a prefix at the end
*
* \param oprp prefix
*
*/
static std::string get_point_property_header(const std::string & oprp)
{
//! vertex node output string
std::string v_out;
// Check if T is a supported format
// for now we support only scalar of native type
typedef typename boost::mpl::at<typename ele_g::value_type::value_type::type,boost::mpl::int_<i>>::type ctype;
typedef typename std::remove_all_extents<ctype>::type vttype;
std::string type = getType<vttype>();
// if the type is not supported return
if (type.size() == 0)
{return v_out;}
// Create point data properties
v_out += "SCALARS " + get_attributes(oprp) + " " + type + "\n";
// Default lookup table
v_out += "LOOKUP_TABLE default\n";
// return the vertex list
return v_out;
}
/*! \brief Get the attributes name
*
*/
static std::string get_attributes(const std::string & oprp)
{
return std::string("attr" + std::to_string(i) + oprp);
}
};
/*! \brief This class is an helper to create properties output from scalar and compile-time array elements
*
* This class is an helper to copy scalar and compile-time array elements
*
*/
template<typename I, typename ele_g, typename St, typename T>
struct meta_prop
{
inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out)
{
// actual string size
size_t sz = v_out.size();
// Produce the point properties header
v_out += prop_output_g<has_attributes<typename ele_g::value_type::value_type>::value,St ,ele_g,I::value>::get_point_property_header("");
// If the output has changed, we have to write the properties
if (v_out.size() != sz)
{
// Produce point data
for (size_t k = 0 ; k < vg.size() ; k++)
{
//! Get a vertex iterator
auto it = vg.get(k).g.getIterator();
// if there is the next element
while (it.isNext())
{
// Print the property
v_out += std::to_string(vg.get(k).g.get_o(it.get()).template get<I::value>()) + "\n";
// increment the iterator and counter
++it;
}
}
}
}
};
//! Partial specialization for N=1 1D-Array
template<typename I, typename ele_g, typename St, typename T, size_t N1>
struct meta_prop<I, ele_g,St,T[N1]>
{
inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out)
{
for (size_t i1 = 0 ; i1 < N1 ; i1++)
{
// actual string size
size_t sz = v_out.size();
// Produce the point properties header
v_out += prop_output_g<has_attributes<typename ele_g::value_type::value_type>::value,St ,ele_g,I::value>::get_point_property_header("_" + std::to_string(i1));
// If the output has changed, we have to write the properties
if (v_out.size() != sz)
{
// Produce point data
for (size_t k = 0 ; k < vg.size() ; k++)
{
//! Get a vertex iterator
auto it = vg.get(k).g.getIterator();
// if there is the next element
while (it.isNext())
{
// Print the property
v_out += std::to_string(vg.get(k).g.get_o(it.get()).template get<I::value>()[i1]) + "\n";
// increment the iterator and counter
++it;
}
}
}
}
}
};
//! Partial specialization for N=2 2D-Array
template<typename I, typename ele_g, typename St ,typename T,size_t N1,size_t N2>
struct meta_prop<I, ele_g,St, T[N1][N2]>
{
inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out)
{
for (size_t i1 = 0 ; i1 < N1 ; i1++)
{
for (size_t i2 = 0 ; i2 < N2 ; i2++)
{
// actual string size
size_t sz = v_out.size();
// Produce the point properties header
v_out += prop_output_g<has_attributes<typename ele_g::value_type::value_type>::value,St ,ele_g,I::value>::get_point_property_header("_" + std::to_string(i1) + "_" + std::to_string(i2));
// If the output has changed, we have to write the properties
if (v_out.size() != sz)
{
// Produce point data
for (size_t k = 0 ; k < vg.size() ; k++)
{
//! Get a vertex iterator
auto it = vg.get(k).g.getIterator();
// if there is the next element
while (it.isNext())
{
// Print the property
v_out += std::to_string(vg.get(k).g.get_o(it.get()).template get<I::value>()[i1][i2]) + "\n";
// increment the iterator and counter
++it;
}
}
}
}
}
}
};
//! Partial specialization for N=3
template<typename I, typename ele_g, typename St,typename T,size_t N1,size_t N2,size_t N3>
struct meta_prop<I,ele_g,St,T[N1][N2][N3]>
{
inline meta_prop(const openfpm::vector< ele_g > & vg, std::string & v_out)
{
for (size_t i1 = 0 ; i1 < N1 ; i1++)
{
for (size_t i2 = 0 ; i2 < N2 ; i2++)
{
for (size_t i3 = 0 ; i3 < N3 ; i3++)
{
// actual string size
size_t sz = v_out.size();
// Produce the point properties header
v_out += prop_output_g<has_attributes<typename ele_g::value_type::value_type>::value,St ,ele_g,I::value>::get_point_property_header("_" + std::to_string(i1) + "_" + std::to_string(i2) + "_" + std::to_string(i3));
// If the output has changed, we have to write the properties
if (v_out.size() != sz)
{
std::string attr = prop_output_g<has_attributes<typename ele_g::value_type::value_type>::value, St,ele_g,I::value>::get_attributes() + "_";
// Produce point data
for (size_t k = 0 ; k < vg.size() ; k++)
{
//! Get a vertex iterator
auto it = vg.get(k).g.getIterator();
// if there is the next element
while (it.isNext())
{
// Print the property
v_out += std::to_string(vg.get(k).g.get_o(it.get()).template get<I::value>()[i1][i2][i3]) + "\n";
// increment the iterator and counter
++it;
}
}
}
}
}
}
}
};
#endif /* SRC_VTKWRITER_GRIDS_UTIL_HPP_ */
......@@ -280,12 +280,16 @@ BOOST_AUTO_TEST_CASE( vtk_writer_use_grids)
size_t sz[] = {16,16};
grid_cpu<2,Point_test<float>> g1(sz);
g1.template setMemory<HeapMemory>();
fill_grid_some_data(g1);
grid_cpu<2,Point_test<float>> g2(sz);
g2.template setMemory<HeapMemory>();
fill_grid_some_data(g2);
grid_cpu<2,Point_test<float>> g3(sz);
g3.template setMemory<HeapMemory>();
fill_grid_some_data(g3);
grid_cpu<2,Point_test<float>> g4(sz);
g4.template setMemory<HeapMemory>();
fill_grid_some_data(g4);
// Create a writer and write
......
Source diff could not be displayed: it is too large. Options to address this: view the blob.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment