PDESolver.hpp 3.24 KB
Newer Older
niklas.baumgarten's avatar
niklas.baumgarten committed
1
2
3
#ifndef PDESOLVER_HPP
#define PDESOLVER_HPP

niklas.baumgarten's avatar
imports    
niklas.baumgarten committed
4
#include "IStochasticDummyAssemble.hpp"
niklas.baumgarten's avatar
niklas.baumgarten committed
5
#include "Sample.hpp"
6

7
#include "TimeIntegrator.hpp"
8
#include "Newton.hpp"
niklas.baumgarten's avatar
niklas.baumgarten committed
9
#include "GMRES.hpp"
niklas.baumgarten's avatar
niklas.baumgarten committed
10
#include "Plotting.hpp"
11

12
#include <typeinfo>
niklas.baumgarten's avatar
niklas.baumgarten committed
13
14
15
16


class PDESolver {
protected:
17
  int verbose = 1;
niklas.baumgarten's avatar
niklas.baumgarten committed
18

19
  int plotting = 0;
niklas.baumgarten's avatar
niklas.baumgarten committed
20

niklas.baumgarten's avatar
niklas.baumgarten committed
21
  const Meshes &meshes;
niklas.baumgarten's avatar
niklas.baumgarten committed
22

23
  std::string quantity;
24

25
  std::string costMeasure;
niklas.baumgarten's avatar
niklas.baumgarten committed
26

27
  virtual void run(SampleSolution &solution) = 0;
niklas.baumgarten's avatar
niklas.baumgarten committed
28

29
  virtual void computeQ(SampleSolution &solution) = 0;
niklas.baumgarten's avatar
niklas.baumgarten committed
30

31
  virtual void computeCost(SampleSolution &solution) = 0;
niklas.baumgarten's avatar
niklas.baumgarten committed
32

33
  virtual void plotSolution(SampleSolution &solution) = 0;
niklas.baumgarten's avatar
niklas.baumgarten committed
34

35
36
37
38
  void weightSolution(SampleSolution &solution) const {
    solution.Q = GetProblem()->SampleWeight(solution.id) * solution.Q;
  }

niklas.baumgarten's avatar
niklas.baumgarten committed
39
public:
niklas.baumgarten's avatar
niklas.baumgarten committed
40
  PDESolver(const Meshes &meshes, const std::string &quantity,
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
            const std::string &costMeasure) :
    meshes(meshes), quantity(quantity), costMeasure(costMeasure) {
    config.get("PDESolverVerbose", verbose);
    config.get("PDESolverPlotting", plotting);
  }

  virtual ~PDESolver() {};

  virtual void PrintInfo() const {
    if (verbose > 0)
      mout.PrintInfo(Name(), verbose,
                     PrintInfoEntry("Quantity", quantity),
                     PrintInfoEntry("Cost measure", costMeasure));
  }

  void Run(SampleSolution &solution) {
    mout.StartBlock("PDE Solver");
    vout(2) << solution.IdString() << endl;
    run(solution);
    computeQ(solution);
    computeCost(solution);
    plotSolution(solution);
63
64
    weightSolution(solution);
    // Todo other idea: Add weight as class member of solution
65
66
67
68
69
70
71
72
73
74
75
76
77
    vout(2) << "Q=" << solution.Q << " cost=" << solution.Cost << endl;
    mout.EndBlock(verbose <= 1);
  }

  virtual IAssemble *GetAssemble() const = 0;

  virtual IDiscretization *GetDisc() const = 0;

  virtual IStochasticProblem *GetProblem() const = 0;

  virtual void DrawSample(const SampleID &id) = 0;

  virtual std::string Name() const = 0;
niklas.baumgarten's avatar
niklas.baumgarten committed
78
79
};

niklas.baumgarten's avatar
niklas.baumgarten committed
80
81
class DummyPDESolver : public PDESolver {
private:
82
  IStochasticDummyAssemble *assemble;
niklas.baumgarten's avatar
niklas.baumgarten committed
83
84

protected:
niklas.baumgarten's avatar
niklas.baumgarten committed
85
  void run(SampleSolution &solution) override {}
niklas.baumgarten's avatar
niklas.baumgarten committed
86

niklas.baumgarten's avatar
niklas.baumgarten committed
87
  void computeQ(SampleSolution &solution) override {
88
    if (quantity == "FunctionEvaluation") solution.Q = assemble->FunctionEvaluation();
niklas.baumgarten's avatar
niklas.baumgarten committed
89
90
    else Exit("Quantity of interest not implemented")
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
91

niklas.baumgarten's avatar
niklas.baumgarten committed
92
93
94
95
96
  void computeCost(SampleSolution &solution) override {
    if (costMeasure == "size") solution.Cost = solution.U.size();
//    else if (costMeasure == "time") solution.Cost = solution.U.size(); // Todo
    else Exit("Cost measure not implemented")
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
97

niklas.baumgarten's avatar
niklas.baumgarten committed
98
  void plotSolution(SampleSolution &solution) override {
niklas.baumgarten's avatar
niklas.baumgarten committed
99
    mpp::plot_mesh(solution.U.GetMesh());
niklas.baumgarten's avatar
niklas.baumgarten committed
100
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
101
102

public:
103
  DummyPDESolver(IStochasticDummyAssemble *assemble,
niklas.baumgarten's avatar
niklas.baumgarten committed
104
                 const Meshes &meshes,
105
106
                 const std::string &quantity = "L2",
                 const std::string &costMeasure = "size") :
107
    PDESolver(meshes, quantity, costMeasure), assemble(assemble) {}
108

109
  IAssemble *GetAssemble() const override { return assemble; }
niklas.baumgarten's avatar
niklas.baumgarten committed
110

111
  IDiscretization *GetDisc() const override { return assemble->GetDisc(); }
niklas.baumgarten's avatar
niklas.baumgarten committed
112

113
  IStochasticProblem *GetProblem() const override { return assemble->GetProblem(); }
niklas.baumgarten's avatar
niklas.baumgarten committed
114

115
  void DrawSample(const SampleID &id) override { assemble->DrawSample(id); }
niklas.baumgarten's avatar
niklas.baumgarten committed
116

117
  std::string Name() const override { return "DummyPDESolver"; }
niklas.baumgarten's avatar
niklas.baumgarten committed
118
119
};

niklas.baumgarten's avatar
niklas.baumgarten committed
120
#endif //PDESOLVER_HPP