cheb_node.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * \file cheb_node.hpp
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 1-22-2011
  5. * \brief This is a derived cheb class of MPI_Node.
  6. */
  7. #include <vector>
  8. #include <cstdlib>
  9. #include <stdint.h>
  10. #include <pvfmm_common.hpp>
  11. #include <tree_node.hpp>
  12. #include <mpi_node.hpp>
  13. #include <vector.hpp>
  14. #ifndef _PVFMM_CHEB_NODE_HPP_
  15. #define _PVFMM_CHEB_NODE_HPP_
  16. namespace pvfmm{
  17. template<class Real_t>
  18. class FunctionInterface{
  19. public:
  20. virtual void operator()(const Real_t* coord, int n, Real_t* out)=0;
  21. };
  22. /**
  23. * \brief Tree node class for volume data.
  24. */
  25. template <class Real_t>
  26. class Cheb_Node: public MPI_Node<Real_t>{
  27. public:
  28. class Function_t{
  29. typedef void (*FnPtr_t)(const Real_t* coord, int n, Real_t* out);
  30. public:
  31. Function_t(): fn_ptr_(NULL), fn_(NULL){}
  32. Function_t(FnPtr_t fn_ptr): fn_ptr_(fn_ptr), fn_(NULL){}
  33. Function_t(FunctionInterface<Real_t>* fn): fn_ptr_(NULL), fn_(fn){}
  34. void operator()(const Real_t* coord, int n, Real_t* out){
  35. if(fn_ptr_) fn_ptr_(coord, n, out);
  36. else if(fn_) (*fn_)(coord, n, out);
  37. else assert(false);
  38. }
  39. bool IsEmpty(){
  40. return (fn_ptr_==NULL && fn_==NULL);
  41. }
  42. private:
  43. FnPtr_t fn_ptr_;
  44. FunctionInterface<Real_t>* fn_;
  45. };
  46. /**
  47. * \brief Base class for node data. Contains initialization data for the node.
  48. */
  49. class NodeData: public MPI_Node<Real_t>::NodeData{
  50. public:
  51. Vector<Real_t> cheb_coord; //Chebyshev point samples.
  52. Vector<Real_t> cheb_value;
  53. Function_t input_fn; // Function pointer.
  54. int data_dof; // Dimension of Chebyshev data.
  55. int cheb_deg; // Chebyshev degree
  56. Real_t tol; // Tolerance for adaptive refinement.
  57. };
  58. /**
  59. * \brief Initialize pointers to NULL.
  60. */
  61. Cheb_Node(): MPI_Node<Real_t>(), cheb_deg(0){}
  62. /**
  63. * \brief Virtual destructor.
  64. */
  65. virtual ~Cheb_Node();
  66. /**
  67. * \brief Initialize the node by passing the relevant data.
  68. */
  69. virtual void Initialize(TreeNode* parent_, int path2node_, TreeNode::NodeData*);
  70. /**
  71. * \brief Returns list of coordinate and value vectors which need to be
  72. * sorted and partitioned across MPI processes and the scatter index is
  73. * saved.
  74. */
  75. virtual void NodeDataVec(std::vector<Vector<Real_t>*>& coord,
  76. std::vector<Vector<Real_t>*>& value,
  77. std::vector<Vector<size_t>*>& scatter){
  78. MPI_Node<Real_t>::NodeDataVec(coord, value, scatter);
  79. coord .push_back(&cheb_coord );
  80. value .push_back(&cheb_value );
  81. scatter.push_back(&cheb_scatter);
  82. coord .push_back( NULL);
  83. value .push_back(&cheb_coeff);
  84. scatter.push_back( NULL);
  85. }
  86. /**
  87. * \brief Clear node data.
  88. */
  89. virtual void ClearData();
  90. /**
  91. * \brief Returns the cost of this node. Used for load balancing.
  92. */
  93. virtual long long& NodeCost(){return MPI_Node<Real_t>::NodeCost();}
  94. /**
  95. * \brief Degree of Chebyshev polynomials used.
  96. */
  97. int ChebDeg(){return cheb_deg;}
  98. /**
  99. * \brief Error tolerance for adaptive refinement of the Chebyshev Tree.
  100. */
  101. Real_t& MaxErr(){return tol;}
  102. /**
  103. * \brief Chebyshev coefficients for the source distribution.
  104. */
  105. Vector<Real_t>& ChebData(){return cheb_coeff;}
  106. /**
  107. * \brief Allocate a new object of the same type (as the derived class) and
  108. * return a pointer to it type cast as (TreeNode*).
  109. */
  110. virtual TreeNode* NewNode(TreeNode* n_=NULL);
  111. /**
  112. * \brief Evaluates and returns the subdivision condition for this node.
  113. * 'true' if node requires further subdivision.
  114. */
  115. virtual bool SubdivCond();
  116. /**
  117. * \brief Create child nodes and Initialize them.
  118. */
  119. virtual void Subdivide();
  120. /**
  121. * \brief Truncates the tree i.e. makes this a leaf node.
  122. */
  123. virtual void Truncate();
  124. /**
  125. * \brief Return degrees of freedom of data.
  126. */
  127. int& DataDOF(){return data_dof;}
  128. /**
  129. * \brief Pack this node to be transmitted to another process. The node
  130. * is responsible for allocating and freeing the memory for the actual data.
  131. */
  132. virtual PackedData Pack(bool ghost=false, void* buff_ptr=NULL, size_t offset=0);
  133. /**
  134. * \brief Initialize the node with data from another process.
  135. */
  136. virtual void Unpack(PackedData data, bool own_data=true);
  137. /**
  138. * \brief Read source distribution at points on a grid defined by array of x,
  139. * y and z coordinates.
  140. */
  141. virtual void ReadVal(std::vector<Real_t> x,std::vector<Real_t> y, std::vector<Real_t> z, Real_t* val, bool show_ghost=true){
  142. read_val(x,y,z,x.size(),y.size(),z.size(),val,show_ghost);
  143. }
  144. /**
  145. * \brief Append node VTU data to vectors.
  146. */
  147. template <class VTUData_t, class Node_t>
  148. static void VTU_Data(VTUData_t& vtu_data, std::vector<Node_t*>& nodes, int lod);
  149. /**
  150. * \brief Compute gradient of the data.
  151. */
  152. void Gradient();
  153. /**
  154. * \brief Compute divergence of the data.
  155. */
  156. void Divergence();
  157. /**
  158. * \brief Compute curl of the data.
  159. */
  160. void Curl();
  161. Function_t input_fn;
  162. Vector<Real_t> cheb_coord; //coordinates of points
  163. Vector<Real_t> cheb_value; //value at points
  164. Vector<size_t> cheb_scatter; //scatter index mapping original data.
  165. private:
  166. /**
  167. * \brief Read source distribution at points on a grid defined by array of x,
  168. * y and z coordinates.
  169. */
  170. void read_val(std::vector<Real_t> x,std::vector<Real_t> y, std::vector<Real_t> z, int nx, int ny, int nz, Real_t* val, bool show_ghost=true);
  171. Real_t tol;
  172. int cheb_deg;
  173. int data_dof;
  174. Vector<Real_t> cheb_coeff;
  175. };
  176. }//end namespace
  177. #include <cheb_node.txx>
  178. #endif //_PVFMM_CHEB_NODE_HPP_