WaveBlocksND
hawp_commons.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdexcept>
4 #include <memory>
5 
6 #include "hawp_paramset.hpp"
7 #include "hawp_evaluator.hpp"
9 
10 
11 namespace waveblocks {
12  namespace wavepackets {
24  template<dim_t D, class MultiIndex>
26  {
27  public:
32  virtual real_t eps() const = 0;
33 
38  virtual HaWpParamSet<D> const& parameters() const = 0;
39 
45 
46  template<int N> HaWpEvaluator<D,MultiIndex,N>
47  create_evaluator(CMatrix<D,N> const& grid) const
48  {
49  return {eps(), &parameters(), shape().get(), grid};
50  }
51 
60  template<int N> HaWpBasisVector<N>
61  evaluate_basis(CMatrix<D,N> const& grid) const
62  {
63  return create_evaluator(grid).all();
64  }
65 
78  template<int N> HaWpBasisVector<N>
79  evaluate_basis(RMatrix<D,N> const& rgrid) const
80  {
81  CMatrix<D,N> cgrid = rgrid.template cast <complex_t>();
82  return evaluate_basis(cgrid);
83  }
84 
85  // virtual HaWpBasisVector<Eigen::Dynamic> evaluate_basis(ComplexGrid<D,Eigen::Dynamic> const& grid) const
86  // {
87  // return create_evaluator<Eigen::Dynamic>(grid).all();
88  // }
89 
105  {
106  return shape_extension_cache_.get_extended_shape( this->shape() );
107  }
108 
109  private:
111  };
112 
113 
128  template<dim_t D, class MultiIndex>
129  class AbstractScalarHaWp : public AbstractScalarHaWpBasis<D,MultiIndex>
130  {
131  public:
132  virtual real_t eps() const = 0;
133  virtual HaWpParamSet<D> const& parameters() const = 0;
135 
140  virtual Coefficients const& coefficients() const = 0;
141 
154  template<int N> CArray<1,N>
155  evaluate(CMatrix<D,N> const& grid) const
156  {
157  if (this->shape()->n_entries() != (std::size_t)coefficients().size())
158  throw std::runtime_error("shape.size() != coefficients.size()");
159 
160  return this->template create_evaluator<N>(grid).reduce(coefficients());
161  }
162 
175  template<int N> CArray<1,N>
176  evaluate(RMatrix<D,N> const& rgrid) const
177  {
178  CMatrix<D,N> cgrid = rgrid.template cast <complex_t>();
179  return evaluate(cgrid);
180  }
181 
182  // virtual HaWpBasisVector<Eigen::Dynamic> evaluate(ComplexGrid<D,Eigen::Dynamic> const& grid) const
183  // {
184  // return create_evaluator<Eigen::Dynamic>().reduce(coefficients());
185  // }
186 
191  {
192  return real_t(1) / this->parameters().sdQ();
193  }
194 
199  {
200  return std::exp(complex_t(0,1) * this->parameters().S() / eps() / eps());
201  }
202  };
203 
204 
208  template<dim_t D, class MultiIndex>
210  {
211  public:
217  {
218  return eps_;
219  }
220 
221  real_t eps() const override
222  {
223  return eps_;
224  }
225 
231  {
232  return parameters_;
233  }
234 
235  HaWpParamSet<D> const& parameters() const override
236  {
237  return parameters_;
238  }
239 
249  {
250  return shape_;
251  }
252 
254  {
255  return shape_;
256  }
257 
263  {
264  return coefficients_;
265  }
266 
267  Coefficients const& coefficients() const override
268  {
269  return coefficients_;
270  }
271 
272  private:
277  };
278 
279 
291  template<dim_t D, class MultiIndex>
293  {
294  public:
303  {
304  public:
305  Component(HomogeneousHaWp const* const owner)
306  : owner_(owner)
307  , coefficients_()
308  { }
309 
311  : owner_(that.owner_)
312  , shape_(that.shape_)
313  , coefficients_(std::move(that.coefficients_))
314  { }
315 
316  Component(Component const& that)
317  : owner_(that.owner_)
318  , shape_(that.shape_)
319  , coefficients_(that.coefficients_)
320  { }
321 
323  {
324  shape_ = that.shape_;
325  coefficients_ = std::move(that.coefficients_);
326  return *this;
327  }
328 
330  {
331  shape_ = that.shape_;
332  coefficients_ = that.coefficients_;
333  return *this;
334  }
335 
336  real_t eps() const override
337  {
338  return owner_->eps();
339  }
340 
341  HaWpParamSet<D> const& parameters() const override
342  {
343  return owner_->parameters();
344  }
345 
355  {
356  return shape_;
357  }
358 
360  {
361  return shape_;
362  }
363 
369  {
370  return coefficients_;
371  }
372 
373  Coefficients const& coefficients() const override
374  {
375  return coefficients_;
376  }
377 
378  private:
379  HomogeneousHaWp const* const owner_;
380 
383  };
384 
385  HomogeneousHaWp(std::size_t n)
386  : eps_(0.0)
387  , parameters_()
388  , components_(n, Component(this))
389  { }
390 
396  {
397  return eps_;
398  }
399 
404  real_t eps() const
405  {
406  return eps_;
407  }
408 
414  {
415  return parameters_;
416  }
417 
423  {
424  return parameters_;
425  }
426 
431  std::vector<Component> & components()
432  {
433  return components_;
434  }
435 
440  std::vector<Component> const& components() const
441  {
442  return components_;
443  }
444 
452  Component & component(std::size_t n)
453  {
454  return components_[n];
455  }
456 
464  Component const& component(std::size_t n) const
465  {
466  return components_[n];
467  }
468 
476  Component & operator[](std::size_t n)
477  {
478  return component(n);
479  }
480 
488  Component const& operator[](std::size_t n) const
489  {
490  return component(n);
491  }
492 
496  std::size_t n_components() const
497  {
498  return components_.size();
499  }
500 
509  {
510  // check cache status
511  bool rebuild_cache = false;
512 
513  if (union_cache_snapshot_.size() != n_components()) {
514  rebuild_cache = true;
515  union_cache_snapshot_.resize(n_components());
516  }
517 
518  for (std::size_t n = 0; n < n_components() && !rebuild_cache; n++) {
519  if (union_cache_snapshot_[n] != component(n).shape().get())
520  rebuild_cache = true;
521  }
522 
523  // rebuild cache if necessary
524  if (rebuild_cache) {
525  std::vector< ShapeEnum<D,MultiIndex> const* > list(n_components());
526  for (std::size_t c = 0; c < n_components(); c++) {
527  list[c] = union_cache_snapshot_[c] = component(c).shape().get();
528  }
529  cached_shape_union_ = std::make_shared< ShapeEnum<D,MultiIndex> >(shapes::shape_enum::strict_union(list));
530  }
531 
532  return cached_shape_union_;
533  }
534 
554  template<int N>
556  {
557  ScalarHaWp<D,MultiIndex> unionwp;
558 
559  unionwp.eps() = eps();
560  unionwp.parameters() = parameters();
561  unionwp.shape() = union_shape();
562 
563  std::vector< ShapeEnum<D,MultiIndex>* > shape_list(n_components());
564  std::vector< complex_t const* > coeffs_list(n_components());
565 
566  for (std::size_t n = 0; n < n_components(); n++) {
567  shape_list[n] = component(n).shape().get();
568  coeffs_list[n] = component(n).coefficients().data();
569  }
570 
571  return unionwp.template create_evaluator<N>(grid).vector_reduce(shape_list.data(), coeffs_list.data(), n_components());
572  }
573 
590  template<int N>
592  {
593  CMatrix<D,N> cgrid = rgrid.template cast<complex_t>();
594  return evaluate(cgrid);
595  }
596 
597  private:
600  std::vector<Component> components_;
601 
602  mutable std::vector< ShapeEnum<D,MultiIndex>* > union_cache_snapshot_;
604  }; // class HomogeneousHaWp
605 
606 
618  template<dim_t D, class MultiIndex>
620  {
621  public:
630  class Component : public AbstractScalarHaWp<D,MultiIndex>
631  {
632  public:
633  Component(InhomogeneousHaWp const* const owner)
634  : owner_(owner)
635  , shape_()
636  , coefficients_()
637  { }
638 
640  : owner_(that.owner_)
641  , parameters_(std::move(that.parameters_))
642  , shape_(that.shape_)
643  , coefficients_(std::move(that.coefficients_))
644  { }
645 
646  Component(Component const& that)
647  : owner_(that.owner_)
648  , parameters_(that.parameters_)
649  , shape_(that.shape_)
650  , coefficients_(that.coefficients_)
651  { }
652 
654  {
655  parameters_ = std::move(that.parameters_);
656  shape_ = that.shape_;
657  coefficients_ = std::move(that.coefficients_);
658  return *this;
659  }
660 
662  {
663  parameters_ = that.parameters_;
664  shape_ = that.shape_;
665  coefficients_ = that.coefficients_;
666  return *this;
667  }
668 
673  real_t eps() const override
674  {
675  return owner_->eps();
676  }
677 
682  {
683  return parameters_;
684  }
685 
686 
687  HaWpParamSet<D> const& parameters() const override
688  {
689  return parameters_;
690  }
691 
701  {
702  return shape_;
703  }
704 
706  {
707  return shape_;
708  }
709 
715  {
716  return coefficients_;
717  }
718 
719  Coefficients const& coefficients() const override
720  {
721  return coefficients_;
722  }
723 
724  private:
725  InhomogeneousHaWp const* const owner_;
726 
730  };
731 
732  InhomogeneousHaWp(std::size_t n)
733  : eps_()
734  , components_(n, Component(this))
735  { }
736 
742  {
743  return eps_;
744  }
745 
750  real_t eps() const
751  {
752  return eps_;
753  }
754 
759  std::vector<Component> & components()
760  {
761  return components_;
762  }
763 
768  std::vector<Component> const& components() const
769  {
770  return components_;
771  }
772 
780  Component & component(std::size_t n)
781  {
782  return components_[n];
783  }
784 
792  Component const& component(std::size_t n) const
793  {
794  return components_[n];
795  }
796 
804  Component & operator[](std::size_t n)
805  {
806  return component(n);
807  }
808 
816  Component const& operator[](std::size_t n) const
817  {
818  return component(n);
819  }
820 
824  std::size_t n_components() const
825  {
826  return components_.size();
827  }
828 
845  template<int N>
847  {
848  CArray<Eigen::Dynamic,N> result(n_components(),grid.cols());
849 
850  for (std::size_t c = 0; c < n_components(); c++) {
851  result.row(c) = component(c).evaluate(grid);
852  }
853 
854  return result;
855  }
856 
873  template<int N>
875  {
876  CMatrix<D,N> cgrid = rgrid.template cast<complex_t>();
877  return evaluate(cgrid);
878  }
879 
880  private:
882  std::vector<Component> components_;
883  };
884  }
885 }
real_t eps() const
Retrieves the semi-classical scaling parameter of the wavepacket.
Definition: hawp_commons.hpp:750
Definition: coefficients_file_parser.cpp:10
Coefficients coefficients_
Definition: hawp_commons.hpp:382
Coefficients coefficients_
Definition: hawp_commons.hpp:276
Represents a component of a homogeneous wavepacket.
Definition: hawp_commons.hpp:302
Component const & component(std::size_t n) const
Grants read-only access to the -th component .
Definition: hawp_commons.hpp:792
std::vector< ShapeEnum< D, MultiIndex > * > union_cache_snapshot_
Definition: hawp_commons.hpp:602
real_t eps_
Definition: hawp_commons.hpp:881
Coefficients & coefficients()
Grants writeable access to the coefficients of the wavepacket.
Definition: hawp_commons.hpp:262
real_t eps() const override
Retrieves the semi-classical scaling parameter of the wavepacket.
Definition: hawp_commons.hpp:221
shapes::ShapeEnumSharedPtr< D, MultiIndex > shape_
Definition: hawp_commons.hpp:275
virtual HaWpParamSet< D > const & parameters() const =0
Grants read-only access to the Hagedorn parameter set of the wavepacket.
HaWpParamSet< D > const & parameters() const override
Grants read-only access to the Hagedorn parameter set of the wavepacket.
Definition: hawp_commons.hpp:341
Eigen::Matrix< real_t, R, C > RMatrix
Definition: types.hpp:22
HaWpParamSet< D > & parameters()
Grants writeable access to the Hagedorn parameter set .
Definition: hawp_commons.hpp:681
real_t & eps()
Grants access to the semi-classical scaling parameter of the wavepacket.
Definition: hawp_commons.hpp:395
Eigen::Matrix< complex_t, Eigen::Dynamic, 1 > Coefficients
Definition: types.hpp:33
std::shared_ptr< ShapeEnum< D, MultiIndex > > ShapeEnumSharedPtr
Definition: shape_extension_cache.hpp:14
Represents a homogeneous Hagedorn wavepacket with components . All components share the same Hagedo...
Definition: hawp_commons.hpp:292
CArray< Eigen::Dynamic, N > evaluate(CMatrix< D, N > const &grid) const
Evaluate the value of all components at once.
Definition: hawp_commons.hpp:846
Component & operator[](std::size_t n)
Grants writeable access to the -th component .
Definition: hawp_commons.hpp:804
Output strict_union(Input1 begin1, Input1 end1, Input2 begin2, Input2 end2, Output sink, Compare less)
Creates union of two shape slices.
Definition: shape_enum_union.hpp:35
HomogeneousHaWp(std::size_t n)
Definition: hawp_commons.hpp:385
std::complex< real_t > complex_t
Definition: types.hpp:15
Definition: stdarray2stream.hpp:7
complex_t prefactor() const
Computes the prefactor .
Definition: hawp_commons.hpp:190
Component(Component const &that)
Definition: hawp_commons.hpp:646
HaWpParamSet< D > const & parameters() const override
Grants read-only access to the Hagedorn parameter set of the wavepacket.
Definition: hawp_commons.hpp:687
var c
Definition: jquery.js:23
Component & operator=(Component &&that)
Definition: hawp_commons.hpp:653
real_t & eps()
Grants access to the semi-classical scaling parameter of the wavepacket.
Definition: hawp_commons.hpp:741
real_t eps() const override
Retrieves the semi-classical scaling parameter of the wavepacket.
Definition: hawp_commons.hpp:336
CArray< Eigen::Dynamic, N > evaluate(CMatrix< D, N > const &grid) const
Evaluate the value of all components at once.
Definition: hawp_commons.hpp:555
double real_t
Definition: types.hpp:14
HaWpBasisVector< N > evaluate_basis(CMatrix< D, N > const &grid) const
Evaluates all basis functions on complex grid nodes .
Definition: hawp_commons.hpp:61
shapes::ShapeEnumSharedPtr< D, MultiIndex > & shape()
Grants access to the basis shape of the wavepacket.
Definition: hawp_commons.hpp:354
Coefficients & coefficients()
Grants writeable access to the coefficients of this wavepacket component.
Definition: hawp_commons.hpp:368
HomogeneousHaWp const *const owner_
Definition: hawp_commons.hpp:379
Component const & operator[](std::size_t n) const
Grants read-only access to the -th component .
Definition: hawp_commons.hpp:816
shapes::ShapeEnumSharedPtr< D, MultiIndex > cached_shape_union_
Definition: hawp_commons.hpp:603
Component const & operator[](std::size_t n) const
Grants read-only access to the -th component .
Definition: hawp_commons.hpp:488
Evaluates a wavepacket slice by slice.
Definition: hawp_evaluator.hpp:41
Component(HomogeneousHaWp const *const owner)
Definition: hawp_commons.hpp:305
InhomogeneousHaWp(std::size_t n)
Definition: hawp_commons.hpp:732
HaWpBasisVector< N > evaluate_basis(RMatrix< D, N > const &rgrid) const
Evaluates all basis functions on real grid nodes .
Definition: hawp_commons.hpp:79
shapes::ShapeEnumSharedPtr< D, MultiIndex > extended_shape() const
Computes the extension of the stored basis shape .
Definition: hawp_commons.hpp:104
Component const & component(std::size_t n) const
Grants read-only access to the -th component .
Definition: hawp_commons.hpp:464
Component & operator[](std::size_t n)
Grants writeable access to the -th component .
Definition: hawp_commons.hpp:476
HaWpParamSet< D > parameters_
Definition: hawp_commons.hpp:727
Component(Component &&that)
Definition: hawp_commons.hpp:310
shapes::ShapeExtensionCache< D, MultiIndex > shape_extension_cache_
Definition: hawp_commons.hpp:110
Component & operator=(Component const &that)
Definition: hawp_commons.hpp:661
Coefficients & coefficients()
Grants writeable access to the coefficients of this wavepacket component.
Definition: hawp_commons.hpp:714
real_t eps() const
Retrieves the semi-classical scaling parameter of the wavepacket.
Definition: hawp_commons.hpp:404
InhomogeneousHaWp const *const owner_
Definition: hawp_commons.hpp:725
shapes::ShapeEnumSharedPtr< D, MultiIndex > shape() const override
Retrieves the basis shape of the wavepacket.
Definition: hawp_commons.hpp:359
CArray< Eigen::Dynamic, N > evaluate(RMatrix< D, N > const &rgrid) const
Evaluates the value of all components at once.
Definition: hawp_commons.hpp:591
CArray< Eigen::Dynamic, N > evaluate(RMatrix< D, N > const &rgrid) const
Evaluates the value of all components at once.
Definition: hawp_commons.hpp:874
This class represents the Hagedorn parameter set .
Definition: hawp_paramset.hpp:24
Definition: shape_extension_cache.hpp:26
Component & operator=(Component &&that)
Definition: hawp_commons.hpp:322
real_t eps_
Definition: hawp_commons.hpp:273
Component(InhomogeneousHaWp const *const owner)
Definition: hawp_commons.hpp:633
CArray< Eigen::Dynamic, N > HaWpBasisVector
Definition: types.hpp:31
HaWpParamSet< D > parameters_
Definition: hawp_commons.hpp:274
Abstract superclass that represents a set of basis function to a scalar Hagedorn wavepacket.
Definition: hawp_commons.hpp:25
std::size_t n_components() const
Returns the number of components.
Definition: hawp_commons.hpp:496
HaWpParamSet< D > & parameters()
Grants writeable access to the Hagedorn parameter set of the wavepacket.
Definition: hawp_commons.hpp:230
real_t & eps()
Grants writeable access to the semi-classical scaling parameter of the wavepacket.
Definition: hawp_commons.hpp:216
shapes::ShapeEnumSharedPtr< D, MultiIndex > shape() const override
Retrieves the basis shape of the wavepacket.
Definition: hawp_commons.hpp:705
CArray< 1, N > evaluate(CMatrix< D, N > const &grid) const
Evaluates this wavepacket at complex grid nodes .
Definition: hawp_commons.hpp:155
HaWpParamSet< D > parameters_
Definition: hawp_commons.hpp:599
Represents an inhomogeneous Hagedorn wavepacket with components . All components have a different s...
Definition: hawp_commons.hpp:619
Component & operator=(Component const &that)
Definition: hawp_commons.hpp:329
Coefficients const & coefficients() const override
Grants read-only access to the coefficients for all of this wavepacket.
Definition: hawp_commons.hpp:719
virtual shapes::ShapeEnumSharedPtr< D, MultiIndex > shape() const =0
Retrieves the basis shape of the wavepacket.
HaWpParamSet< D > const & parameters() const
Grants read-only access to the Hagedorn parameter set of the wavepacket.
Definition: hawp_commons.hpp:422
Coefficients const & coefficients() const override
Grants read-only access to the coefficients for all of this wavepacket.
Definition: hawp_commons.hpp:267
Component(Component &&that)
Definition: hawp_commons.hpp:639
shapes::ShapeEnumSharedPtr< D, MultiIndex > & shape()
Grants access to the basis shape of the wavepacket.
Definition: hawp_commons.hpp:248
std::vector< Component > & components()
Grants writeable access to all components of this wavepacket.
Definition: hawp_commons.hpp:431
Component(Component const &that)
Definition: hawp_commons.hpp:316
Abstract superclass that represents a scalar (1-component) hagedorn wavepacket.
Definition: hawp_commons.hpp:129
Component & component(std::size_t n)
Grants writeable access to the -th component .
Definition: hawp_commons.hpp:780
std::vector< Component > & components()
Grants writeable access to all components of this wavepacket.
Definition: hawp_commons.hpp:759
shapes::ShapeEnumSharedPtr< D, MultiIndex > & shape()
Grants access to the basis shape of the wavepacket.
Definition: hawp_commons.hpp:700
complex_t phasefactor() const
Computes the global phase factor .
Definition: hawp_commons.hpp:198
Concrete implementation of a scalar Hagedorn wavepacket.
Definition: hawp_commons.hpp:209
Eigen::Matrix< complex_t, R, C > CMatrix
Definition: types.hpp:19
std::size_t n_components() const
Returns the number of components.
Definition: hawp_commons.hpp:824
std::vector< Component > components_
Definition: hawp_commons.hpp:600
shapes::ShapeEnumSharedPtr< D, MultiIndex > shape_
Definition: hawp_commons.hpp:381
std::vector< Component > const & components() const
Grants read-only access to all components of this wavepacket.
Definition: hawp_commons.hpp:768
virtual real_t eps() const =0
Retrieves the semi-classical scaling parameter of the wavepacket.
Component & component(std::size_t n)
Grants writeable access to the -th component .
Definition: hawp_commons.hpp:452
Coefficients coefficients_
Definition: hawp_commons.hpp:729
std::vector< Component > const & components() const
Grants read-only access to all components of this wavepacket.
Definition: hawp_commons.hpp:440
real_t eps_
Definition: hawp_commons.hpp:598
HaWpEvaluator< D, MultiIndex, N > create_evaluator(CMatrix< D, N > const &grid) const
Definition: hawp_commons.hpp:47
CArray< 1, N > evaluate(RMatrix< D, N > const &rgrid) const
Evaluates this wavepacket at real grid nodes .
Definition: hawp_commons.hpp:176
Represents a component of an inhomogeneous wavepacket .
Definition: hawp_commons.hpp:630
std::vector< Component > components_
Definition: hawp_commons.hpp:882
shapes::ShapeEnumSharedPtr< D, MultiIndex > shape_
Definition: hawp_commons.hpp:728
Coefficients const & coefficients() const override
Grants read-only access to the coefficients for all of this wavepacket.
Definition: hawp_commons.hpp:373
HaWpParamSet< D > const & parameters() const override
Grants read-only access to the Hagedorn parameter set of the wavepacket.
Definition: hawp_commons.hpp:235
HaWpParamSet< D > & parameters()
Grants writeable access to the Hagedorn parameter set of the wavepacket.
Definition: hawp_commons.hpp:413
shapes::ShapeEnumSharedPtr< D, MultiIndex > union_shape() const
Computes the union of basis shapes of all components.
Definition: hawp_commons.hpp:508
shapes::ShapeEnumSharedPtr< D, MultiIndex > shape() const override
Retrieves the basis shape of the wavepacket.
Definition: hawp_commons.hpp:253
Eigen::Array< complex_t, R, C > CArray
Definition: types.hpp:25
real_t eps() const override
Forwards the scaling parameter of the owning inhomogeneous wavepacket.
Definition: hawp_commons.hpp:673