8#ifndef OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_PETSC_HPP_
9#define OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_PETSC_HPP_
11#include "Vector/map_vector.hpp"
12#include "Vector/vector_def.hpp"
13#include <boost/mpl/int.hpp>
15#include "util/petsc_util.hpp"
16#include <unordered_map>
17#include "VTKWriter/VTKWriter.hpp"
18#include "CSVWriter/CSVWriter.hpp"
34 typedef boost::fusion::vector<PetscInt,T>
type;
40 static const unsigned int row = 0;
43 static const unsigned int value = 1;
46 static const unsigned int max_prop = 2;
55 return boost::fusion::at_c<row>(data);
65 return boost::fusion::at_c<value>(data);
96constexpr unsigned int row_id = 0;
97constexpr unsigned int val_id = 1;
115 mutable bool v_created =
false;
124 mutable std::unordered_map<size_t,size_t>
map;
135 if (v_created ==
false)
136 {PETSC_SAFE_CALL(VecSetType(v,VECMPI));}
140 if (row_val.
size() != 0)
141 {PETSC_SAFE_CALL(VecSetValues(v,row_val.
size(),&row_val.template get<row_id>(0),&row_val.template get<val_id>(0),INSERT_VALUES))}
143 PETSC_SAFE_CALL(VecAssemblyBegin(v));
144 PETSC_SAFE_CALL(VecAssemblyEnd(v));
179 if (is_openfpm_init() ==
true)
180 {PETSC_SAFE_CALL(VecDestroy(&v));}
190 :n_row_local(n_row_local),v(NULL),invalid(0)
193 PETSC_SAFE_CALL(VecCreate(PETSC_COMM_WORLD,&v));
202 :n_row(0),n_row_local(0),invalid(0)
205 PETSC_SAFE_CALL(VecCreate(PETSC_COMM_WORLD,&v));
219 PETSC_SAFE_CALL(VecSetSizes(v,n_row_local,n_row));
233 map[i] = row_val.
size()-1;
235 row_val.last().template get<row_id>() = i;
236 row_val.last().template get<val_id>() = val;
251 map[i] = row_val.
size()-1;
253 row_val.last().template get<row_id>() = i;
254 return row_val.last().template get<val_id>();
264 inline const PetscScalar &
insert(
size_t i)
const
269 map[i] = row_val.
size()-1;
271 row_val.last().template get<row_id>() = i;
272 return row_val.last().template get<val_id>();
288 std::unordered_map<size_t,size_t>::iterator it = map.find(i);
290 if ( it != map.end() )
291 return row_val.template get<val_id>(it->second);
309 std::unordered_map<size_t,size_t>::iterator it = map.find(i);
311 if ( it != map.end() )
312 return row_val.template get<val_id>(it->second);
347 PetscInt n_row_local;
350 VecGetSize(v,&n_row);
351 VecGetLocalSize(v,&n_row_local);
354 this->n_row_local = n_row_local;
356 row_val.resize(n_row_local);
363 VecGetOwnershipRange(v,&low,&high);
368 for (
size_t i = low ; i < (size_t)high ; i++)
370 row_val.template get<row_id>(k) = i;
375 PETSC_SAFE_CALL(VecGetValues(v,row_val.
size(),&row_val.template get<row_id>(0),&row_val.template get<val_id>(0)))
399 row_val.swap(v.row_val);
410 if (v_created ==
false)
411 {PETSC_SAFE_CALL(VecSetType(v,VECMPI));}
421 bool write(std::string out,
size_t opt = VTK_WRITER)
428 row_col.resize(n_row_local);
429 values.resize(n_row_local);
432 for (
auto it = map.begin() ; it != map.end() ; it++, i++)
434 row_col.template get<0>(i)[1] = it->first;
435 row_col.template get<0>(i)[0] = 0.0;
437 values.template get<0>(i) = row_val.template get<1>(it->second);
440 if (opt == VTK_WRITER)
442 auto ft = file_type::ASCII;
447 VECTOR_POINTS> vtk_writer;
449 vtk_writer.add(row_col,values,row_col.
size());
451 std::string output = std::to_string(out +
"_" + std::to_string(v_cl.
getProcessUnitID()) + std::to_string(
".vtk"));
454 prp_names.add(
"value");
457 return vtk_writer.write(output,prp_names,
"vector",
"",ft);
465 std::string output = std::to_string(out +
"_" + std::to_string(v_cl.
getProcessUnitID()) + std::to_string(
".csv"));
468 return csv_writer.
write(output,row_col,values);
bool write(std::string file, v_pos &v, v_prp &prp, size_t offset=0)
It write a CSV file.
This class allocate, and destroy CPU memory.
size_t getProcessUnitID()
Get the process unit id.
Implementation of VCluster class.
const PetscScalar & operator()(size_t i) const
Return a reference to the vector element.
std::unordered_map< size_t, size_t > map
Global to local map.
void resize(size_t row, size_t l_row)
Resize the Vector.
Vector< T, PETSC_BASE > & operator=(Vector< T, PETSC_BASE > &&v)
Copy the vector.
const PetscScalar & insert(size_t i) const
Return a reference to the vector element.
openfpm::vector< rval< PetscScalar, PETSC_RVAL >, HeapMemory, memory_traits_inte > row_val
Mutable row value vector.
size_t n_row
Number of row the petsc vector has.
void insert(size_t i, T val)
Return a reference to the vector element.
const Vec & getVec() const
Get the PETSC Vector object.
Vec & getVec()
Get the PETSC Vector object.
void setZero()
Set to zero all the entries.
~Vector()
Destroy the vector.
Vector< T, PETSC_BASE > & operator=(const Vector< T, PETSC_BASE > &v)
Copy the vector.
void setPetsc() const
Set the Eigen internal vector.
size_t n_row_local
Number of local rows.
PetscScalar & insert(size_t i)
Return a reference to the vector element.
Vector(const Vector< T, PETSC_BASE > &v)
Copy the vector.
Vector(Vector< T, PETSC_BASE > &&v)
Copy the vector.
Vector(size_t n, size_t n_row_local)
Create a vector with n elements.
Vector()
Create a vector with 0 elements.
PetscScalar & operator()(size_t i)
Return a reference to the vector element.
void update()
Update the Vector with the PETSC object.
Sparse Matrix implementation stub object when OpenFPM is compiled with no linear algebra support.
void resize(size_t row, size_t row_n)
stub resize
void insert(size_t i, T val)
stub insert
Vector< T > & operator=(const Vector< T > &v)
stub operator=
Implementation of 1-D std::vector like structure.
rval()
Default constructor.
long int & rw()
Get the row.
rval(long int i, T val)
Constructor from row, column and value.
static bool noPointers()
Indicate that the structure has no pointer.
boost::fusion::vector< PetscInt, T > type
boost fusion that store the point
type data
structure that store the data of the point
It store one row value of a vector.
Transform the boost::fusion::vector into memory specification (memory_traits)