pfc_rb.cc 7.79 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "AMDiS.h"
#include "Helpers.h"
#include "PhaseFieldCrystal.h"

#if (defined HAVE_SEQ_PETSC) || (defined HAVE_PETSC)
#include "preconditioner/PetscPreconPfc.h"
#endif
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
#include "preconditioner/PetscSolverPfc.h"
#endif
#if (!defined HAVE_SEQ_PETSC) && (!defined HAVE_PETSC) && (!defined HAVE_PARALLEL_DOMAIN_AMDIS)
#include "preconditioner/MTLPreconPfc.h"
#endif

#include "GenericOperatorTerm.h"

using namespace AMDiS;

class PFC_RosenbrockStationary : public RosenbrockStationary
{
public:
  PFC_RosenbrockStationary(std::string name) : RosenbrockStationary(name) {};
  
  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;
  
public:
  PfcPC(std::string name) : super(name) { }
  
  /// initialize the preconditioners, i.e. set parameters
  void initData() override
  {
    super::initData();

    // sequential PFC preconditioner
#if (defined HAVE_SEQ_PETSC) || (defined HAVE_PETSC)
    PetscPreconPfc* runner = dynamic_cast<PetscPreconPfc*>(prob->getSolver()->getRunner());
    if (runner) {
      dynamic_cast<PetscSolver<PetscPreconPfc>*>(prob->getSolver())->setNested(true);
      runner->setData(getTau());	
    }
#endif

    // parallel PFC preconditioner
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    Parallel::PetscSolverPfc* solver = dynamic_cast<Parallel::PetscSolverPfc*>(prob->getSolver());
    if (solver)
      solver->setData(getTau());
#endif
      
    // sequential PFC preconditioner using MTL    
#if (!defined HAVE_SEQ_PETSC) && (!defined HAVE_PETSC) && (!defined HAVE_PARALLEL_DOMAIN_AMDIS)
    using AMDiS::extensions::MTLPreconPfc;
    MTLPreconPfc* precon = dynamic_cast<MTLPreconPfc*>(prob->getSolver()->getRightPrecon());
    if (precon)
      precon->setData(getTau());	
#endif
  }
  
  /// 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;
    
    double amplitude = 0.1;
    Parameters::get(name + "->density amplitude",amplitude);
      
    DOFVector<double>* rho = prob->getSolution()->getDOFVector(1);
    *rho << eval(new Random(density, amplitude));
  }
  
  /// add Rosenbrock operators
  void fillOperators() override
  {
    const FiniteElemSpace* feSpace0 = prob->getFeSpace(0);
    const FiniteElemSpace* feSpace1 = prob->getFeSpace(1);
    const FiniteElemSpace* feSpace2 = prob->getFeSpace(2);
    
    DOFVector<double>* mu = prob->getStageSolution(0);
    DOFVector<double>* rho = prob->getStageSolution(1);
    DOFVector<double>* nu = prob->getStageSolution(2);
    
    DOFVector<double>* mu_old = prob->getUnVec(0);
    DOFVector<double>* rho_old = prob->getUnVec(1);
    
    // 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);
    
    Operator *op00_lhs = new Operator(feSpace0, feSpace0);
    addZOT(op00_lhs, 1.0);
    prob->addMatrixOperator(op00_lhs, 0, 0);
    
    Operator *op02_lhs = new Operator(feSpace0, feSpace2);
    addSOT(op02_lhs, 1.0);
    prob->addMatrixOperator(op02_lhs, 0, 2);
    
    // ------- //
    
    Operator *op01_rhs0 = new Operator(feSpace0, feSpace1);
    addSOT(op01_rhs0, -2.0); 
    op01_rhs0->setUhOld(rho);
    prob->addVectorOperator(op01_rhs0, 0);
    
    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);
    
    Operator *op02_rhs = new Operator(feSpace0, feSpace2);
    addSOT(op02_rhs, -1.0);
    op02_rhs->setUhOld(nu);
    prob->addVectorOperator(op02_rhs, 0);
    
    // dt(rho) = laplace(mu)
    // -----------------------------------
    prob->addTimeOperator(1, 1);
    
    Operator *opLM_lhs0 = new Operator(feSpace1, feSpace0);
    Operator *opLM_rhs = new Operator(feSpace1, feSpace0);
    if (useMobility) {
      addSOT(opLM_lhs0, max(abs_(valueOf(rho_old) + 1.5)*M0, 1.e-5));
      
      // ------- //
      Operator *opLM_lhs1 = new Operator(feSpace1, feSpace0);
      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()); 
      
      // ------- //
      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);
      prob->addMatrixOperator(opLM_lhs0, 1, 0, prob->getTau(), prob->getTau()); 
      
      // ------- //
      addSOT(opLM_rhs, -M0);
      opLM_rhs->setUhOld(mu);
      prob->addVectorOperator(opLM_rhs, 1, prob->getTau(), prob->getTau());
    }
    
    // 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);
    
    Operator *op22_lhs = new Operator(feSpace2, feSpace2);
    addZOT(op22_lhs, 1.0);
    prob->addMatrixOperator(op22_lhs, 2, 2); // nu
    
    // ------- //    
    
    Operator *op21_rhs = new Operator(feSpace2, feSpace1);
    addSOT(op21_rhs, -1.0);
    op21_rhs->setUhOld(rho);
    prob->addVectorOperator(op21_rhs, 2);
    
    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;
  
  // add preconditioner / solver to the parameter list. Must be added before problem is initialized.
#if (defined HAVE_SEQ_PETSC) || (defined HAVE_PETSC)
  CreatorMap<LinearSolver>::addCreator("petsc_pfc", new PetscSolver<PetscPreconPfc>::Creator);
#endif
  
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
  CreatorMap<LinearSolver>::addCreator("p_petsc_pfc", new Parallel::PetscSolverPfc::Creator);
#endif
  
#if (!defined HAVE_SEQ_PETSC) && (!defined HAVE_PETSC) && (!defined HAVE_PARALLEL_DOMAIN_AMDIS)
  using AMDiS::extensions::MTLPreconPfc;
  CreatorMap<BasePreconditioner>::addCreator("pfc", new MTLPreconPfc::Creator);
#endif
  
  // create and initialize the PFC BaseProblem
  PfcPC pfcProb("pfc");
  pfcProb.initialize(INIT_ALL);

  // Adapt-Infos
  AdaptInfo adaptInfo("adapt", pfcProb.getNumComponents());
  RosenbrockAdaptInstationary adaptInstat("adapt", pfcProb.getProblem(), &adaptInfo, &pfcProb, &adaptInfo);
  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);
  }
  
  int error_code = adaptInstat.adapt(); 

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