Peano
MyObserver.cpp
Go to the documentation of this file.
1 #include "MyObserver.h"
2 
3 #include "peano4/grid/GridControlEvent.h"
4 #include "peano4/parallel/Node.h"
5 #include "peano4/parallel/Tasks.h"
6 #include "peano4/grid/GridTraversalEvent.h"
7 
8 #include "tarch/la/MatrixOperations.h"
9 #include "tarch/multicore/Core.h"
10 
11 #include "toolbox/finiteelements/ElementMatrix.h"
12 #include "toolbox/finiteelements/StencilFactory.h"
13 
14 
15 tarch::logging::Log examples::regulargridupscaling::MyObserver::_log( "examples::regulargridupscaling::MyObserver" );
18 
19 
20 examples::regulargridupscaling::MyObserver::MyObserver(int spacetreeId, double h, int flopsPerCell):
21  _spacetreeId(spacetreeId),
22  _h(h),
23  _flopsPerCell(flopsPerCell),
24  _accumulator(0.0) {
25 }
26 
27 
29 }
30 
31 
33  const tarch::la::Vector<DIMENSIONS,double>& x,
34  const tarch::la::Vector<DIMENSIONS,double>& h
35 ) {
36  _accumulator = 0.0;
37  _taskAccumulator = 0.0;
38 }
39 
40 
42  const tarch::la::Vector<DIMENSIONS,double>& x,
43  const tarch::la::Vector<DIMENSIONS,double>& h
44 ) {
45  tarch::multicore::processPendingTasks();
46  logDebug( "endTraversal", _accumulator );
47 }
48 
49 
51  const peano4::grid::GridTraversalEvent& event
52 ) {
53  if (
54  event.getIsRefined()==false
55  and
56  event.getCellData() != TraversalObserver::NoData
57  ) {
58  for (int i=0; i<_flopsPerCell; i++) {
59  _accumulator += 1.0;
60  }
61 
62  if (_spacetreeId==0) {
63  _taskAccumulator += std::abs(FractionOfCellsYieldingIntegrationTask) * tarch::multicore::Core::getInstance().getNumberOfThreads();
64  }
65 
66  if (_taskAccumulator>=1.0) {
67  _taskAccumulator -= 1.0;
68  assertion(IntegrationAccuracy>=1);
69  peano4::parallel::Tasks(
70  [&]() -> bool {
71  tarch::la::Matrix<TWO_POWER_D,TWO_POWER_D,double> localStiffnessMatrix =
72  toolbox::finiteelements::getPoissonMatrixWithJumpingCoefficient(
73  event.getX(), event.getH(), IntegrationAccuracy,
74  [](const tarch::la::Vector<DIMENSIONS,double>& x) -> double {
75  const double Theta = 32.0;
76  return 1.0
77  + 0.3/DIMENSIONS * std::exp(-Theta * x(0)) * std::cos( tarch::la::PI * x(0) * Theta )
78  + 0.3/DIMENSIONS * std::exp(-Theta * x(1)) * std::cos( tarch::la::PI * x(1) * Theta );
79  }
80  );
81  // just to ensure we cannot remove it.
82  _accumulator += tarch::la::frobeniusNorm(localStiffnessMatrix);
83  return false;
84  },
85  FractionOfCellsYieldingIntegrationTask<0 ? peano4::parallel::Tasks::TaskType::Sequential : peano4::parallel::Tasks::TaskType::Task,
86  peano4::parallel::Tasks::getLocationIdentifier( "examples::regulargridupscaling::MyObserver::enterCell" )
87  );
88  }
89  }
90 }
91 
92 
94  const peano4::grid::GridTraversalEvent& event
95 ) {
96 }
97 
98 
99 peano4::grid::TraversalObserver* examples::regulargridupscaling::MyObserver::clone(int spacetreeId) {
100  return new MyObserver( spacetreeId, _h, _flopsPerCell );
101 }
102 
103 
104 std::vector< peano4::grid::GridControlEvent > examples::regulargridupscaling::MyObserver::getGridControlEvents() {
105  std::vector< peano4::grid::GridControlEvent > controlEvents;
106 
107  peano4::grid::GridControlEvent newEvent;
108  newEvent.setRefinementControl( peano4::grid::GridControlEvent::RefinementControl::Refine );
109  #if DIMENSIONS==2
110  newEvent.setOffset( {0.0,0.0} );
111  newEvent.setWidth( {1.0,1.0} );
112  newEvent.setH( {_h,_h} );
113  #elif DIMENSIONS==3
114  newEvent.setOffset( {0.0,0.0,0.0} );
115  newEvent.setWidth( {1.0,1.0,1.0} );
116  newEvent.setH( {_h,_h,_h} );
117  #endif
118  controlEvents.push_back(newEvent);
119 
120  return controlEvents;
121 }
static tarch::logging::Log _log
Definition: MyObserver.h:20
void enterCell(const peano4::grid::GridTraversalEvent &event) override
Definition: MyObserver.cpp:48
std::vector< peano4::grid::GridControlEvent > getGridControlEvents() override
Definition: MyObserver.cpp:84
void leaveCell(const peano4::grid::GridTraversalEvent &event) override
Definition: MyObserver.cpp:57
MyObserver(int spacetreeId, double h, int flopsPerCell)
Definition: MyObserver.cpp:13
void endTraversal(const tarch::la::Vector< DIMENSIONS, double > &x, const tarch::la::Vector< DIMENSIONS, double > &h) override
Definition: MyObserver.cpp:38
TraversalObserver * clone(int spacetreeId) override
I use the clone to create one observer object per traversal thread.
Definition: MyObserver.cpp:66
void beginTraversal(const tarch::la::Vector< DIMENSIONS, double > &x, const tarch::la::Vector< DIMENSIONS, double > &h) override
Definition: MyObserver.cpp:28
h
Definition: swe.py:79