cheb_node.hpp 4.9 KB

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