Advertisement
bolverk

new concept for computational cell

Mar 6th, 2021
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.12 KB | None | 0 0
  1. /*! \file computational_cell.hpp
  2.   \author Almog Yalinewich
  3.   \brief Computational cell
  4. */
  5.  
  6. #ifndef COMPUTATIONAL_CELL_HPP
  7. #define COMPUTATIONAL_CELL_HPP 1
  8.  
  9. /*#include <boost/container/small_vector.hpp>
  10.   typedef boost::container::small_vector<double,0> tvector;
  11.   typedef boost::container::small_vector<bool,0> svector;*/
  12. #include <vector>
  13. #include <array>
  14. typedef std::vector<double> tvector;
  15. typedef std::vector<bool> svector;
  16. #include <string>
  17. #include "../../tessellation/geometry.hpp"
  18. #include "../../misc/int2str.hpp"
  19. #ifdef RICH_MPI
  20. #include "../../misc/serializable.hpp"
  21. #endif // RICH_MPI
  22.  
  23. using std::array;
  24. using std::string;
  25.  
  26. //! \brief Computational cell
  27. template<int N, class T> class ComputationalCell
  28. #ifdef RICH_MPI
  29.   : public Serializable
  30. #endif // RICH_MPI
  31.  
  32. {
  33. public:
  34.  
  35.   //! \brief Density
  36.   double density;
  37.  
  38.   //! \brief Pressure
  39.   double pressure;
  40.  
  41.   //! \brief Velocity
  42.   Vector2D velocity;
  43.  
  44.   //! \brief Tracers (can transfer from one cell to another)
  45. #ifdef __clang__
  46.   [[clang::no_destroy]]
  47. #endif // __clang__
  48.   static array<string, N> tracer_names;
  49.   array<double, N> tracers;
  50.  
  51.   //! \brief Stickers (stick to the same cell)
  52.   T meta;
  53.  
  54.   /*!
  55.     \brief Copy constructor
  56.     \param other The cell to copy
  57.   */
  58.   ComputationalCell(ComputationalCell<N, T> const& other):
  59.     density(other.density),
  60.     pressure(other.pressure),
  61.     velocity(other.velocity),
  62.     tracers(other.tracers) {}
  63.  
  64.   /*!
  65.     \brief Default constructor
  66.   */
  67.   ComputationalCell(void):
  68.     density(0),
  69.     pressure(0),
  70.     velocity(Vector2D()),
  71.     tracers() {}
  72.  
  73.   /*! \brief Self increment operator
  74.     \param other Addition
  75.     \return Reference to self
  76.   */
  77.   ComputationalCell& operator+=(ComputationalCell const& other)
  78.   {
  79.     this->density += other.density;
  80.     this->pressure += other.pressure;
  81.     this->velocity += other.velocity;
  82.     assert(this->tracers.size() == other.tracers.size());
  83.     //    size_t N = this->tracers.size();
  84.     for (size_t j = 0; j < N; ++j)
  85.       this->tracers[j] += other.tracers[j];
  86.     return *this;
  87.   }
  88.  
  89.   /*! \brief Self reduction operator
  90.     \param other Reduction
  91.     \return Reference to self
  92.   */
  93.   ComputationalCell& operator-=(ComputationalCell const& other)
  94.   {
  95.     this->density -= other.density;
  96.     this->pressure -= other.pressure;
  97.     this->velocity -= other.velocity;
  98.     transform(this->tracers.begin(),
  99.           this->tracers.end(),
  100.           other.tracers.begin(),
  101.           this->tracers.begin(),
  102.           [](double x, double y)
  103.           {return x-y;});
  104.     return *this;
  105.   }
  106.  
  107.   /*! \brief Self multiplication operator
  108.     \param s The scalar to multiply
  109.     \return Reference to self
  110.   */
  111.   ComputationalCell& operator*=(double s)
  112.   {
  113.     this->density *= s;
  114.     this->pressure *= s;
  115.     this->velocity *= s;
  116.     // size_t N = this->tracers.size();
  117.     for (size_t j = 0; j < N; ++j)
  118.       this->tracers[j] *= s;
  119.     return *this;
  120.   }
  121.  
  122.   /*! \brief Self decrement operator
  123.     \param other difference
  124.     \return Reference to self
  125.   */
  126.   ComputationalCell<N, T>& operator=(ComputationalCell<N, T> const& other)
  127.   {
  128.     density = other.density;
  129.     pressure = other.pressure;
  130.     velocity = other.velocity;
  131.     tracers = other.tracers;
  132.     return *this;
  133.   }
  134.  
  135. #ifdef RICH_MPI
  136.   size_t getChunkSize(void) const;
  137.  
  138.   vector<double> serialize(void) const;
  139.  
  140.   void unserialize
  141.   (const vector<double>& data);
  142. #endif // RICH_MPI
  143.  
  144.   ~ComputationalCell(void) {}
  145. };
  146.  
  147. /*
  148.   template<int N> array<string, N> uninitialised_tracer_names(void)
  149.   {
  150.   array<string, N> res;
  151.   generate(res.begin(),
  152.   res.end(),
  153.   [n=0]() mutable{
  154.   ++n;
  155.   return "untitled"+int2str(n);
  156.   });
  157.   return res;
  158.   }
  159. */
  160.  
  161. //template<int N, class T> array<string, N> ComputationalCell<N, T>::tracer_names = uninitialised_tracer_names<N>();
  162.  
  163. template<int N, class T> array<string, N> ComputationalCell<N, T>::tracer_names = array<string, N>();
  164.  
  165. /*! \brief Term by term addition
  166.   \param p1 Computational Cell
  167.   \param p2 Computational Cell
  168.   \return Computational Cell
  169. */
  170. /*
  171.   ComputationalCell operator+
  172.   (ComputationalCell const& p1,
  173.   ComputationalCell const& p2);
  174. */
  175.  
  176. /*! \brief Term by term subtraction
  177.   \param p1 Computational Cell
  178.   \param p2 Computational Cell
  179.   \return Computational Cell
  180. */
  181. template<int N, class T> ComputationalCell<N, T> operator-
  182. (ComputationalCell<N, T> const& p1,
  183.  ComputationalCell<N, T> const& p2)
  184. {
  185.   ComputationalCell<N, T> res(p1);
  186.   res -= p2;
  187.   return res;
  188. }
  189.  
  190. /*! \brief Scalar division
  191.   \param p Computational Cell
  192.   \param s Scalar
  193.   \return Computational Cell
  194. */
  195. template<int N, class T> ComputationalCell<N, T> operator/
  196. (const ComputationalCell<N, T>& p,
  197.  double s)
  198. {
  199.   ComputationalCell<N, T> res(p);
  200.   res.density /= s;
  201.   res.pressure /= s;
  202.   res.velocity.x /= s;
  203.   res.velocity.y /= s;
  204.   transform(res.tracers.begin(),
  205.         res.tracers.end(),
  206.         res.tracers.begin(),
  207.         [&s](double x)
  208.         {return x/s;});
  209.   return res;
  210. }
  211.  
  212. /*! \brief Scalar multiplication on the right
  213.   \param p Computational Cell
  214.   \param s Scalar
  215.   \return Computational Cell
  216. */
  217. template<int N, class T> ComputationalCell<N, T> operator*
  218. (const ComputationalCell<N, T>& p, double s)
  219. {
  220.   ComputationalCell<N, T> res(p);
  221.   res.density *= s;
  222.   res.pressure *= s;
  223.   res.velocity *= s;
  224.   transform(res.tracers.begin(),
  225.         res.tracers.end(),
  226.         res.tracers.begin(),
  227.         [&s](double x)
  228.         {return x*s;});
  229.   return res;
  230. }
  231.  
  232. /*! \brief Scalar multiplication on the left
  233.   \param s Scalar
  234.   \param p Computational Cell
  235.   \return Computational Cell
  236. */
  237. //ComputationalCell operator*(double s, ComputationalCell const& p);
  238.  
  239. template<int N, class T> void ComputationalCellAddMult
  240. (ComputationalCell<N, T> &res,
  241.  const ComputationalCell<N, T>& other,
  242.  double scalar)
  243. {
  244.   res.density += other.density*scalar;
  245.   res.pressure += other.pressure*scalar;
  246.   res.velocity += other.velocity*scalar;
  247.   assert(res.tracers.size() == other.tracers.size());
  248.   size_t N0 = res.tracers.size();
  249.   for (size_t j = 0; j < N0; ++j)
  250.     res.tracers[j] += other.tracers[j]*scalar;
  251. }
  252.  
  253. //void ReplaceComputationalCell(ComputationalCell &res, ComputationalCell const& other);
  254.  
  255. //void ReplaceComputationalCellDiff(ComputationalCell &res, ComputationalCell const& first, ComputationalCell const& second);
  256.  
  257. //! \brief Class for spatial interpolations
  258.  
  259. template<int N, class T> class Slope
  260. #ifdef RICH_MPI
  261.   : public Serializable
  262. #endif // RICH_MPI
  263. {
  264. public:
  265.   //! \brief Slope in the x direction
  266.   ComputationalCell<N, T> xderivative;
  267.  
  268.   //! \brief Slope in the y direction
  269.   ComputationalCell<N, T> yderivative;
  270.  
  271.   /*!
  272.     \brief Class constructor
  273.     \param x The x derivative
  274.     \param y The y derivative
  275.   */
  276.   Slope
  277.   (const ComputationalCell<N, T>& x,
  278.    const ComputationalCell<N, T>& y):
  279.     xderivative(x), yderivative(y) {}
  280.   //! \brief Default constructor
  281.   Slope(void):
  282.     xderivative(), yderivative() {}
  283. #ifdef RICH_MPI
  284.   size_t getChunkSize(void) const;
  285.  
  286.   vector<double> serialize(void) const;
  287.  
  288.   void unserialize(const vector<double>& data);
  289. #endif//RICH_MPI
  290. };
  291.  
  292. #endif // COMPUTATIONAL_CELL_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement