PolarizationField.hh 5.72 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
/******************************************************************************
 *
 * Extension of AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: Simon Praetorius et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/

#include "Helpers.h"
#include "POperators.h"

namespace detail {

using namespace AMDiS;

template<typename P> 
PolarizationField<P>::PolarizationField(const std::string &name_) :
  super(name_, true),
  vectorField(NULL),
  oldTimestep(0.0),
  minus1(-1.0),
  alpha2(1.0),
  alpha4(1.0),
  epsilon(0.1),
  K(1.0),
  fileWriter(NULL)
{
  oldSolution.resize(self::dow);
  for (size_t i = 0; i < self::dow; i++)
    oldSolution[i] = NULL;
  
  Parameters::get(self::name + "->alpha2", alpha2);
  Parameters::get(self::name + "->alpha4", alpha4);
  
  Parameters::get(self::name + "->epsilon", epsilon);
  epsInv = 1.0/epsilon;
  
  Parameters::get(self::name + "->K", K);
}

template<typename P> 
PolarizationField<P>::~PolarizationField() 
{   
  if (vectorField != NULL) {
    delete vectorField;
    vectorField = NULL;
  }
  
  for (size_t i = 0; i < self::dow; i++) {
    if (oldSolution[i] != NULL)
      delete oldSolution[i];
    oldSolution[i] = NULL;
  }
 
  if (fileWriter) { 
    delete fileWriter;
    fileWriter = NULL;
  }
}


template<typename P> 
void PolarizationField<P>::initData()
{ 
  if (vectorField == NULL)
    vectorField = new DOFVector<WorldVector<double> >(self::getFeSpace(0), "vectorField");
  
  for (size_t i = 0; i < self::dow; i++)
    oldSolution[i] = new DOFVector<double>(self::getFeSpace(i), "old(v_"+Helpers::toString(i)+")");
  
  fileWriter = new FileVectorWriter(self::name + "->vectorField->output", self::getFeSpace()->getMesh(), vectorField);

  super::initData();
}


template<typename P> 
void PolarizationField<P>::transferInitialSolution(AdaptInfo *adaptInfo)
{ 
  calcVectorField();
  for (size_t i = 0; i < self::dow; i++)
    oldSolution[i]->copy(*self::prob->getSolution()->getDOFVector(i));
  
  super::transferInitialSolution(adaptInfo);
}


template<typename P> 
void PolarizationField<P>::fillOperators()
{ 
  WorldVector<DOFVector<double>* > vec;
  for (size_t k = 0; k < self::dow; k++)
    vec[k] = self::prob->getSolution()->getDOFVector(k);
  
  const FiniteElemSpace* feSpace = self::getFeSpace(0);
  
  // fill operators for component P
  for (size_t i = 0; i < self::dow; ++i) {
    /// < (1/tau)*P_i , psi >
    Operator *opTime = new Operator(feSpace, feSpace);
    addZOT(opTime, constant(1.0));
    self::prob->addMatrixOperator(*opTime, i, i, self::getInvTau(), self::getInvTau());
    
    /// < (1/tau)*P_i^old , psi >
    Operator *opTimeOld = new Operator(feSpace, feSpace);
    addZOT(opTimeOld, valueOf(getOldSolution(i)));
    self::prob->addVectorOperator(*opTimeOld, i, self::getInvTau(), self::getInvTau());

    /// Diffusion-Operator
    Operator *opLaplace = new Operator(feSpace, feSpace);
    addSOT(opLaplace, constant(alpha2));
    addZOT(opLaplace, constant(alpha4));
    self::prob->addMatrixOperator(*opLaplace, i, i+self::dow);
  }

  // fill operators for component P#
  for (size_t i = 0; i < self::dow; ++i) {
    /// < P# , psi >
    Operator *opM = new Operator(feSpace, feSpace);
    addZOT(opM, constant(1.0));
    self::prob->addMatrixOperator(*opM, i+self::dow, i+self::dow);
    
    /// < (-C1 - C4*P^2)*P , psi >
    Operator *opNonlin = new Operator(feSpace, feSpace);
    addZOT(opNonlin, epsInv * (1.0 - pow<2>(valueOf(vectorField))) );
    self::prob->addMatrixOperator(*opNonlin, i+self::dow, i);
    
    for (size_t j = 0; j < self::dow; ++j) {
      Operator *opNonlin2 = new Operator(feSpace, feSpace);
      addZOT(opNonlin2, (-2.0*epsInv) * valueOf(vec[i]) * valueOf(vec[j]));
      self::prob->addMatrixOperator(*opNonlin, i+self::dow, j);
    }
    
    Operator *opNonlin3 = new Operator(feSpace, feSpace);
    addZOT(opNonlin3, (-2.0*epsInv) * valueOf(vec[i]) * pow<2>(valueOf(vectorField)) );
    self::prob->addVectorOperator(*opNonlin3, i+self::dow);
  }
  
  fillLaplacian();

  Operator *opNull = new Operator(feSpace, feSpace);
  addZOT(opNull, constant(0.0));
  
  for (size_t i = 0; i < self::dow; ++i) {
    for (size_t j = i+1; j < self::dow; ++j) {
      self::prob->addMatrixOperator(*opNull, i, j);
      self::prob->addMatrixOperator(*opNull, j, i);
      self::prob->addMatrixOperator(*opNull, i, j+self::dow);
      self::prob->addMatrixOperator(*opNull, j, i+self::dow);
      self::prob->addMatrixOperator(*opNull, i+self::dow, j+self::dow);
      self::prob->addMatrixOperator(*opNull, j+self::dow, i+self::dow);
    }
  }
}


template<typename P> 
void PolarizationField<P>::fillLaplacian()
{
  for (size_t i = 0; i < self::dow; ++i) {
    /// < -K*grad(P) , grad(psi) >
    Operator *opL = new Operator(feSpace, feSpace);
    addSOT(opL, constant(-K));
    self::prob->addMatrixOperator(*opL, i+self::dow, i);
  }
}



template<typename P> 
void PolarizationField<P>::closeTimestep(AdaptInfo *adaptInfo)
{ FUNCNAME("PolarizationField::closeTimestep()");

  calcVectorField();
  for (size_t i = 0; i < self::dow; i++)
    oldSolution[i]->copy(*self::prob->getSolution()->getDOFVector(i));
  
  super::closeTimestep(adaptInfo);
}


template<typename P> 
void PolarizationField<P>::writeFiles(AdaptInfo *adaptInfo, bool force)
{ FUNCNAME("PolarizationField::closeTimestep()");

  super::writeFiles(adaptInfo, force);
  self::fileWriter->writeFiles(adaptInfo, false);
}

} // end namespace detail