PolarizationField.hh 5.98 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/******************************************************************************
 *
 * 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"

20
namespace AMDiS { namespace base_problems {
21

22
namespace detail {
23
24
25
26
27
28
29
30
31

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),
Praetorius, Simon's avatar
Praetorius, Simon committed
32
33
34
  C1(0.2),
  C4(0.0),
  epsilon(0.0),
35
36
37
38
39
40
41
42
43
44
  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);
  
Praetorius, Simon's avatar
Praetorius, Simon committed
45
46
47
  Parameters::get(self::name + "->C1", C1);
  Parameters::get(self::name + "->C4", C4);
  
48
  Parameters::get(self::name + "->epsilon", epsilon);
Praetorius, Simon's avatar
Praetorius, Simon committed
49
50
51
52
53
54
  if (epsilon > 0.0) {
    epsInv = 1.0/epsilon;
    C1 = -epsInv;
    C4 = epsInv;
  }
  
55
56
57
58
59
60
61
62
63
64
65
66
  
  Parameters::get(self::name + "->K", K);
}

template<typename P> 
PolarizationField<P>::~PolarizationField() 
{   
  if (vectorField != NULL) {
    delete vectorField;
    vectorField = NULL;
  }
  
67
  for (size_t i = 0; i < oldSolution.size(); i++) {
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    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)+")");
  
89
90
91
92
93
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
  for (size_t i = 0; i < self::dow; i++)
    Parallel::MeshDistributor::globalMeshDistributor->addInterchangeVector(oldSolution[i]);
#endif
    
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
  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);
124
    addZOT(opTime, 1.0);
125
126
127
128
129
130
131
132
133
    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);
134
135
    addSOT(opLaplace, alpha2);
    addZOT(opLaplace, alpha4);
136
137
138
139
140
141
142
    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);
143
    addZOT(opM, 1.0);
144
145
146
147
    self::prob->addMatrixOperator(*opM, i+self::dow, i+self::dow);
    
    /// < (-C1 - C4*P^2)*P , psi >
    Operator *opNonlin = new Operator(feSpace, feSpace);
148
    addZOT(opNonlin, (-C1 - C4 * unary_dot(valueOf(vectorField))) );
149
150
151
152
    self::prob->addMatrixOperator(*opNonlin, i+self::dow, i);
    
    for (size_t j = 0; j < self::dow; ++j) {
      Operator *opNonlin2 = new Operator(feSpace, feSpace);
Praetorius, Simon's avatar
Praetorius, Simon committed
153
      addZOT(opNonlin2, (-2.0*C4) * valueOf(vec[i]) * valueOf(vec[j]));
154
      self::prob->addMatrixOperator(*opNonlin2, i+self::dow, j);
155
156
157
    }
    
    Operator *opNonlin3 = new Operator(feSpace, feSpace);
158
    addZOT(opNonlin3, (-2.0*C4) * valueOf(vec[i]) * unary_dot(valueOf(vectorField)) );
159
160
161
162
163
164
    self::prob->addVectorOperator(*opNonlin3, i+self::dow);
  }
  
  fillLaplacian();

  Operator *opNull = new Operator(feSpace, feSpace);
165
  addZOT(opNull, 0.0);
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  
  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()
{
183
  const FiniteElemSpace* feSpace = self::getFeSpace(0);
184
185
186
  for (size_t i = 0; i < self::dow; ++i) {
    /// < -K*grad(P) , grad(psi) >
    Operator *opL = new Operator(feSpace, feSpace);
187
    addSOT(opL, -K);
188
189
190
191
192
193
194
195
    self::prob->addMatrixOperator(*opL, i+self::dow, i);
  }
}



template<typename P> 
void PolarizationField<P>::closeTimestep(AdaptInfo *adaptInfo)
196
{
197
198
199
200
201
202
203
204
205
206
  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)
207
{
208
209
210
211
  super::writeFiles(adaptInfo, force);
  self::fileWriter->writeFiles(adaptInfo, false);
}

212
213
214
} // end namespace detail

} }