-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFourier.cpp
158 lines (127 loc) · 3.88 KB
/
Fourier.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
// Steady wave program - C++ version
#include <math.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <curses.h>
#include <stdlib.h>
#define ANSI
#include "Allocation.h"
void
init(void), Solve(void), Title_block(FILE*), Output(void);
int
flushall(void);
#define Main
#define Int int
#define Double double
#include "Headers.h"
//#define Diagnostic
//#ifdef Diagnostic
//char Diagname[30], Theory[10];
//#endif
double SU;
int main(void)
{
int i, j, iter, m;
int Read_data(void);
double Newton(int), dhe, dho, error, **CC;
void Powell(double *, double **, int, double, int *, double *,double (*)(double *));
Input1 = fopen("Data.dat","r");
strcpy(Convergence_file,"Convergence.dat");
strcpy(Points_file,"Points.dat");
monitor = stdout;
strcpy(Theory,"Fourier");
strcpy(Diagname,"Catalogue.res");
for ( wave=1 ; wave<2; wave++ )
{
if (Read_data() == 0) break;
num=2*n+10;
dhe=Height/nstep;
dho=MaxH/nstep;
CC = dmatrix(1,num,1,num);
for ( j=1; j <=num ; ++j)
{
for ( i=1; i <=num ; ++i)
CC[j][i] = 0.;
CC[j][j] = 1.;
}
Y = dvector(0,num);
z = dvector(1,num);
rhs1 = dvector(1,num);
rhs2 = dvector(1,num);
coeff = dvector(0, n);
cosa = dvector(0,2*n);
sina = dvector(0,2*n);
sol = dmatrix(0,num,1,2);
B = dvector(1, n);
Tanh = dvector(1,n);
// Commence stepping through steps in wave height
for ( ns = 1 ; ns <= nstep ; ns++ )
{
height=ns*dhe;
Hoverd=ns*dho;
fprintf(monitor,"\n\nHeight step %2d of %2d\n", ns, nstep);
// Calculate initial linear solution
if(ns <= 1) init();
// Or, extrapolate for next wave height, if necessary
else
for ( i=1 ; i <= num ; i++ )
z[i]=2.*sol[i][2]-sol[i][1];
// Commence iterative solution
for (iter=1 ; iter <= number ; iter++ )
{
fprintf(monitor,"\nIteration%3d:", iter);
// Calculate right sides of equations and differentiate numerically
// to obtain Jacobian matrix, then solve matrix equation
error = Newton(iter);
// Convergence criterion satisfied?
fprintf(stdout," Mean of corrections to free surface: %8.1e", error);
if(ns == nstep) criter = 1.e-10 ;
else criter = crit;
if((error < criter * fabs(z[1])) && iter > 1 ) break;
if(iter == number)
{
fprintf(stdout,"\nNote that the program still had not converged to the degree specified\n");
}
// Operations for extrapolations if more than one height step used
if(ns == 1)
for ( i=1 ; i<=num ; i++ )
sol[i][2] = z[i];
else
for ( i=1 ; i<=num ; i++ )
{
sol[i][1] = sol[i][2];
sol[i][2] = z[i];
}
}
// Fourier coefficients (for surface elevation by slow Fourier transform)
for ( Y[0] = 0., j = 1 ; j <= n ; j++ )
{
B[j]=z[j+n+10];
sum = 0.5*(z[10]+z[n+10]*pow(-1.,(double)j));
for ( m = 1 ; m <= n-1 ; m++ )
sum += z[10+m]*cosa[(m*j)%(n+n)];
Y[j] = 2. * sum / n;
}
} // End stepping through wave heights
// Print results
Solution=fopen("Solution.res","w");
Elevation = fopen("Surface.res","w");
Flowfield = fopen("Flowfield.res","w");
Output();
fflush(NULL);
printf("\nTouch key to continue\n\n"); getch();
free_dmatrix(CC,1,num,1,num);
free_dvector(Y,0,num);
free_dvector(z, 1,num);
free_dvector(rhs1, 1,num);
free_dvector(rhs2, 1,num);
free_dvector(coeff, 0, n);
free_dvector(cosa, 0,2*n);
free_dvector(sina, 0,2*n);
free_dmatrix(sol, 0,num,1,2);
free_dvector( B, 1, n);
free_dvector(Tanh, 1,n);
} // End stepping through waves
printf("\nFinished\n");
} // End main program