cheb_node.hpp 5.3 KB

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