pfc_rb.cc 7.28 KB
Newer Older
1
2
3
4
#include "AMDiS.h"
#include "Helpers.h"
#include "PhaseFieldCrystal.h"

Praetorius, Simon's avatar
Praetorius, Simon committed
5
#if defined(USE_SEQ_PETSC)
6
#include "preconditioner/PetscPreconPfc.h"
Praetorius, Simon's avatar
Praetorius, Simon committed
7
#elif defined(USE_PARALLEL_PETSC)
8
#include "preconditioner/PetscSolverPfc.h"
Praetorius, Simon's avatar
Praetorius, Simon committed
9
#elif defined(USE_MTL)
10
11
12
13
14
15
16
17
18
19
20
#include "preconditioner/MTLPreconPfc.h"
#endif

#include "GenericOperatorTerm.h"

using namespace AMDiS;

class PFC_RosenbrockStationary : public RosenbrockStationary
{
public:
  PFC_RosenbrockStationary(std::string name) : RosenbrockStationary(name) {};
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  void addTimeOperator(int row, int col) override
  {
    FUNCNAME("PFC_RosenbrockStationary::addTimeOperator()");
    MSG("PFC_RosenbrockStationary::addTimeOperator()\n");

    Operator *op = new Operator(componentSpaces[row], componentSpaces[col]);
    addZOT(op, 1.0 / rm->getGamma() );
    ProblemStat::addMatrixOperator(op, row, col);

    Operator *opRhs = new Operator(componentSpaces[row]);
    addZOT(opRhs, valueOf(timeRhsVec->getDOFVector(col)) );
    ProblemStat::addVectorOperator(opRhs, row, tauPtr, tauPtr);
  }
};

