proteus
1.8.1
C/C++/Fortran libraries
Users
cekees
proteus
proteus
fenton
Fourier.cpp
Go to the documentation of this file.
1
2
// Steady wave program - C++ version
3
4
#include <math.h>
5
#include <stdio.h>
6
#include <sys/types.h>
7
#include <string.h>
8
#include "ncurses/curses.h"
9
#include <stdlib.h>
10
#define ANSI
11
#include "
Allocation.h
"
12
13
void
14
init
(
void
),
Solve
(
void
),
Title_block
(FILE*),
Output
(
void
);
15
int
16
flushall
(
void
);
17
18
#define Main
19
#define Int int
20
#define Double double
21
#include "
Headers.h
"
22
//#define Diagnostic
23
//#ifdef Diagnostic
24
//char Diagname[30], Theory[10];
25
//#endif
26
double
SU
;
27
28
void
runfourier
()
29
{
30
int
i, j, iter, m;
31
int
Read_data
(
void
);
32
33
double
Newton
(
int
), dhe, dho, error, **CC;
34
void
Powell(
double
*,
double
**,
int
,
double
,
int
*,
double
*,
double
(*)(
double
*));
35
36
Input1 = fopen(
"Data.dat"
,
"r"
);
37
38
strcpy(Convergence_file,
"Convergence.dat"
);
39
strcpy(Points_file,
"Points.dat"
);
40
monitor = stdout;
41
strcpy(Theory,
"Fourier"
);
42
strcpy(Diagname,
"Catalogue.res"
);
43
44
for
(
wave
=1 ;
wave
<2;
wave
++ )
45
{
46
if
(
Read_data
() == 0)
break
;
47
num
=2*
n
+10;
48
dhe=
Height
/
nstep
;
49
dho=
MaxH
/
nstep
;
50
51
CC =
dmatrix
(1,
num
,1,
num
);
52
for
( j=1; j <=
num
; ++j)
53
{
54
for
( i=1; i <=
num
; ++i)
55
CC[j][i] = 0.;
56
CC[j][j] = 1.;
57
}
58
Y
=
dvector
(0,
num
);
59
z
=
dvector
(1,
num
);
60
rhs1
=
dvector
(1,
num
);
61
rhs2
=
dvector
(1,
num
);
62
coeff
=
dvector
(0,
n
);
63
cosa
=
dvector
(0,2*
n
);
64
sina
=
dvector
(0,2*
n
);
65
sol
=
dmatrix
(0,
num
,1,2);
66
B
=
dvector
(1,
n
);
67
Tanh
=
dvector
(1,
n
);
68
69
// Commence stepping through steps in wave height
70
71
for
(
ns
= 1 ;
ns
<=
nstep
;
ns
++ )
72
{
73
height
=
ns
*dhe;
74
Hoverd
=
ns
*dho;
75
fprintf(monitor,
"\n\nHeight step %2d of %2d\n"
,
ns
,
nstep
);
76
77
// Calculate initial linear solution
78
79
if
(
ns
<= 1)
init
();
80
81
// Or, extrapolate for next wave height, if necessary
82
83
else
84
for
( i=1 ; i <=
num
; i++ )
85
z
[i]=2.*
sol
[i][2]-
sol
[i][1];
86
87
// Commence iterative solution
88
89
for
(iter=1 ; iter <=
number
; iter++ )
90
{
91
fprintf(monitor,
"\nIteration%3d:"
, iter);
92
93
// Calculate right sides of equations and differentiate numerically
94
// to obtain Jacobian matrix, then solve matrix equation
95
96
error =
Newton
(iter);
97
98
// Convergence criterion satisfied?
99
100
fprintf(stdout,
" Mean of corrections to free surface: %8.1e"
, error);
101
if
(
ns
==
nstep
)
criter
= 1.e-10 ;
102
else
criter
=
crit
;
103
if
((error <
criter
* fabs(
z
[1])) && iter > 1 )
break
;
104
if
(iter ==
number
)
105
{
106
fprintf(stdout,
"\nNote that the program still had not converged to the degree specified\n"
);
107
}
108
109
// Operations for extrapolations if more than one height step used
110
111
if
(
ns
== 1)
112
for
( i=1 ; i<=
num
; i++ )
113
sol
[i][2] =
z
[i];
114
else
115
for
( i=1 ; i<=
num
; i++ )
116
{
117
sol
[i][1] =
sol
[i][2];
118
sol
[i][2] =
z
[i];
119
}
120
}
121
122
// Fourier coefficients (for surface elevation by slow Fourier transform)
123
124
for
(
Y
[0] = 0., j = 1 ; j <=
n
; j++ )
125
{
126
B
[j]=
z
[j+
n
+10];
127
sum
= 0.5*(
z
[10]+
z
[
n
+10]*pow(-1.,(
double
)j));
128
for
( m = 1 ; m <=
n
-1 ; m++ )
129
sum
+=
z
[10+m]*
cosa
[(m*j)%(
n
+
n
)];
130
Y
[j] = 2. *
sum
/
n
;
131
}
132
}
// End stepping through wave heights
133
134
// Print results
135
136
Solution=fopen(
"Solution.res"
,
"w"
);
137
Elevation = fopen(
"Surface.res"
,
"w"
);
138
Flowfield = fopen(
"Flowfield.res"
,
"w"
);
139
Output
();
140
fflush(NULL);
141
printf(
"\nTouch key to continue\n\n"
); getch();
142
143
free_dmatrix
(CC,1,
num
,1,
num
);
144
free_dvector
(
Y
,0,
num
);
145
free_dvector
(
z
, 1,
num
);
146
free_dvector
(
rhs1
, 1,
num
);
147
free_dvector
(
rhs2
, 1,
num
);
148
free_dvector
(
coeff
, 0,
n
);
149
free_dvector
(
cosa
, 0,2*
n
);
150
free_dvector
(
sina
, 0,2*
n
);
151
free_dmatrix
(
sol
, 0,
num
,1,2);
152
free_dvector
(
B
, 1,
n
);
153
free_dvector
(
Tanh
, 1,
n
);
154
}
// End stepping through waves
155
156
printf(
"\nFinished\n"
);
157
}
158
159
int
main
(
void
)
160
{
161
runfourier
();
162
}
// End main program
dmatrix
double ** dmatrix(long nrl, long nrh, long ncl, long nch)
Definition:
Util.cpp:16
Y
Double * Y
Definition:
Headers.h:48
Solve
void Solve(void)
Title_block
void Title_block(FILE *)
Definition:
Inout.cpp:125
dvector
double * dvector(long nl, long nh)
Definition:
Util.cpp:7
B
Double * B
Definition:
Headers.h:41
criter
Double criter
Definition:
Headers.h:57
Hoverd
Double Hoverd
Definition:
Headers.h:68
flushall
int flushall(void)
MaxH
Double MaxH
Definition:
Headers.h:73
main
int main(void)
Definition:
Fourier.cpp:159
Allocation.h
SU
double SU
Definition:
Fourier.cpp:26
number
Int number
Definition:
Headers.h:33
rhs1
Double * rhs1
Definition:
Headers.h:44
coeff
Double * coeff
Definition:
Headers.h:42
crit
Double crit
Definition:
Headers.h:56
sum
Double sum
Definition:
Headers.h:85
Tanh
Double * Tanh
Definition:
Headers.h:47
free_dmatrix
void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
Definition:
Util.cpp:44
n
Int n
Definition:
Headers.h:28
wave
Int wave
Definition:
Headers.h:36
num
Int num
Definition:
Headers.h:32
cosa
Double * cosa
Definition:
Headers.h:43
Newton
double Newton(int count)
Definition:
Subroutines.cpp:139
Height
Double Height
Definition:
Headers.h:66
sina
Double * sina
Definition:
Headers.h:46
free_dvector
void free_dvector(double *v, long nl, long nh)
Definition:
Util.cpp:38
z
Double * z
Definition:
Headers.h:49
ns
Int ns
Definition:
Headers.h:30
Output
void Output(void)
Definition:
Inout.cpp:140
runfourier
void runfourier()
Definition:
Fourier.cpp:28
sol
Double ** sol
Definition:
Headers.h:40
init
void init(void)
rhs2
Double * rhs2
Definition:
Headers.h:45
Headers.h
height
Double height
Definition:
Headers.h:69
Read_data
int Read_data(void)
Definition:
Inout.cpp:20
nstep
Int nstep
Definition:
Headers.h:31
Generated on Fri Jul 1 2022 11:17:31 for proteus by
1.8.20