Finite Element Domain Decomposition Library
FEDDLib
Loading...
Searching...
No Matches
AssembleFENonLinLaplace_def.hpp
1#ifndef ASSEMBLEFENONLINLAPLACE_DEF_hpp
2#define ASSEMBLEFENONLINLAPLACE_DEF_hpp
3
4#include "feddlib/core/FEDDCore.hpp"
5#include <Teuchos_Array.hpp>
6#include <Teuchos_ScalarTraitsDecl.hpp>
7#include <cmath>
8
9namespace FEDD {
10
11void rhsFunc(double *x, double *res, double *parameters) {
12 res[0] = 1; // x[0] * sin(x[1]);
13}
14
23template <class SC, class LO, class GO, class NO>
25 int flag, vec2D_dbl_Type nodesRefConfig, ParameterListPtr_Type params,
26 tuple_disk_vec_ptr_Type tuple)
27 : AssembleFE<SC, LO, GO, NO>(flag, nodesRefConfig, params, tuple) {
28
29 this->FEType_ = std::get<1>(this->diskTuple_->at(0));
30 this->dofs_ = std::get<2>(this->diskTuple_->at(0));
31 // Same as this->getNodesRefConfig().size();
32 this->numNodes_ = std::get<3>(this->diskTuple_->at(0));
33 this->dofsElement_ = this->numNodes_ * this->dofs_;
34 // Specifying rhs func here for now
35 // Should be done in main when possible
36 this->rhsFunc_ = rhsFunc;
37}
38
43
44template <class SC, class LO, class GO, class NO>
46
47 SmallMatrixPtr_Type elementMatrix =
48 Teuchos::rcp(new SmallMatrix_Type(this->dofsElement_));
49 assemblyNonLinLaplacian(elementMatrix);
50
51 this->jacobian_ = elementMatrix;
52}
53
60template <class SC, class LO, class GO, class NO>
61void AssembleFENonLinLaplace<SC, LO, GO, NO>::assemblyNonLinLaplacian(
62 SmallMatrixPtr_Type &elementMatrix) {
63
64 int dim = this->getDim();
65 UN deg = 2*Helper::determineDegree(dim, this->FEType_,Helper::Deriv1); // TODO: [JK] 2025/04 Please check if degree is really sufficient. What about u_0^2?
66
67 vec3D_dbl_ptr_Type dPhi;
68 vec2D_dbl_ptr_Type phi;
69 vec_dbl_ptr_Type weights = Teuchos::rcp(new vec_dbl_Type(0));
70
71 // Get values of nodal basis at the quadrature nodes
72 Helper::getDPhi(dPhi, weights, dim, this->FEType_, deg);
73 Helper::getPhi(phi, weights, dim, this->FEType_, deg);
74
75 // Built affine transformation from the reference element to the current
76 // element
77 SC detB;
78 SC absDetB;
79 SmallMatrix<SC> B(dim);
80 SmallMatrix<SC> Binv(dim);
81 buildTransformation(B);
82 detB = B.computeInverse(Binv);
83 absDetB = std::fabs(detB);
84 vec3D_dbl_Type dPhiTrans(
85 dPhi->size(),
86 vec2D_dbl_Type(dPhi->at(0).size(), vec_dbl_Type(dim, 0.)));
87 // Apple transformation to gradients of basis functions
88 applyBTinv(dPhi, dPhiTrans, Binv);
89
90 vec_dbl_Type uLoc(weights->size(), 0.);
91 // At each quad node p_i gradient vector is duLoc[w] = [\partial_x phi,
92 // \partial_y phi]
93 vec2D_dbl_Type duLoc(weights->size(), vec_dbl_Type(dim, 0));
94
95 // Build vector of current solution at quadrature nodes
96 // Iterate over quadrature nodes
97 for (int w = 0; w < phi->size(); w++) {
98 // Iterate over each basis function at the current quadrature node
99 for (int i = 0; i < phi->at(0).size(); i++) {
100 uLoc[w] += (*this->solution_)[i] * phi->at(w).at(i);
101 for (int d = 0; d < dim; d++) {
102 duLoc[w][d] += (*this->solution_)[i] * dPhiTrans[w][i][d];
103 }
104 }
105 }
106
107 // Build local stiffness matrix
108 auto value = Teuchos::ScalarTraits<SC>::zero();
109 for (UN i = 0; i < this->numNodes_; i++) {
110 for (UN j = 0; j < this->numNodes_; j++) {
111 value = 0.;
112 for (UN w = 0; w < weights->size(); w++) {
113 for (UN d = 0; d < dim; d++) {
114 value += weights->at(w) *
115 ((1 + uLoc[w] * uLoc[w]) * dPhiTrans[w][i][d] +
116 2 * uLoc[w] * phi->at(w).at(j) * duLoc[w][d]) *
117 dPhiTrans[w][j][d];
118 }
119 }
120 value *= absDetB;
121 (*elementMatrix)[i][j] = value;
122 }
123 }
124}
125
132template <class SC, class LO, class GO, class NO>
134
135 int dim = this->getDim();
136 UN deg = 2*Helper::determineDegree(dim, this->FEType_, Helper::Deriv1); // TODO: [JK] 2025/04 Please check if degree is really sufficient. What about u_0^2?
137
138 vec3D_dbl_ptr_Type dPhi;
139 vec2D_dbl_ptr_Type phi;
140 vec_dbl_ptr_Type weights = Teuchos::rcp(new vec_dbl_Type(0));
141
142 Helper::getDPhi(dPhi, weights, dim, this->FEType_, deg);
143 Helper::getPhi(phi, weights, dim, this->FEType_, deg);
144
145 SC detB;
146 SC absDetB;
147 SmallMatrix<SC> B(dim);
148 SmallMatrix<SC> Binv(dim);
149 buildTransformation(B);
150 detB = B.computeInverse(Binv);
151 absDetB = std::fabs(detB);
152 vec3D_dbl_Type dPhiTrans(
153 weights->size(),
154 vec2D_dbl_Type(dPhi->at(0).size(), vec_dbl_Type(dim, 0.)));
155 applyBTinv(dPhi, dPhiTrans, Binv);
156 vec_dbl_Type uLoc(weights->size(), 0.);
157 vec_dbl_ptr_Type funcValues;
158 Helper::getFuncAtQuadNodes(funcValues, this->rhsFunc_, dim, this->FEType_,
159 deg);
160
161 // Build vector of current solution at quadrature nodes
162 for (int w = 0; w < phi->size(); w++) { // quadrature nodes
163 for (int i = 0; i < phi->at(0).size();
164 i++) { // each basis function at the current quadrature node
165 uLoc[w] += (*this->solution_)[i] * phi->at(w).at(i);
166 }
167 }
168
169 this->rhsVec_.reset(new vec_dbl_Type(this->dofsElement_, 0.));
170 // $fv$ term
171 auto valueOne = Teuchos::ScalarTraits<SC>::zero();
172 // $(1 + u_0^2)\nabla u_0 \nabla v$ term
173 auto valueTwo = Teuchos::ScalarTraits<SC>::zero();
174 // for storing matrix_column \cdot current_solution reduction operation
175 auto reductionValue = Teuchos::ScalarTraits<SC>::zero();
176 // Build local stiffness matrx
177 for (UN i = 0; i < this->numNodes_; i++) {
178 reductionValue = 0.;
179 // Iterate test functions (rows)
180 for (UN j = 0; j < this->numNodes_; j++) {
181 valueTwo = 0.;
182 // Iterate quadrature nodes
183 for (UN w = 0; w < dPhiTrans.size(); w++) {
184 for (UN d = 0; d < dim; d++) {
185 valueTwo += weights->at(w) * (1 + uLoc[w] * uLoc[w]) *
186 dPhiTrans[w][i][d] * dPhiTrans[w][j][d];
187 }
188 }
189 // Perform reduction of current valueTwo and solution
190 reductionValue += valueTwo * (*this->solution_)[j];
191 }
192 reductionValue *= absDetB;
193 valueOne = 0.;
194 for (UN w = 0; w < dPhiTrans.size(); w++) {
195 // Note that phi does not require transformation. absDetB suffices.
196 valueOne += weights->at(w) * funcValues->at(w) * phi->at(w).at(i);
197 }
198 valueOne *= absDetB;
199 (*this->rhsVec_)[i] = -(valueOne - reductionValue);
200 }
201}
202
207
208template <class SC, class LO, class GO, class NO>
209void AssembleFENonLinLaplace<SC, LO, GO, NO>::buildTransformation(
210 SmallMatrix<SC> &B) {
211
212 TEUCHOS_TEST_FOR_EXCEPTION((B.size() < 2 || B.size() > 3), std::logic_error,
213 "Initialize SmallMatrix for transformation.");
214 UN index;
215 UN index0 = 0;
216 for (UN j = 0; j < B.size(); j++) {
217 index = j + 1;
218 for (UN i = 0; i < B.size(); i++) {
219 // name nodesRefConfig_ is deceptive: actually holds coords of nodes
220 // of current element
221 B[i][j] = this->nodesRefConfig_.at(index).at(i) -
222 this->nodesRefConfig_.at(index0).at(i);
223 }
224 }
225}
226
233template <class SC, class LO, class GO, class NO>
234void AssembleFENonLinLaplace<SC, LO, GO, NO>::applyBTinv(
235 vec3D_dbl_ptr_Type &dPhiIn, vec3D_dbl_Type &dPhiOut,
236 SmallMatrix<SC> &Binv) {
237 UN dim = Binv.size();
238 for (UN w = 0; w < dPhiIn->size(); w++) {
239 for (UN i = 0; i < dPhiIn->at(w).size(); i++) {
240 for (UN d1 = 0; d1 < dim; d1++) {
241 for (UN d2 = 0; d2 < dim; d2++) {
242 dPhiOut[w][i][d1] +=
243 dPhiIn->at(w).at(i).at(d2) * Binv[d2][d1];
244 }
245 }
246 }
247 }
248}
249
250} // namespace FEDD
251#endif
void assembleRHS() override
Assemble the element right hand side vector.
Definition AssembleFENonLinLaplace_def.hpp:133
void assembleJacobian() override
Assemble the element Jacobian matrix.
Definition AssembleFENonLinLaplace_def.hpp:45
AssembleFENonLinLaplace(int flag, vec2D_dbl_Type nodesRefConfig, ParameterListPtr_Type parameters, tuple_disk_vec_ptr_Type tuple)
Constructor for AssembleFENonLinLaplace.
Definition AssembleFENonLinLaplace_def.hpp:24
AssembleFE(int flag, vec2D_dbl_Type nodesRefConfig, ParameterListPtr_Type parameters, tuple_disk_vec_ptr_Type tuple)
Definition AssembleFE_def.hpp:8
static UN determineDegree(UN dim, std::string FEType, VarType orderOfDerivative)
Determine polynomial degree of a finite element basis function or its gradient that is required to se...
Definition Helper.cpp:68
@ Deriv1
order 1, gradient(f(x))
Definition Helper.hpp:28
static int getPhi(vec2D_dbl_ptr_Type &Phi, vec_dbl_ptr_Type &weightsPhi, int dim, std::string FEType, int Degree, std::string FETypeQuadPoints="")
Get basisfunction phi per quadrature point.
Definition Helper.cpp:921
static int getDPhi(vec3D_dbl_ptr_Type &DPhi, vec_dbl_ptr_Type &weightsDPhi, int Dimension, std::string FEType, int Degree)
Full matrix representation of gradient of a basis function for each quadrature point.
Definition Helper.cpp:215
This class represents a templated small Matrix of type T. Primarily created for 2x2 and 3x3 matrices....
Definition SmallMatrix.hpp:20
Adaptive Mesh Refinement.
Definition AdaptiveMeshRefinement_decl.hpp:36