class PfcPC : public base_problems::detail::PhaseFieldCrystal<PFC_RosenbrockStationary>
{
public:
  typedef base_problems::detail::PhaseFieldCrystal<PFC_RosenbrockStationary> super;
41

42
43
public:
  PfcPC(std::string name) : super(name) { }
44

45
46
47
48
49
  /// initialize the preconditioners, i.e. set parameters
  void initData() override
  {
    super::initData();

Praetorius, Simon's avatar
Praetorius, Simon committed
50
#if defined(USE_SEQ_PETSC)
51
52
53
    // sequential PFC preconditioner
    PetscPreconPfc* runner = dynamic_cast<PetscPreconPfc*>(prob->getSolver()->getRunner());
    if (runner) {
54
      runner->setData(getTau());
55
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
56
#elif defined(USE_PARALLEL_PETSC)
57
58
59
60
    // parallel PFC preconditioner
    Parallel::PetscSolverPfc* solver = dynamic_cast<Parallel::PetscSolverPfc*>(prob->getSolver());
    if (solver)
      solver->setData(getTau());
Praetorius, Simon's avatar
Praetorius, Simon committed
61
#elif defined(USE_MTL)
62
    // sequential PFC preconditioner using MTL
63
64
65
    using AMDiS::extensions::MTLPreconPfc;
    MTLPreconPfc* precon = dynamic_cast<MTLPreconPfc*>(prob->getSolver()->getRightPrecon());
    if (precon)
66
      precon->setData(getTau());
67
68
#endif
  }
69

70
71
72
73
74
75
76
  /// generate initial solution for evolution equation
  void solveInitialProblem(AdaptInfo *adaptInfo) override
  { FUNCNAME("PFC_Demo::solveInitialProblem()");

    Flag initFlag = initDataFromFile(adaptInfo);
    if (initFlag.isSet(DATA_ADOPTED))
      return;
77

78
79
    double amplitude = 0.1;
    Parameters::get(name + "->density amplitude",amplitude);
80

81
82
83
    DOFVector<double>* rho = prob->getSolution()->getDOFVector(1);
    *rho << eval(new Random(density, amplitude));
  }
84

85
86
87
88
89
90
  /// add Rosenbrock operators
  void fillOperators() override
  {
    const FiniteElemSpace* feSpace0 = prob->getFeSpace(0);
    const FiniteElemSpace* feSpace1 = prob->getFeSpace(1);
    const FiniteElemSpace* feSpace2 = prob->getFeSpace(2);
91

92
93
94
    DOFVector<double>* mu = prob->getStageSolution(0);
    DOFVector<double>* rho = prob->getStageSolution(1);
    DOFVector<double>* nu = prob->getStageSolution(2);
95

96
97
    DOFVector<double>* mu_old = prob->getUnVec(0);
    DOFVector<double>* rho_old = prob->getUnVec(1);
98

99
100
101
102
103
104
105
    // F = -mu_s + (1+r)*rho_s + 2*laplace(rho_s) + laplace(nu_s) + rho_s^3
    // -J = mu - (1+r)*rho - 2*laplace(rho) - laplace(nu) - 3*rho_old^2 * rho
    // ----------------------------------------------------------------------
    Operator *op01_lhs = new Operator(feSpace0, feSpace1);
    addZOT(op01_lhs, -(1.0 + r) - 3.0 * pow<2>(valueOf(rho_old)) );
    addSOT(op01_lhs, 2.0);
    prob->addMatrixOperator(op01_lhs, 0, 1);
106

107
108
109
    Operator *op00_lhs = new Operator(feSpace0, feSpace0);
    addZOT(op00_lhs, 1.0);
    prob->addMatrixOperator(op00_lhs, 0, 0);
110

111
112
113
    Operator *op02_lhs = new Operator(feSpace0, feSpace2);
    addSOT(op02_lhs, 1.0);
    prob->addMatrixOperator(op02_lhs, 0, 2);
114

115
    // ------- //
116

117
    Operator *op01_rhs0 = new Operator(feSpace0, feSpace1);
118
    addSOT(op01_rhs0, -2.0);
119
120
    op01_rhs0->setUhOld(rho);
    prob->addVectorOperator(op01_rhs0, 0);
121

122
123
124
    Operator *op01_rhs1 = new Operator(feSpace0, feSpace1);
    addZOT(op01_rhs1, ((1.0 + r) + pow<2>(valueOf(rho)))*valueOf(rho) - valueOf(mu) );
    prob->addVectorOperator(op01_rhs1, 0);
125

126
127
128
129
    Operator *op02_rhs = new Operator(feSpace0, feSpace2);
    addSOT(op02_rhs, -1.0);
    op02_rhs->setUhOld(nu);
    prob->addVectorOperator(op02_rhs, 0);
130

131
132
133
    // dt(rho) = laplace(mu)
    // -----------------------------------
    prob->addTimeOperator(1, 1);
134

135
136
    Operator *opLM_lhs0 = new Operator(feSpace1, feSpace0);
    Operator *opLM_rhs = new Operator(feSpace1, feSpace0);
137
    if (mobility_type > 0) {
138
      addSOT(opLM_lhs0, max(abs_(valueOf(rho_old) + 1.5)*M0, 1.e-5));
139

140
141
      // ------- //
      Operator *opLM_lhs1 = new Operator(feSpace1, feSpace0);
142
143
144
145
146
      addFOT(opLM_lhs1, gradientOf(mu_old), GRD_PSI);

      prob->addMatrixOperator(opLM_lhs0, 1, 0, prob->getTau(), prob->getTau());
      prob->addMatrixOperator(opLM_lhs1, 1, 1, prob->getTau(), prob->getTau());

147
148
149
150
151
152
      // ------- //
      addSOT(opLM_rhs, -max(abs_(valueOf(rho) + 1.5)*M0, 1.e-5));
      opLM_rhs->setUhOld(mu);
      prob->addVectorOperator(opLM_rhs, 1, prob->getTau(), prob->getTau());
    } else {
      addSOT(opLM_lhs0, M0);
153
154
      prob->addMatrixOperator(opLM_lhs0, 1, 0, prob->getTau(), prob->getTau());

155
156
157
158
159
      // ------- //
      addSOT(opLM_rhs, -M0);
      opLM_rhs->setUhOld(mu);
      prob->addVectorOperator(opLM_rhs, 1, prob->getTau(), prob->getTau());
    }
160

161
162
163
164
165
166
    // F  =-nu + laplace(rho)
    // -J = nu - laplace(rho)
    // -------------------
    Operator *op21_lhs = new Operator(feSpace2, feSpace1);
    addSOT(op21_lhs, 1.0);
    prob->addMatrixOperator(op21_lhs, 2, 1);
167

168
169
170
    Operator *op22_lhs = new Operator(feSpace2, feSpace2);
    addZOT(op22_lhs, 1.0);
    prob->addMatrixOperator(op22_lhs, 2, 2); // nu
171
172
173

    // ------- //

174
175
176
177
    Operator *op21_rhs = new Operator(feSpace2, feSpace1);
    addSOT(op21_rhs, -1.0);
    op21_rhs->setUhOld(rho);
    prob->addVectorOperator(op21_rhs, 2);
178

179
180
181
182
183
184
185
186
187
188
189
190
    Operator *op22_rhs = new Operator(feSpace2, feSpace2);
    addZOT(op22_rhs, -valueOf(nu));
    prob->addVectorOperator(op22_rhs, 2); // nu
  }
};


int main(int argc, char** argv)
{ FUNCNAME("main");

  AMDiS::init(argc, argv);
  Timer t;
191

192
  // add preconditioner / solver to the parameter list. Must be added before problem is initialized.
Praetorius, Simon's avatar
Praetorius, Simon committed
193
194
195
#if defined(USE_SEQ_PETSC)
  CreatorMap<PetscPreconditionerNested>::addCreator("petsc_pfc", new PetscPreconPfc::Creator);
#elif defined(USE_PARALLEL_PETSC)
196
  CreatorMap<LinearSolverInterface>::addCreator("p_petsc_pfc", new Parallel::PetscSolverPfc::Creator);
Praetorius, Simon's avatar
Praetorius, Simon committed
197
#elif defined(USE_MTL)
198
  using AMDiS::extensions::MTLPreconPfc;
199
  CreatorMap<typename MTLPreconPfc::precon_base>::addCreator("pfc", new MTLPreconPfc::Creator);
200
#endif
201

202
203
204
205
206
207
  // create and initialize the PFC BaseProblem
  PfcPC pfcProb("pfc");
  pfcProb.initialize(INIT_ALL);

  // Adapt-Infos
  AdaptInfo adaptInfo("adapt", pfcProb.getNumComponents());
Praetorius, Simon's avatar
Praetorius, Simon committed
208
  RosenbrockAdaptInstationary adaptInstat("adapt", pfcProb.getProblem(), pfcProb.getProblem(), &adaptInfo, &pfcProb, &adaptInfo);
209
210
211
212
213
214
215
216
217
218
219
  pfcProb.initTimeInterface(); // fill operators and BC

  // Scale Mesh
  bool scaleMesh = false;
  Initfile::get("mesh->scale mesh",scaleMesh);
  if (scaleMesh) {
    WorldVector<double> scale; scale.set(1.0);
    Initfile::get("mesh->dimension",scale);
    Helpers::scaleMesh(pfcProb.getMesh(), scale);
  }

220
221
222
  int error_code = adaptInstat.adapt();

  MSG("elapsed time= %f sec\n", t.elapsed());
223
224
225
  AMDiS::finalize();
  return error_code;
}