forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helmholtz3.cpp
349 lines (293 loc) · 9.12 KB
/
Helmholtz3.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
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
#include <iostream>
#include <stdio.h>
#include <math.h>
#include "polylib.h"
/*
To compile (in same directory as polylib.c and polylib.h):
g++ -c polylib.c
g++ -c Helmholtz3.cpp
g++ -o Helmholtz3 Helmholtz3.o polylib.o -llapack -g2c
*/
using namespace std;
using namespace polylib;
void *diff(int np,int eln, double **D, double *p, double *pd, double *Jac);
double integr(int np, double *w, double *phi1, double *phi2);
void *rhbc(int np, int P, double *phi1, double *phi2, double *z, double *u_D, double *bc, int *Dirichlet);
double *dvector(int np);
int *ivector(int n);
void *chi(int np, double *x, double *z, double *Jac, double *bound);
void *func(int np, double *z, double *p, double lambda);
void *assem(int Mdim, int np, int P, double *z, double *w, double *phi1, double *phi2, double **M, double *f, double *p, double *u_D, double *ud_D, double *Jac, double *dphi1, double *dphi2, double lambda, int eln, double **L, double **D, int *Dirichlet, double *bc);
void *basis(int np, int P, int i, double *z, double *phi);
double **dmatrix(int Mdim);
void *sol(int np, int P, double *z, double *phi1, double *f, double *u_H, int *Dirichlet);
extern "C" {extern void dgetrf_(int *, int *, double (*), int *, int [], int*);}
extern "C" {extern void dgetrs_(unsigned char *, int *, int *, double (*), int *, int [], double [], int *, int *);}
main()
{
unsigned char TRANS = 'T';
int np=15,P=8,Mdim,NRHS=1,INFO,*ipiv,*Dirichlet,eln=0;
double *x,*z,*w,*p,*f,*phi1,*dphi1,*dphi2,*phi2,*u_H,*u_D,*ud_D,*bc,*Jac,*bound,**M,sum=0,lambda=1,**L,**D,**Dt;
/* enter global matrix size (depends on bc's!)*/
Mdim = P;
/* set up vectors and matrices */
D = dmatrix(np);
Dt = dmatrix(np);
Dirichlet = ivector(2);
ipiv = ivector(Mdim);
bc = dvector(2);
bound = dvector(2);
Jac = dvector(1);
x = dvector(np);
u_D = dvector(np);
ud_D = dvector(np);
u_H = dvector(np);
z = dvector(np);
w = dvector(np);
p = dvector(np);
f = dvector(Mdim);
phi1 = dvector(np);
dphi1 = dvector(np);
phi2 = dvector(np);
dphi2 = dvector(np);
M = dmatrix(Mdim);
L = dmatrix(Mdim);
/* Dirichlet[0] = 0 means at the left boundary a Neumann bc is posed */
/* Dirichlet[1] = 1 means at the left boudary a Dirichlet bc is posed */
/* Dirichlet[1] has similar meanings at the right boundary */
Dirichlet[0] = 0;
Dirichlet[1] = 1;
/* enter boundary conditions (bc[0] is left bc and bc[1] is right bc) */
bc[0] = M_PI;
bc[1] = 0;
/* construct 'grid' (bound[0] is left boundary and bound[1] is right boundary) */
bound[0] = -0;
bound[1] = 1;
/* get zeros and weights */
zwgll(z, w, np);
/* get differentiation matrix D */
Dgll(D, Dt, z, np);
/* get mapping details; x-locations and Jacobian */
chi(np, x, z, Jac, bound);
/* calculate p=-(pi^2+lambda)sin(pi*x) at np points x */
func(np, x, p, lambda);
/* construct Dirichlet solution u_D */
rhbc(np, P, phi1, phi2, z, u_D, bc, Dirichlet);
/* assemble element mass matrix */
assem(Mdim, np, P, z, w, phi1, phi2, M, f, p, u_D, ud_D, Jac, dphi1, dphi2, lambda, eln, L, D, Dirichlet, bc);
/* LU-decomposition using Lapack */
dgetrf_(&Mdim, &Mdim, M[0], &Mdim, ipiv, &INFO);
/* LU-solve using Lapack */
dgetrs_(&TRANS, &Mdim, &NRHS, M[0], &Mdim, ipiv, f, &Mdim, &INFO);
/* construct homogeneous solution u_H */
sol(np, P, z, phi1, f, u_H, Dirichlet);
/* generate output */
cout << "\n\nx = [";
for(int i=0;i<np-1;i++){
cout << x[i] << ";\n";
}cout << x[np-1] << "];\n";
cout << "\nu_delta = [";
for(int i=0;i<np-1;i++){
cout << u_H[i] + u_D[i] << ";\n";
}cout << u_H[np-1] + u_D[np-1] << "];\n\nplot(x,u_delta);\n\n";
}
double *dvector(int n)
{
double *v;
v = (double *)malloc(n*sizeof(double));
return v;
}
int *ivector(int n)
{
int *v;
v = (int *)malloc(n*sizeof(int));
return v;
}
double **dmatrix(int n)
{
double **A;
A = (double **)malloc(n*sizeof(double *));
A[0] = (double *)malloc(n*n*sizeof(double));
for(int i=1;i<n;i++){
A[i] = A[i-1]+n;
}
return A;
}
void *func(int np, double *z, double *p, double lambda)
{
for(int i=0;i<np;i++){
p[i] =-((M_PI*M_PI) + lambda)*sin(M_PI*z[i]);
}
}
double integr(int np, double *w, double *phi1, double *phi2)
{
register double sum = 0.;
for(int i=0;i<np;i++){
sum = sum + phi1[i]*phi2[i]*w[i];
}
return sum;
}
void *assem(int Mdim, int np, int P, double *z, double *w, double *phi1, double *phi2, double **M, double *f, double *p, double *u_D, double *ud_D, double *Jac, double *dphi1, double *dphi2, double lambda, int eln, double **L, double **D, int *Dirichlet, double *bc)
{
if(Dirichlet[0] == 0 && Dirichlet[1] == 0){
for(int i=0;i<P+1;i++){
basis(np, P, i, z, phi1);
diff(np, eln, D, phi1, dphi1, Jac);
for(int j=0;j<P+1;j++){
basis(np, P, j, z, phi2);
M[i][j] = Jac[eln]*integr(np, w, phi1, phi2);
diff(np, eln, D, phi2, dphi2, Jac);
L[i][j] = integr(np, w, dphi1, dphi2)*Jac[eln];
M[i][j] = -L[i][j] - lambda*M[i][j];
}
f[i] = Jac[eln]*(integr(np, w, phi1, p));
if(i == 0){
f[i] = f[i] + bc[0];
}else if(i == P){
f[i] = f[i] - bc[1];
}else{
f[i] = f[i];
}
}
}else if(Dirichlet[0] == 1 && Dirichlet[1] == 0){
for(int i=0;i<P;i++){
basis(np, P, i+1, z, phi1);
diff(np, eln, D, phi1, dphi1, Jac);
diff(np, eln, D, u_D, ud_D, Jac);
for(int j=0;j<P;j++){
basis(np, P, j+1, z, phi2);
M[i][j] = Jac[eln]*integr(np, w, phi1, phi2);
diff(np, eln, D, phi2, dphi2, Jac);
L[i][j] = integr(np, w, dphi1, dphi2)*Jac[eln];
M[i][j] = -L[i][j] - lambda*M[i][j];
}
f[i] = Jac[eln]*(integr(np, w, phi1, p) + integr(np, w, dphi1, ud_D));
if(i == P-1){
f[i] = f[i] - bc[1] + Jac[eln]*lambda*integr(np, w, phi1, u_D);
}else{
f[i] = f[i] + Jac[eln]*lambda*integr(np, w, phi1, u_D);
}
}
}else if(Dirichlet[0] == 0 && Dirichlet[1] == 1){
for(int i=0;i<P;i++){
basis(np, P, i, z, phi1);
diff(np, eln, D, phi1, dphi1, Jac);
diff(np, eln, D, u_D, ud_D, Jac);
for(int j=0;j<P;j++){
basis(np, P, j, z, phi2);
M[i][j] = Jac[eln]*integr(np, w, phi1, phi2);
diff(np, eln, D, phi2, dphi2, Jac);
L[i][j] = integr(np, w, dphi1, dphi2)*Jac[eln];
M[i][j] = -L[i][j] - lambda*M[i][j];
}
f[i] = Jac[eln]*(integr(np, w, phi1, p) + integr(np, w, ud_D, dphi1));
if(i == 0){
f[i] = f[i] + bc[0] + Jac[eln]*lambda*integr(np, w, phi1, u_D);
}else{
f[i] = f[i] + Jac[eln]*lambda*integr(np, w, phi1, u_D);
}
}
}else if(Dirichlet[0] == 1 && Dirichlet[1] == 1){
for(int i=0;i<P-1;i++){
basis(np, P, i+1, z, phi1);
diff(np, eln, D, phi1, dphi1, Jac);
diff(np, eln, D, u_D, ud_D, Jac);
for(int j=0;j<P-1;j++){
basis(np, P, j+1, z, phi2);
M[i][j] = Jac[eln]*integr(np, w, phi1, phi2);
diff(np, eln, D, phi2, dphi2, Jac);
L[i][j] = integr(np, w, dphi1, dphi2)*Jac[eln];
M[i][j] = -L[i][j] - lambda*M[i][j];
}
f[i] = Jac[eln]*(integr(np, w, phi1, p) + lambda*integr(np, w, phi1, u_D) + integr(np, w, dphi1, ud_D));
}
}
}
void *basis(int np, int P, int i, double *z, double *phi)
{
if(i == 0){
for(int k=0;k<np;k++){
phi[k] = (1 - z[k])/2;
}
}else if(i == P){
for(int k=0;k<np;k++){
phi[k] = (1 + z[k] )/2;
}
}else{
jacobfd(np, z, phi, NULL, i-1, 1.0, 1.0);
for(int k=0;k<np;k++){
phi[k] = ((1-z[k])/2)*((1+z[k])/2)*phi[k];
}
}
}
void *sol(int np, int P, double *z, double *phi, double *f, double *u_H, int *Dirichlet)
{
for(int i=0;i<np;i++){
u_H[i] = 0;
}
if(Dirichlet[0] == 0 && Dirichlet[1] == 0){
for(int i=0;i<P+1;i++){
basis(np, P, i, z, phi);
for(int j=0;j<np;j++){
u_H[j] = u_H[j] + f[i]*phi[j];
}
}
}else if(Dirichlet[0] == 1 && Dirichlet[1] == 0){
for(int i=0;i<P;i++){
basis(np, P, i+1, z, phi);
for(int j=0;j<np;j++){
u_H[j] = u_H[j] + f[i]*phi[j];
}
}
}else if(Dirichlet[0] == 0 && Dirichlet[1] == 1){
for(int i=0;i<P;i++){
basis(np, P, i, z, phi);
for(int j=0;j<np;j++){
u_H[j] = u_H[j] + f[i]*phi[j];
}
}
}else if(Dirichlet[0] == 1 && Dirichlet[1] == 1){
for(int i=0;i<P-1;i++){
basis(np, P, i+1, z, phi);
for(int j=0;j<np;j++){
u_H[j] = u_H[j] + f[i]*phi[j];
}
}
}
}
void *rhbc(int np, int P, double *phi1, double *phi2, double *z, double *u_D, double *bc, int *Dirichlet)
{
basis(np, P, 0, z, phi1);
basis(np, P, P, z, phi2);
for(int i=0;i<np;i++){
u_D[i] = 0;
}
for(int i=0;i<np;i++){
if(Dirichlet[0] == 1 && Dirichlet[1] == 0){
u_D[i] = bc[0]*phi1[i];
}else if(Dirichlet[1] == 1 && Dirichlet[0] == 0){
u_D[i] = bc[1]*phi2[i];
}else if(Dirichlet[0] == 1 && Dirichlet[1] == 1){
u_D[i] = bc[0]*phi1[i] + bc[1]*phi2[i];
}else{
u_D[i] = 0;
}
}
}
void *chi(int np, double *x, double *z, double *Jac, double *bound)
{
for(int i=0;i<np;i++){
x[i] = ((1 - z[i])/2)*bound[0] + ((1 + z[i])/2)*bound[1];
}
Jac[0] = (-bound[0]/2) + (bound[1]/2);
}
void *diff(int np, int eln, double **D, double *p, double *pd, double *Jac)
{
for(int i=0;i<np;i++){
pd[i] = 0;
for(int j=0;j<np;j++){
pd[i] = pd[i] + D[i][j]*p[j];
}
pd[i] = pd[i]/Jac[eln];
}
}