PDESolver.hpp 2.83 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
  void weightSolution(SampleSolution &solution) const {
36
    solution.W = GetProblem()->SampleWeight(solution.id);
37
38
  }

niklas.baumgarten's avatar
niklas.baumgarten committed
39
public:
40
41
  PDESolver(const Meshes &meshes,
            const std::string &quantity,
42
43
44
45
46
47
            const std::string &costMeasure) :
    meshes(meshes), quantity(quantity), costMeasure(costMeasure) {
    config.get("PDESolverVerbose", verbose);
    config.get("PDESolverPlotting", plotting);
  }

48
  virtual ~PDESolver() = default;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

  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);
64
    weightSolution(solution);
niklas.baumgarten's avatar
niklas.baumgarten committed
65
    vout(2) << "Q=" << solution.Q << " cost=" << solution.C << endl;
66
67
68
69
70
71
72
73
74
75
76
77
    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 {
niklas.baumgarten's avatar
niklas.baumgarten committed
88
    solution.Q = assemble->FunctionEvaluation();
niklas.baumgarten's avatar
niklas.baumgarten committed
89
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
90

niklas.baumgarten's avatar
niklas.baumgarten committed
91
  void computeCost(SampleSolution &solution) override {
niklas.baumgarten's avatar
niklas.baumgarten committed
92
    solution.C = 1.0;  // Cost corresponds to one function evaluation
niklas.baumgarten's avatar
niklas.baumgarten committed
93
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
94

niklas.baumgarten's avatar
niklas.baumgarten committed
95
  void plotSolution(SampleSolution &solution) override {}
niklas.baumgarten's avatar
niklas.baumgarten committed
96
97

public:
niklas.baumgarten's avatar
niklas.baumgarten committed
98
99
100
  // Todo remove costMeasure and quantity
  DummyPDESolver(IStochasticDummyAssemble *assemble, const Meshes &meshes) :
    PDESolver(meshes, "", ""), assemble(assemble) {}
101

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

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

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

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

110
  std::string Name() const override { return "DummyPDESolver"; }
niklas.baumgarten's avatar
niklas.baumgarten committed
111
112
};

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