-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy patheulerian_conv_diff.cpp
More file actions
419 lines (324 loc) · 18.3 KB
/
eulerian_conv_diff.cpp
File metadata and controls
419 lines (324 loc) · 18.3 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// KRATOS ___ ___ _ ___ __ ___ ___ ___ ___
// / __/ _ \| \| \ \ / /__| \_ _| __| __|
// | (_| (_) | .` |\ V /___| |) | || _|| _|
// \___\___/|_|\_| \_/ |___/___|_| |_| APPLICATION
//
// License: BSD License
// Kratos default license: kratos/license.txt
//
// Main authors: Riccardo Rossi
//
// System includes
// External includes
// Project includes
#include "includes/define.h"
#include "custom_elements/eulerian_conv_diff.h"
#include "convection_diffusion_application.h"
#include "includes/convection_diffusion_settings.h"
#include "utilities/math_utils.h"
#include "utilities/geometry_utilities.h"
namespace Kratos
{
//----------------------------------------------------------------------------------------
template< unsigned int TDim, unsigned int TNumNodes >
void EulerianConvectionDiffusionElement< TDim, TNumNodes >::EquationIdVector(EquationIdVectorType& rResult, const ProcessInfo& rCurrentProcessInfo) const
{
KRATOS_TRY
ConvectionDiffusionSettings::Pointer my_settings = rCurrentProcessInfo.GetValue(CONVECTION_DIFFUSION_SETTINGS);
const Variable<double>& rUnknownVar = my_settings->GetUnknownVariable();
if (rResult.size() != TNumNodes)
rResult.resize(TNumNodes, false);
for (unsigned int i = 0; i < TNumNodes; i++)
{
rResult[i] = GetGeometry()[i].GetDof(rUnknownVar).EquationId();
}
KRATOS_CATCH("")
}
//----------------------------------------------------------------------------------------
template< unsigned int TDim, unsigned int TNumNodes >
void EulerianConvectionDiffusionElement< TDim, TNumNodes >::GetDofList(DofsVectorType& ElementalDofList, const ProcessInfo& rCurrentProcessInfo) const
{
KRATOS_TRY
ConvectionDiffusionSettings::Pointer my_settings = rCurrentProcessInfo.GetValue(CONVECTION_DIFFUSION_SETTINGS);
const Variable<double>& rUnknownVar = my_settings->GetUnknownVariable();
if (ElementalDofList.size() != TNumNodes)
ElementalDofList.resize(TNumNodes);
for (unsigned int i = 0; i < TNumNodes; i++)
{
ElementalDofList[i] = GetGeometry()[i].pGetDof(rUnknownVar);
}
KRATOS_CATCH("")
}
//----------------------------------------------------------------------------------------
template< unsigned int TDim, unsigned int TNumNodes >
void EulerianConvectionDiffusionElement<TDim,TNumNodes>::CalculateLocalSystem(Matrix& rLeftHandSideMatrix,
Vector& rRightHandSideVector,
const ProcessInfo& rCurrentProcessInfo)
{
KRATOS_TRY
// Resize of the Left and Right Hand side
if (rLeftHandSideMatrix.size1() != TNumNodes)
rLeftHandSideMatrix.resize(TNumNodes, TNumNodes, false); //false says not to preserve existing storage!!
if (rRightHandSideVector.size() != TNumNodes)
rRightHandSideVector.resize(TNumNodes, false); //false says not to preserve existing storage!!
//Element variables
ElementVariables Variables;
this->InitializeEulerianElement(Variables,rCurrentProcessInfo);
// Compute the geometry
BoundedMatrix<double,TNumNodes, TDim> DN_DX;
array_1d<double,TNumNodes > N;
double Volume;
this-> CalculateGeometry(DN_DX,Volume);
// Getting the values of shape functions on Integration Points
BoundedMatrix<double,TNumNodes, TNumNodes> Ncontainer;
const GeometryType& Geom = this->GetGeometry();
Ncontainer = Geom.ShapeFunctionsValues( GeometryData::IntegrationMethod::GI_GAUSS_2 );
// Getting the values of Current Process Info and computing the value of h
this-> GetNodalValues(Variables,rCurrentProcessInfo);
double h = this->ComputeH(DN_DX);
array_1d<double,TDim> grad_phi_halfstep = prod(trans(DN_DX), 0.5*(Variables.phi+Variables.phi_old));
const double norm_grad = norm_2(grad_phi_halfstep);
//Computing the divergence
for (unsigned int i = 0; i < TNumNodes; i++)
{
for(unsigned int k=0; k<TDim; k++)
{
Variables.div_v += DN_DX(i,k)*(Variables.v[i][k]*Variables.theta + Variables.vold[i][k]*(1.0-Variables.theta));
}
}
//Some auxiliary definitions
BoundedMatrix<double,TNumNodes, TNumNodes> aux1 = ZeroMatrix(TNumNodes, TNumNodes); //terms multiplying dphi/dt
BoundedMatrix<double,TNumNodes, TNumNodes> aux2 = ZeroMatrix(TNumNodes, TNumNodes); //terms multiplying phi
bounded_matrix<double,TNumNodes, TDim> tmp;
// CrossWind variables
// Projected velocity
array_1d<double,TDim> u_proj;
// Diffusion contribution
BoundedMatrix<double,TDim,TDim> Dcw;
// Gauss points and Number of nodes coincides in this case.
for(unsigned int igauss=0; igauss<TNumNodes; igauss++)
{
noalias(N) = row(Ncontainer,igauss);
//obtain the velocity in the middle of the time step
array_1d<double, TDim > vel_gauss=ZeroVector(TDim);
for (unsigned int i = 0; i < TNumNodes; i++)
{
for(unsigned int k=0; k<TDim; k++)
vel_gauss[k] += N[i]*(Variables.v[i][k]*Variables.theta + Variables.vold[i][k]*(1.0-Variables.theta));
}
const double norm_vel = norm_2(vel_gauss);
array_1d<double, TNumNodes > a_dot_grad = prod(DN_DX, vel_gauss);
const double tau = this->CalculateTau(Variables,norm_vel,h);
//terms multiplying dphi/dt (aux1)
noalias(aux1) += (1.0+tau*Variables.beta*Variables.div_v)*outer_prod(N, N);
noalias(aux1) += tau*outer_prod(a_dot_grad, N);
//terms which multiply the gradient of phi
noalias(aux2) += (1.0+tau*Variables.beta*Variables.div_v)*outer_prod(N, a_dot_grad);
noalias(aux2) += tau*outer_prod(a_dot_grad, a_dot_grad);
// Crosswind term according to https://doi.org/10.1016/0045-7825(93)90213-H
const double norm_vel2 = norm_vel * norm_vel;
if(Variables.crosswind_constant > 0.0 && norm_grad > 1e-3 && norm_vel2 > 1e-9)
{
// Temporal derivative
const double phi_gauss = inner_prod(N, Variables.phi);
const double phi_old_gauss = inner_prod(N, Variables.phi_old);
const double dphi_dt = Variables.dt_inv * (phi_gauss - phi_old_gauss);
// Convective term
const double convection = inner_prod(vel_gauss, grad_phi_halfstep);
// Reaction term
const double reaction = Variables.beta * Variables.div_v * phi_gauss;
// Source termn
const double source = inner_prod(N, Variables.volumetric_source);
// Complete residual
const double residual = dphi_dt + convection + reaction - source;
// //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// // Peclet's projected formulation was commented after some testing due to
// // residual radial instabilities in 3D cases.
// // It was decided to use a complete approach with 'crosswind_constant' without
// // considering the projection.
// //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// // Velocity projected in the direction of the solution gradient
// u_proj = (convection/std::pow(norm_grad,2)) * grad_phi_halfstep;
// // Peclet number projected in the direction of the solution gradient
// const double Pe_proj = norm_2(u_proj) * h / (2.0 * Variables.conductivity + 1e-12);
// // Limiter
// const double alpha_c = std::max( 0.0, Variables.crosswind_constant - 1.0/(Pe_proj + 1e-12));
// // Discontinuity capturing coefficient
// double k_c = 0.5 * alpha_c * h * std::abs(residual / norm_grad);
// //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Discontinuity capturing coefficient
double k_c = Variables.crosswind_constant * h * std::abs(residual / norm_grad);
// Crosswind diffusion tensor
Dcw = k_c * IdentityMatrix(TDim);
// Removing diffusion in streamline direcction
const double k_streamline = tau * norm_vel2;
const double correction = std::max(k_c - k_streamline, 0.0) - k_c;
Dcw += (correction / norm_vel2) * outer_prod(vel_gauss, vel_gauss);
// Add diffusion contribution
noalias(tmp) = prod(DN_DX, Dcw);
noalias(aux2) += prod(tmp, trans(DN_DX));
}
}
//adding the second and third term in the formulation
noalias(rLeftHandSideMatrix) = (Variables.dt_inv*Variables.density*Variables.specific_heat + Variables.theta*Variables.beta*Variables.div_v)*aux1;
noalias(rRightHandSideVector) = (Variables.dt_inv*Variables.density*Variables.specific_heat - (1.0-Variables.theta)*Variables.beta*Variables.div_v)*prod(aux1,Variables.phi_old);
//adding the diffusion
noalias(rLeftHandSideMatrix) += (Variables.conductivity * Variables.theta * prod(DN_DX, trans(DN_DX)))*static_cast<double>(TNumNodes);
noalias(rRightHandSideVector) -= prod((Variables.conductivity * (1.0-Variables.theta) * prod(DN_DX, trans(DN_DX))),Variables.phi_old)*static_cast<double>(TNumNodes) ;
//terms in aux2
noalias(rLeftHandSideMatrix) += Variables.density*Variables.specific_heat*Variables.theta*aux2;
noalias(rRightHandSideVector) -= Variables.density*Variables.specific_heat*(1.0-Variables.theta)*prod(aux2,Variables.phi_old);
// volume source terms (affecting the RHS only)
noalias(rRightHandSideVector) += prod(aux1, Variables.volumetric_source);
//take out the dirichlet part to finish computing the residual
noalias(rRightHandSideVector) -= prod(rLeftHandSideMatrix, Variables.phi);
rRightHandSideVector *= Volume/static_cast<double>(TNumNodes);
rLeftHandSideMatrix *= Volume/static_cast<double>(TNumNodes);
KRATOS_CATCH("Error in Eulerian ConvDiff Element")
}
//----------------------------------------------------------------------------------------
template< unsigned int TDim, unsigned int TNumNodes >
void EulerianConvectionDiffusionElement<TDim,TNumNodes>::InitializeEulerianElement(ElementVariables& rVariables, const ProcessInfo& rCurrentProcessInfo)
{
KRATOS_TRY
rVariables.crosswind_constant = rCurrentProcessInfo[CROSS_WIND_STABILIZATION_FACTOR];
rVariables.theta = rCurrentProcessInfo[TIME_INTEGRATION_THETA]; //Variable defining the temporal scheme (0: Forward Euler, 1: Backward Euler, 0.5: Crank-Nicolson)
rVariables.dyn_st_beta = rCurrentProcessInfo[DYNAMIC_TAU];
const double delta_t = rCurrentProcessInfo[DELTA_TIME];
rVariables.dt_inv = 1.0 / delta_t;
rVariables.lumping_factor = 1.00 / double(TNumNodes);
rVariables.conductivity = 0.0;
rVariables.specific_heat = 0.0;
rVariables.density = 0.0;
rVariables.beta = 0.0;
rVariables.div_v = 0.0;
KRATOS_CATCH( "" )
}
//----------------------------------------------------------------------------------------
template< unsigned int TDim, unsigned int TNumNodes >
void EulerianConvectionDiffusionElement<TDim,TNumNodes>::CalculateGeometry(BoundedMatrix<double,TNumNodes,TDim>& rDN_DX, double& rVolume)
{
const GeometryType& Geom = this->GetGeometry();
// We select GI_GAUSS_1 due to we are computing at the barycenter.
const GeometryType::IntegrationPointsArrayType& integration_points = Geom.IntegrationPoints( GeometryData::IntegrationMethod::GI_GAUSS_1 );
const unsigned int NumGPoints = integration_points.size();
rVolume = Geom.Area();
GeometryType::ShapeFunctionsGradientsType DN_DXContainer(NumGPoints);
Geom.ShapeFunctionsIntegrationPointsGradients(DN_DXContainer,GeometryData::IntegrationMethod::GI_GAUSS_1);
noalias( rDN_DX ) = DN_DXContainer[0];
}
//----------------------------------------------------------------------------------------
template< unsigned int TDim, unsigned int TNumNodes >
double EulerianConvectionDiffusionElement< TDim, TNumNodes >::ComputeH(BoundedMatrix<double,TNumNodes,TDim >& DN_DX)
{
double h=0.0;
for(unsigned int i=0; i<TNumNodes; i++)
{
double h_inv = 0.0;
for(unsigned int k=0; k<TDim; k++)
{
h_inv += DN_DX(i,k)*DN_DX(i,k);
}
h += 1.0/h_inv;
}
h = sqrt(h)/static_cast<double>(TNumNodes);
return h;
}
//----------------------------------------------------------------------------------------
template< unsigned int TDim, unsigned int TNumNodes >
void EulerianConvectionDiffusionElement<TDim,TNumNodes>::GetNodalValues(ElementVariables& rVariables, const ProcessInfo& rCurrentProcessInfo) const
{
ConvectionDiffusionSettings::Pointer my_settings = rCurrentProcessInfo.GetValue(CONVECTION_DIFFUSION_SETTINGS);
//////storing locally the flags to avoid repeated check in the nodal loops
const bool IsDefinedVelocityVariable = my_settings->IsDefinedVelocityVariable();
const bool IsDefinedMeshVelocityVariable = my_settings->IsDefinedMeshVelocityVariable();
const bool IsDefinedDensityVariable = my_settings->IsDefinedDensityVariable();
const bool IsDefinedSpecificHeatVariableVariable = my_settings->IsDefinedSpecificHeatVariable();
const bool IsDefinedDiffusionVariable = my_settings->IsDefinedDiffusionVariable();
const bool IsDefinedVolumeSourceVariable = my_settings->IsDefinedVolumeSourceVariable();
const Variable<double>& rUnknownVar = my_settings->GetUnknownVariable();
for (unsigned int i = 0; i < TNumNodes; i++)
{
rVariables.phi[i] = GetGeometry()[i].FastGetSolutionStepValue(rUnknownVar);
rVariables.phi_old[i] = GetGeometry()[i].FastGetSolutionStepValue(rUnknownVar,1);
//dphi_dt[i] = dt_inv*(phi[i] - phi_old [i];
rVariables.v[i]=ZeroVector(3);
rVariables.vold[i]=ZeroVector(3);
rVariables.volumetric_source[i] = 0.0;
if (IsDefinedVelocityVariable)
{
const Variable<array_1d<double, 3 > >& rVelocityVar = my_settings->GetVelocityVariable();
rVariables.v[i] = GetGeometry()[i].FastGetSolutionStepValue(rVelocityVar);
rVariables.vold[i] = GetGeometry()[i].FastGetSolutionStepValue(rVelocityVar,1);
//active_convection=true;
}
if (IsDefinedMeshVelocityVariable)
{
const Variable<array_1d<double, 3 > >& rMeshVelocityVar = my_settings->GetMeshVelocityVariable();
rVariables.v[i] -= GetGeometry()[i].FastGetSolutionStepValue(rMeshVelocityVar);
rVariables.vold[i] -= GetGeometry()[i].FastGetSolutionStepValue(rMeshVelocityVar,1);
//active_convection=true;
}
if (IsDefinedDensityVariable)
{
const Variable<double>& rDensityVar = my_settings->GetDensityVariable();
rVariables.density += GetGeometry()[i].FastGetSolutionStepValue(rDensityVar);
}
else
rVariables.density += 1.0;
if (IsDefinedSpecificHeatVariableVariable)
{
const Variable<double>& rSpecificHeatVar = my_settings->GetSpecificHeatVariable();
rVariables.specific_heat += GetGeometry()[i].FastGetSolutionStepValue(rSpecificHeatVar);
}
else
rVariables.specific_heat += 1.0;
if (IsDefinedDiffusionVariable)
{
const Variable<double>& rDiffusionVar = my_settings->GetDiffusionVariable();
rVariables.conductivity += GetGeometry()[i].FastGetSolutionStepValue(rDiffusionVar);
}
//if not, then the conductivity = 0
if (IsDefinedVolumeSourceVariable)
{
const Variable<double>& rVolumeSourceVar = my_settings->GetVolumeSourceVariable();
rVariables.volumetric_source[i] += GetGeometry()[i].FastGetSolutionStepValue(rVolumeSourceVar);
}
}
//array_1d<double,TDim> grad_phi_halfstep = prod(trans(DN_DX), 0.5*(phi+phi_old));
//const double norm_grad = norm_2(grad_phi_halfstep);
rVariables.conductivity *= rVariables.lumping_factor;
rVariables.density *= rVariables.lumping_factor;
rVariables.specific_heat *= rVariables.lumping_factor;
}
template< unsigned int TDim, unsigned int TNumNodes >
double EulerianConvectionDiffusionElement<TDim,TNumNodes>::CalculateTau(const ElementVariables& rVariables, double norm_vel, double h)
{
// Dynamic part
double inv_tau = rVariables.dyn_st_beta * rVariables.dt_inv;
// Convection
inv_tau += 2.0 * norm_vel / h + rVariables.beta*rVariables.div_v;
// Dynamic and convection terms are multiplyied by density*specific_heat to have consistent dimensions
inv_tau *= rVariables.density * rVariables.specific_heat;
// Diffusion
inv_tau += 4.0 * rVariables.conductivity / (h*h);
// Limiting
inv_tau = std::max(inv_tau, 1e-2);
return (rVariables.density*rVariables.specific_heat) / inv_tau;
}
//----------------------------------------------------------------------------------------
template< unsigned int TDim, unsigned int TNumNodes >
void EulerianConvectionDiffusionElement< TDim, TNumNodes >::CalculateRightHandSide(
VectorType& rRightHandSideVector, const ProcessInfo& rCurrentProcessInfo)
{
Matrix LeftHandSide;
this->CalculateLocalSystem(LeftHandSide,rRightHandSideVector,rCurrentProcessInfo);
}
//----------------------------------------------------------------------------------------
template class EulerianConvectionDiffusionElement<2,3>;
template class EulerianConvectionDiffusionElement<2,4>;
template class EulerianConvectionDiffusionElement<3,4>;
template class EulerianConvectionDiffusionElement<3,8>;
}