-
Notifications
You must be signed in to change notification settings - Fork 9
/
div_ops.cxx
210 lines (152 loc) · 6.64 KB
/
div_ops.cxx
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
/*
Copyright B.Dudson, University of York, 2016
email: [email protected]
This file is part of SD1D.
SD1D is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SD1D is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SD1D. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mpi.h>
#include "div_ops.hxx"
#include <bout/mesh.hxx>
#include <globals.hxx>
#include <derivs.hxx>
#include <output.hxx>
#include <utils.hxx>
#include <bout/assert.hxx>
#include <cmath>
using bout::globals::mesh;
const Field3D Div_par_diffusion(const Field3D &K, const Field3D &f, bool bndry_flux) {
Field3D result;
result = 0.0;
Coordinates *coord = mesh->getCoordinates();
for(int i=mesh->xstart;i<=mesh->xend;i++)
for(int j=mesh->ystart-1;j<=mesh->yend;j++)
for(int k=0;k<mesh->LocalNz;k++) {
// Calculate flux at upper surface
if(!bndry_flux && !mesh->periodicY(i)) {
if((j == mesh->yend) && mesh->lastY(i))
continue;
if((j == mesh->ystart-1) && mesh->firstY(i))
continue;
}
BoutReal c = 0.5*(K(i,j,k) + K(i,j+1,k)); // K at the upper boundary
BoutReal J = 0.5*(coord->J(i,j) + coord->J(i,j+1)); // Jacobian at boundary
//if((i == mesh->xstart) && (j == mesh->ystart) && (k==0))
// output << "c = " << c << endl;
BoutReal g_22 = 0.5*(coord->g_22(i,j) + coord->g_22(i,j+1));
BoutReal gradient = 2.*(f(i,j+1,k) - f(i,j,k)) / (coord->dy(i,j) + coord->dy(i,j+1));
BoutReal flux = c * J * gradient / g_22;
result(i,j,k) += flux / (coord->dy(i,j) * coord->J(i,j));
result(i,j+1,k) -= flux / (coord->dy(i,j+1) * coord->J(i,j+1));
}
return result;
}
const Field3D Div_par_spitzer(BoutReal K0, const Field3D &Te, bool bndry_flux) {
Field3D result;
result = 0.0;
Coordinates *coord = mesh->getCoordinates();
for(int i=mesh->xstart;i<=mesh->xend;i++)
for(int j=mesh->ystart-1;j<=mesh->yend;j++)
for(int k=0;k<mesh->LocalNz;k++) {
// Calculate flux at upper surface
if(!bndry_flux && !mesh->periodicY(i)) {
if((j == mesh->yend) && mesh->lastY(i))
continue;
if((j == mesh->ystart-1) && mesh->firstY(i))
continue;
}
BoutReal Te0 = 0.5*(Te(i,j,k) + Te(i,j+1,k)); // Te at the upper boundary
BoutReal K = K0*pow(Te0,2.5);
BoutReal J = 0.5*(coord->J(i,j) + coord->J(i,j+1)); // Jacobian at boundary
BoutReal g_22 = 0.5*(coord->g_22(i,j) + coord->g_22(i,j+1));
BoutReal gradient = 2.*(Te(i,j+1,k) - Te(i,j,k)) / (coord->dy(i,j) + coord->dy(i,j+1));
BoutReal flux = K * J * gradient / g_22;
result(i,j,k) += flux / (coord->dy(i,j) * coord->J(i,j));
result(i,j+1,k) -= flux / (coord->dy(i,j+1) * coord->J(i,j+1));
}
return result;
}
const Field3D Div_par_diffusion_upwind(const Field3D &K, const Field3D &f, bool bndry_flux) {
Field3D result;
result = 0.0;
Coordinates *coord = mesh->getCoordinates();
for(int i=mesh->xstart;i<=mesh->xend;i++)
for(int j=mesh->ystart-1;j<=mesh->yend;j++)
for(int k=0;k<mesh->LocalNz;k++) {
// Calculate flux at upper surface
if(!bndry_flux && !mesh->periodicY(i)) {
if((j == mesh->yend) && mesh->lastY(i))
continue;
if((j == mesh->ystart-1) && mesh->firstY(i))
continue;
}
BoutReal J = 0.5*(coord->J(i,j) + coord->J(i,j+1)); // Jacobian at boundary
BoutReal g_22 = 0.5*(coord->g_22(i,j) + coord->g_22(i,j+1));
BoutReal gradient = 2.*(f(i,j+1,k) - f(i,j,k)) / (coord->dy(i,j) + coord->dy(i,j+1));
BoutReal c; // K at the upper boundary
if(gradient > 0.0) {
c = K(i,j+1,k);
}else {
c = K(i,j,k);
}
BoutReal flux = c * J * gradient / g_22;
result(i,j,k) += flux / (coord->dy(i,j) * coord->J(i,j));
result(i,j+1,k) -= flux / (coord->dy(i,j+1) * coord->J(i,j+1));
}
return result;
}
const Field3D Div_par_diffusion_index(const Field3D &f, bool bndry_flux) {
Field3D result;
result = 0.0;
Coordinates *coord = mesh->getCoordinates();
for(int i=mesh->xstart;i<=mesh->xend;i++)
for(int j=mesh->ystart-1;j<=mesh->yend;j++)
for(int k=0;k<mesh->LocalNz;k++) {
// Calculate flux at upper surface
if(!bndry_flux && !mesh->periodicY(i)) {
if((j == mesh->yend) && mesh->lastY(i))
continue;
if((j == mesh->ystart-1) && mesh->firstY(i))
continue;
}
BoutReal J = 0.5*(coord->J(i,j) + coord->J(i,j+1)); // Jacobian at boundary
BoutReal gradient = f(i,j+1,k) - f(i,j,k);
BoutReal flux = J * gradient;
result(i,j,k) += flux / coord->J(i,j);
result(i,j+1,k) -= flux / coord->J(i,j+1);
}
return result;
}
const Field3D AddedDissipation(const Field3D &N, const Field3D &P, const Field3D f, bool bndry_flux) {
Field3D result = 0.0;
Coordinates *coord = mesh->getCoordinates();
for(int i=mesh->xstart;i<=mesh->xend;i++)
for(int j=mesh->ystart-1;j<=mesh->yend;j++)
for(int k=0;k<mesh->LocalNz;k++) {
// Calculate flux at upper surface
if(!bndry_flux && !mesh->periodicY(i)) {
if((j >= mesh->yend-1) && mesh->lastY(i))
continue;
if((j <= mesh->ystart) && mesh->firstY(i))
continue;
}
// At upper boundary
BoutReal d = 0.5*(1./N(i,j,k) + 1./N(i,j+1,k));
// Velocity
BoutReal v = - 0.25*d*( (P(i,j-1,k) + P(i,j+1,k) - 2.*P(i,j,k)) - (P(i,j,k) + P(i,j+2,k)-2.*P(i,j+1,k)) );
// Variable being advected. Could use different interpolation?
BoutReal var = 0.5*(f(i,j,k) + f(i,j+1,k));
BoutReal flux = var * v * (coord->J(i,j) + coord->J(i,j+1)) / (sqrt(coord->g_22(i,j))+ sqrt(coord->g_22(i,j+1)));
result(i,j,k) -= flux / (coord->dy(i,j) * coord->J(i,j));
result(i,j+1,k) += flux / (coord->dy(i,j+1) * coord->J(i,j+1));
}
return result;
}