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