forked from llodds/ecsg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_ghost_syz.cpp
242 lines (222 loc) · 12.5 KB
/
update_ghost_syz.cpp
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
#include "sim_utils.hpp"
#include <cassert>
#include <omp.h>
//assume radius=1, H=2h
void UpdateGhostSYZ(Field *sim1, Field *sim2,
Field *buoy1, Field *buoy2,
real **coeff1, real **coeff2,
int mode)
{
Field *syy1 = sim1+4, *syy2 = sim2+4;
Field *syz1 = sim1+6, *syz2 = sim2+6;
Field *sxy1 = sim1+8, *sxy2 = sim2+8;
Field *vy1 = sim1+1, *vy2 = sim2+1;
int nz1 = buoy1->GetEnd(2);
assert(mode==0 || mode==1);
//mode =
// 0: 2nd order accurate intuitive (energy non-conserving) interpolation
// 1: energy conserving interpolation
if(mode==0)
{
#ifdef _OPENMP
#pragma omp parallel default(shared)
#endif
{
//second order accurate approximation to ghost syz2
#ifdef _OPENMP
#pragma omp for
#endif
for(int i1 = syz2->GetStart(1); i1 <= syz2->GetEnd(1); i1++)
for(int i0 = syz2->GetStart(0); i0 <= syz2->GetEnd(0); i0++)
syz2->SetVal(i0,i1,-1) = 0.125*(syz1->GetVal(2*i0,2*i1,nz1-2)
+ syz1->GetVal(2*i0+1,2*i1,nz1-2)
+ syz1->GetVal(2*i0,2*i1+1,nz1-2)
+ syz1->GetVal(2*i0+1,2*i1+1,nz1-2)
+ syz1->GetVal(2*i0,2*i1,nz1-1)
+ syz1->GetVal(2*i0+1,2*i1,nz1-1)
+ syz1->GetVal(2*i0,2*i1+1,nz1-1)
+ syz1->GetVal(2*i0+1,2*i1+1,nz1-1));
//second order accurate approximation to ghost syz1
#ifdef _OPENMP
#pragma omp for
#endif
for(int i1 = syz2->GetStart(1); i1 < syz2->GetEnd(1); i1++)
for(int i0 = syz2->GetStart(0); i0 < syz2->GetEnd(0); i0++)
{
syz1->SetVal(2*i0+1,2*i1+1,nz1) = syz1->GetVal(2*i0+1,2*i1+1,nz1-1)/3
+ 0.375*syz2->GetVal(i0,i1,0)
+ 0.125*syz2->GetVal(i0+1,i1,0)
+ 0.125*syz2->GetVal(i0,i1+1,0)
+ syz2->GetVal(i0+1,i1+1,0)/24;
syz1->SetVal(2*i0+2,2*i1+1,nz1) = syz1->GetVal(2*i0+2,2*i1+1,nz1-1)/3
+ 0.125*syz2->GetVal(i0,i1,0)
+ 0.375*syz2->GetVal(i0+1,i1,0)
+ syz2->GetVal(i0,i1+1,0)/24
+ 0.125*syz2->GetVal(i0+1,i1+1,0);
syz1->SetVal(2*i0+1,2*i1+2,nz1) = syz1->GetVal(2*i0+1,2*i1+2,nz1-1)/3
+ 0.125*syz2->GetVal(i0,i1,0)
+ syz2->GetVal(i0+1,i1,0)/24
+ 0.375*syz2->GetVal(i0,i1+1,0)
+ 0.125*syz2->GetVal(i0+1,i1+1,0);
syz1->SetVal(2*i0+2,2*i1+2,nz1) = syz1->GetVal(2*i0+2,2*i1+2,nz1-1)/3
+ syz2->GetVal(i0,i1,0)/24
+ 0.125*syz2->GetVal(i0+1,i1,0)
+ 0.125*syz2->GetVal(i0,i1+1,0)
+ 0.375*syz2->GetVal(i0+1,i1+1,0);
}
// option1: use extrapolation to update boundary points to ensure second order accuracy
#ifdef _OPENMP
#pragma omp for
#endif
for(int i0 = syz1->GetStart(0); i0 <= syz1->GetEnd(0); i0++) //update top and bottom edges
{
if(i0 == syz1->GetStart(0))
{
syz1->SetVal(i0,syz1->GetStart(1),nz1) = syz1->GetVal(i0,syz1->GetStart(1),nz1-1)
- syz1->GetVal(i0+1,syz1->GetStart(1),nz1-1)/3
- syz1->GetVal(i0,syz1->GetStart(1)+1,nz1-1)/3
+ syz2->GetVal(i0/2,syz1->GetStart(1)/2,0)*2/3;
syz1->SetVal(i0,syz1->GetEnd(1),nz1) = syz1->GetVal(i0,syz1->GetEnd(1),nz1-1)
- syz1->GetVal(i0+1,syz1->GetEnd(1),nz1-1)/3
- syz1->GetVal(i0,syz1->GetEnd(1)-1,nz1-1)/3
+ syz2->GetVal(i0/2,syz1->GetEnd(1)/2,0)*2/3;
}else if(i0 == syz1->GetEnd(0))
{
syz1->SetVal(i0,syz1->GetStart(1),nz1) = syz1->GetVal(i0,syz1->GetStart(1),nz1-1)
- syz1->GetVal(i0-1,syz1->GetStart(1),nz1-1)/3
- syz1->GetVal(i0,syz1->GetStart(1)+1,nz1-1)/3
+ syz2->GetVal(i0/2,syz1->GetStart(1)/2,0)*2/3;
syz1->SetVal(i0,syz1->GetEnd(1),nz1) = syz1->GetVal(i0,syz1->GetEnd(1),nz1-1)
- syz1->GetVal(i0-1,syz1->GetEnd(1),nz1-1)/3
- syz1->GetVal(i0,syz1->GetEnd(1)-1,nz1-1)/3
+ syz2->GetVal(i0/2,syz1->GetEnd(1)/2,0)*2/3;
}else
{
syz1->SetVal(i0,syz1->GetStart(1),nz1) = syz1->GetVal(i0,syz1->GetStart(1),nz1-1)/3
+ syz1->GetVal(i0-1+2*(i0%2),syz1->GetStart(1),nz1-1)/3
- syz1->GetVal(i0,syz1->GetStart(1)+1,nz1-1)/3
+ syz2->GetVal(i0/2,syz1->GetStart(1)/2,0)*2/3;
syz1->SetVal(i0,syz1->GetEnd(1),nz1) = syz1->GetVal(i0,syz1->GetEnd(1),nz1-1)/3
+ syz1->GetVal(i0-1+2*(i0%2),syz1->GetEnd(1),nz1-1)/3
- syz1->GetVal(i0,syz1->GetEnd(1)-1,nz1-1)/3
+ syz2->GetVal(i0/2,syz1->GetEnd(1)/2,0)*2/3;
}
}
#ifdef _OPENMP
#pragma omp for
#endif
for(int i1 = syz1->GetStart(1)+1; i1 < syz1->GetEnd(1); i1++) //update left and right edges
{
syz1->SetVal(syz1->GetStart(0),i1,nz1) = syz1->GetVal(syz1->GetStart(0),i1,nz1-1)/3
+ syz1->GetVal(syz1->GetStart(0),i1-1+2*(i1%2),nz1-1)/3
- syz1->GetVal(syz1->GetStart(0)+1,i1,nz1-1)/3
+ syz2->GetVal(syz1->GetStart(0)/2,i1/2,0)*2/3;
syz1->SetVal(syz1->GetEnd(0),i1,nz1) = syz1->GetVal(syz1->GetEnd(0),i1,nz1-1)/3
+ syz1->GetVal(syz1->GetEnd(0),i1-1+2*(i1%2),nz1-1)/3
- syz1->GetVal(syz1->GetEnd(0)-1,i1,nz1-1)/3
+ syz2->GetVal(syz1->GetEnd(0)/2,i1/2,0)*2/3;
}
// option 2: do not use extrapolation near the boundary
//#ifdef _OPENMP
//#pragma omp for
//#endif
// for(int i0 = syz1->GetStart(0); i0 <= syz1->GetEnd(0); i0++)
// {
// syz1->SetVal(i0,syz1->GetStart(1),nz1) = syz1->GetVal(i0,syz1->GetStart(1),nz1-1)/3 + 2*syz2->GetVal(i0/2,syz1->GetStart(1)/2,0)/3;
// syz1->SetVal(i0,syz1->GetEnd(1),nz1) = syz1->GetVal(i0,syz1->GetEnd(1),nz1-1)/3 + 2*syz2->GetVal(i0/2,syz1->GetEnd(1)/2,0)/3;
// }
//#ifdef _OPENMP
//#pragma omp for
//#endif
// for(int i1 = syz1->GetStart(1)+1; i1 < syz1->GetEnd(1); i1++)
// for(int i0 = syz1->GetStart(0); i0 <= syz1->GetEnd(0); i0 += syz1->GetEnd(0)-syz1->GetStart(0))
// syz1->SetVal(i0,i1,nz1) = syz1->GetVal(i0,i1,nz1-1)/3 + 2*syz2->GetVal(i0/2,i1/2,0)/3;
}
}else if(mode==1)
{
#ifdef _OPENMP
#pragma omp parallel default(shared)
#endif
{
//TODO vectorize!!!
//compute ghost syz2
#ifdef _OPENMP
#pragma omp for
#endif
for(int i1 = syz2->GetStart(1); i1 <= syz2->GetEnd(1); i1++)
for(int i0 = syz2->GetStart(0); i0 <= syz2->GetEnd(0); i0++)
{
real c0 = 0.25*(buoy2->GetVal(i0,i1,0)
+ buoy2->GetVal(i0+1,i1,0)
+ buoy2->GetVal(i0,i1+1,0)
+ buoy2->GetVal(i0+1,i1+1,0));
real c1 = 0.25*(buoy1->GetVal(2*i0,2*i1,nz1)
+ buoy1->GetVal(2*i0+1,2*i1,nz1)
+ buoy1->GetVal(2*i0,2*i1+1,nz1)
+ buoy1->GetVal(2*i0+1,2*i1+1,nz1));
real c2 = 0.25*(buoy1->GetVal(2*i0+1,2*i1,nz1)
+ buoy1->GetVal(2*i0+2,2*i1,nz1)
+ buoy1->GetVal(2*i0+1,2*i1+1,nz1)
+ buoy1->GetVal(2*i0+2,2*i1+1,nz1));
real c3 = 0.25*(buoy1->GetVal(2*i0,2*i1+1,nz1)
+ buoy1->GetVal(2*i0+1,2*i1+1,nz1)
+ buoy1->GetVal(2*i0,2*i1+2,nz1)
+ buoy1->GetVal(2*i0+1,2*i1+2,nz1));
real c4 = 0.25*(buoy1->GetVal(2*i0+1,2*i1+1,nz1)
+ buoy1->GetVal(2*i0+2,2*i1+1,nz1)
+ buoy1->GetVal(2*i0+1,2*i1+2,nz1)
+ buoy1->GetVal(2*i0+2,2*i1+2,nz1));
syz2->SetVal(i0,i1,-1) = 4*(vy2->GetVal(i0,i1,0)
+ c0*(coeff2[0][0]*(sxy2->GetVal(i0+1,i1,0)
-sxy2->GetVal(i0,i1,0))
+ coeff2[1][0]*(syy2->GetVal(i0,i1+1,0)
-syy2->GetVal(i0,i1,0))
+ coeff2[2][0]*syz2->GetVal(i0,i1,0)))
- (vy1->GetVal(2*i0,2*i1,nz1)
+ c1*(coeff1[0][0]*(sxy1->GetVal(2*i0+1,2*i1,nz1)
-sxy1->GetVal(2*i0,2*i1,nz1))
+ coeff1[1][0]*(syy1->GetVal(2*i0,2*i1+1,nz1)
-syy1->GetVal(2*i0,2*i1,nz1))
+ coeff1[2][0]*(syz2->GetVal(i0,i1,0)
- 2*syz1->GetVal(2*i0,2*i1,nz1-1))))
- (vy1->GetVal(2*i0+1,2*i1,nz1)
+ c2*(coeff1[0][0]*(sxy1->GetVal(2*i0+2,2*i1,nz1)
- sxy1->GetVal(2*i0+1,2*i1,nz1))
+ coeff1[1][0]*(syy1->GetVal(2*i0+1,2*i1+1,nz1)
- syy1->GetVal(2*i0+1,2*i1,nz1))
+ coeff1[2][0]*(syz2->GetVal(i0,i1,0)
- 2*syz1->GetVal(2*i0+1,2*i1,nz1-1))))
- (vy1->GetVal(2*i0,2*i1+1,nz1)
+ c3*(coeff1[0][0]*(sxy1->GetVal(2*i0+1,2*i1+1,nz1)
- sxy1->GetVal(2*i0,2*i1+1,nz1))
+ coeff1[1][0]*(syy1->GetVal(2*i0,2*i1+2,nz1)
- syy1->GetVal(2*i0,2*i1+1,nz1))
+ coeff1[2][0]*(syz2->GetVal(i0,i1,0)
- 2*syz1->GetVal(2*i0,2*i1+1,nz1-1))))
- (vy1->GetVal(2*i0+1,2*i1+1,nz1)
+ c4*(coeff1[0][0]*(sxy1->GetVal(2*i0+2,2*i1+1,nz1)
- sxy1->GetVal(2*i0+1,2*i1+1,nz1))
+ coeff1[1][0]*(syy1->GetVal(2*i0+1,2*i1+2,nz1)
- syy1->GetVal(2*i0+1,2*i1+1,nz1))
+ coeff1[2][0]*(syz2->GetVal(i0,i1,0)
- 2*syz1->GetVal(2*i0+1,2*i1+1,nz1-1))));
syz2->SetVal(i0,i1,-1) /= (4*c0*coeff2[2][0] + c1*coeff1[2][0]
+ c2*coeff1[2][0] + c3*coeff1[2][0]
+ c4*coeff1[2][0]);
}
//compute ghost syz1
#ifdef _OPENMP
#pragma omp for
#endif
for(int i1 = syz2->GetStart(1); i1 <= syz2->GetEnd(1); i1++)
for(int i0 = syz2->GetStart(0); i0 <= syz2->GetEnd(0); i0++)
{
real tmp = syz2->GetVal(i0,i1,-1) + syz2->GetVal(i0,i1,0);
syz1->SetVal(2*i0,2*i1,nz1) = -syz1->GetVal(2*i0,2*i1,nz1-1) + tmp;
syz1->SetVal(2*i0+1,2*i1,nz1) = -syz1->GetVal(2*i0+1,2*i1,nz1-1) + tmp;
syz1->SetVal(2*i0,2*i1+1,nz1) = -syz1->GetVal(2*i0,2*i1+1,nz1-1) + tmp;
syz1->SetVal(2*i0+1,2*i1+1,nz1) = -syz1->GetVal(2*i0+1,2*i1+1,nz1-1) + tmp;
}
}
}
}