proteus  1.8.1
C/C++/Fortran libraries
Inout.cpp
Go to the documentation of this file.
1 #include <math.h>
2 #include "ncurses/curses.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 
7 #define Subprograms
8 #define Int extern int
9 #define Double extern double
10 #include "Headers.h"
11 
12 
13 
14 extern double SU;
15 
16 void
17  Title_block(FILE*), Input_Data_block(FILE*);
18 
19 /**********************************************************************/
20 int Read_data(void)
21 /**********************************************************************/
22 {
23 Readtext(Title);
24 iff(Title, FINISH) return(0);
25 Read(MaxH,lf);
26 fscanf(Input1,"%s", Case); Skip;
27 iff(Case,Wavelength)
28  {
29  Read(L,lf);
30  Height = MaxH/L;
31  }
32 iff(Case,Period)
33  {
34  Read(T,lf);
35  Height = MaxH/(T*T);
36  }
38 Read(Current,lf);
39 if(Current_criterion == 1) strcpy(Currentname, Current1);
40 if(Current_criterion == 2) strcpy(Currentname, Current2);
41 
42 Read(n,d);
43 Read(nstep,d);
44 
45 Input_Data_block(monitor);
46 
47 if(strcmp(Theory,"Stokes")==0)
48  {
49  iff(Case,Wavelength)
50  if(L > 10.)
51  {
52  printf("\nThe dimensionless wavelength is greater than 10.");
53  printf("\nStokes theory should not be applied. Exiting.");
54  getch();
55  exit(1);
56  }
57  iff(Case,Period)
58  if(T > 10.)
59  {
60  printf("\nThe dimensionless period is greater than 10.");
61  printf("\nStokes theory should not be applied. Exiting.");
62  getch();
63  exit(1);
64  }
65  }
66 
67 // Convergence criteria
68 
69 Input2=fopen(Convergence_file,"r");
70 fgets(dummy,400,Input2);
71 fscanf(Input2,"%d", &number);fgets(dummy,400,Input2);
72 fscanf(Input2,"%le", &crit);fgets(dummy,400,Input2);
73 fclose(Input2);
74 
75 // Number of data points to present results for
76 
77 Input2 = fopen(Points_file,"r");
78 fgets(dummy,400,Input2);
79 // Number of points on surface profile (clustered quadratically near crest)
80 fscanf(Input2,"%d", &Surface_points);fgets(dummy,400,Input2);
81 // Number of vertical profiles
82 fscanf(Input2,"%d", &Nprofiles);fgets(dummy,400,Input2);
83 // Number of points in each profile
84 fscanf(Input2,"%d", &Points);fgets(dummy,400,Input2);
85 
86 fclose(Input2);
87 
88 return(1);
89 }
90 
91 // PRINT OUT TITLE BLOCKS
92 
93 void Input_Data_block(FILE* file)
94 {
95 fprintf(file,"# %s", Title);
96 fprintf(file,"\n\n# Printing input data here to check");
97 fprintf(file,"\n\n# Height/Depth:%6.3f", MaxH);
98 iff(Case,Wavelength)
99  {
100  fprintf(file,"\n# Length/Depth:%7.2f", L);
101  }
102 iff(Case,Period)
103  {
104  fprintf(file,"\n# Dimensionless Period T*sqrt(g/d):%7.2f", T);
105  }
106 fprintf(file,"\n# Current criterion: %s, Dimensionless value:%6.3lf", Currentname, Current);
107 
108 if(strcmp(Theory,"Stokes")==0)
109  {
110  if(n<=5) sprintf(Method, "\n# Solution by %d-order Stokes theory", n);
111  else
112  {
113  n = 5;
114  sprintf(Method, "\n# Solution by %d-order Stokes theory", n);
115  printf("\n\n# (A value of N > 5 has been specified for the Stokes theory.");
116  printf("\n# I do not have a theory for that. The program has set N = 5)");
117  }
118  }
119 if(strcmp(Theory,"Fourier")==0)
120  sprintf(Method, "\n# Solution by %d-term Fourier series", n);
121 
122 fprintf(file,"\n%s\n", Method);
123 }
124 
125 void Title_block(FILE* file)
126 {
127 // Highest wave - eqn (32) of Fenton (1990)
128 L = 2*pi/z[1];
129 Highest = (0.0077829*L*L*L+0.0095721*L*L+0.141063*L)
130  /(0.0093407*L*L*L+0.0317567*L*L+0.078834*L+1);
131 fprintf(file,"# %s", Title);
132 fprintf(file,"\n%s\n", Method);
133 fprintf(file,"\n# Height/Depth:%6.3f, %3.0lf\%% of the maximum of H/d =%6.3f for this length:",
134  z[2]/z[1],z[2]/z[1]/Highest*100., Highest);
135 fprintf(file,"\n# Length/Depth:%7.2f", 2*pi/z[1]);
136 fprintf(file,"\n# Dimensionless Period T*sqrt(g/d):%7.2f", z[3]/sqrt(z[1]));
137 fprintf(file,"\n# Current criterion: %s, Dimensionless value:%6.3lf\n", Currentname, Current);
138 }
139 
140 void Output(void)
141 {
142 int i, I;
143 double X, eta, y;
144  double Surface(double);
145  void Point(double, double);
146 
147 fprintf(monitor,"\n\n# Solution summary:\n\n");
148 Title_block(monitor);
149 
150 // Print out summary file of solution
151 
152 Title_block(Solution);
153 
154 kd = z[1];
155 L=2*pi/z[1];
156 H=z[2]/z[1];
157 T=z[3]/sqrt(z[1]);
158 c=z[4]/sqrt(z[1]);
159 ce=z[5]/sqrt(z[1]);
160 cs=z[6]/sqrt(z[1]);
161 ubar=z[7]/sqrt(z[1]);
162 Q=ubar-z[8]/pow(kd,1.5);
163 R=1+z[9]/z[1];
164 
165 pulse=z[8]+z[1]*z[5];
166 ke=0.5*(z[4]*pulse-z[5]*Q*pow(kd,1.5));
167 
168 // Calculate potential energy, not by computing the mean of 1/2 (eta-d)^2
169 // but by exploiting orthogonality of the cosine functions to give the sum of 1/4 Y[i]^2
170 pe = 0;
171 for(i=1;i<=n;++i)
172  pe += 0.25*pow(Y[i],2);
173 
174 ub2=2.*z[9]-z[4]*z[4];
175 sxx=4.*ke-3.*pe+ub2*z[1]+2.*z[5]*(z[7]*z[1]-z[8]);
176 f=z[4]*(3.*ke-2.*pe)+0.5*ub2*(pulse+z[4]*z[1])+z[4]*z[5]*(z[7]*z[1]-z[8]);
177 q=z[7]*z[1]-z[8];
178 r=z[9]+z[1];
179 s=sxx-2.*z[4]*pulse+(z[4]*z[4]+0.5*z[1])*z[1];
180 
181 fprintf(Solution, "\n# Stokes-Ursell number %7.3f", 0.5*z[2]/pow(z[1],3));
182 fprintf(Solution, "\n\n# Integral quantities - notation from Fenton (1988)");
183 fprintf(Solution, "\n# (1) Quantity, (2) symbol, solution non-dimensionalised by (3) g & wavenumber, and (4) g & mean depth\n");
184 fprintf(Solution, "\n# Water depth (d)" LO LO, z[1], 1.);
185 fprintf(Solution, "\n# Wave length (lambda)" LO LO, 2*pi, L);
186 fprintf(Solution, "\n# Wave height (H)" LO LO, z[2], H);
187 fprintf(Solution, "\n# Wave period (tau)" LO LO, z[3], T);
188 fprintf(Solution, "\n# Wave speed (c)" LO LO, z[4], c);
189 fprintf(Solution, "\n# Eulerian current (u1_)" LO LO, z[5], ce);
190 fprintf(Solution, "\n# Stokes current (u2_)" LO LO, z[6], cs);
191 fprintf(Solution, "\n# Mean fluid speed in frame of wave (U_)" LO LO, z[7], ubar);
192 fprintf(Solution, "\n# Volume flux due to waves (q)" LO LO, z[8], z[8]/pow(kd,1.5));
193 fprintf(Solution, "\n# Bernoulli constant (r)" LO LO, z[9], z[9]/kd);
194 fprintf(Solution, "\n# Volume flux (Q)" LO LO, Q*pow(kd,1.5), Q);
195 fprintf(Solution, "\n# Bernoulli constant (R)" LO LO, R*kd, R);
196 fprintf(Solution, "\n# Momentum flux (S)" LO LO, s, s/kd/kd );
197 fprintf(Solution, "\n# Impulse (I)" LO LO, pulse, pulse/pow(kd,1.5));
198 fprintf(Solution, "\n# Kinetic energy (T)" LO LO, ke, ke/kd/kd);
199 fprintf(Solution, "\n# Potential energy (V)" LO LO, pe, pe/kd/kd);
200 fprintf(Solution, "\n# Mean square of bed velocity (ub2_)" LO LO, ub2, ub2/kd);
201 fprintf(Solution, "\n# Radiation stress (Sxx)" LO LO, sxx, sxx/kd/kd);
202 fprintf(Solution, "\n# Wave power (F)" LO LO, f, f/pow(kd,2.5));
203 
204 fprintf(Solution, "\n\n# Dimensionless coefficients in Fourier series" );
205 fprintf(Solution, "\n# Potential/Streamfn\tSurface elevations" );
206 fprintf(Solution, "\n# j, B[j], & E[j], j=1..N\n" );
207 for ( i=1 ; i <= n ; i++ )
208 fprintf(Solution, "\n%2d\t%15.7e\t%15.7e", i, B[i], Y[i]);
209 fprintf(Solution, "\n\n" );
210 
211 // Surface - print out coordinates of points on surface for plotting plus check of pressure on surface
212 
213 fprintf(Elevation, "# %s\n", Title);
214 fprintf(Elevation, "%s\n", Method);
215 fprintf(Elevation, "\n# Surface of wave - trough-crest-trough,");
216 fprintf(Elevation, " note quadratic point spacing clustered around crest");
217 fprintf(Elevation, "\n# Non-dimensionalised with respect to depth");
218 fprintf(Elevation, "\n# X/d, eta/d, & check of surface pressure\n");
219 
220 for ( i=-Surface_points/2 ; i <= Surface_points/2; i++)
221  {
222  X = 2 * L * (i * fabs(i)/Surface_points/Surface_points); //NB Quadratic point spacing, clustered near crest
223  eta = Surface(X);
224  Point(X,eta);
225  fprintf(Elevation, "\n%8.4lf\t%7.4f\t%7.0e", X, eta, Pressure);
226  }
227 fprintf(Elevation, "\n\n");
228 
229 // Surface - print out Velocity and acceleration profiles plus check of Bernoulli
230 
231 fprintf(Flowfield, "# %s\n", Title);
232 fprintf(Flowfield, "%s\n", Method);
233 fprintf(Flowfield, "\n# Velocity and acceleration profiles and Bernoulli checks\n");
234 fprintf(Flowfield, "\n# All quantities are dimensionless with respect to g and/or d\n");
235 fprintf(Flowfield, "\n#*******************************************************************************");
236 fprintf(Flowfield, "\n# y u v dphi/dt du/dt dv/dt du/dx du/dy Bernoulli check ");
237 fprintf(Flowfield, "\n# - ------------- ------- ------ ----- ------------- --------------- ");
238 fprintf(Flowfield, "\n# d sqrt(gd) gd g g sqrt(g/d) gd ");
239 fprintf(Flowfield, "\n#*******************************************************************************");
240 
241 for(I = 0; I <= Nprofiles ; ++I)
242  {
243  X = 0.5 * L * I/(Nprofiles);
244  eta = Surface(X);
245  fprintf(Flowfield, "\n\n# X/d = %8.4f, Phase = %6.1f°\n", X, X/L*360);
246 
247  for(i=0 ; i <= Points; ++i)
248  {
249  y = (i)*eta/(Points);
250  Point(X, y);
251  fprintf(Flowfield, "\n%7.4f\t%7.4f\t%7.4f\t%7.4f\t%7.4f\t%7.4f\t%7.4f\t%7.4f\t%7.4f",
252  y, u, v, dphidt, ut, vt, ux, uy, Bernoulli_check);
253  }
254  }
255 fprintf(Flowfield, "\n\n");
256 
257 /*
258 Procedure for recording every run - not activated in distribution versions
259 If the lines below are not commented out the program will add a line to a
260 file Catalogue.res, which could have this as a header:
261 
262 # A continuing record of all runs with Fourier, Cnoidal, or Stokes.
263 # Any run of those programs adds a line to it.
264 # This can be edited at any time.
265 # Columns are: name of theory, N, H/d, L/d, Stokes-Ursell Number,
266 # wave height as a percentage of the highest possible for that L/d,
267 # mean horizontal velocity on a vertical line under the crest.
268 
269 # Theory n H/d L/d S-U Highest% u_crest_mean
270 */
271 
272 // To activate, de-comment these lines
273 /***************************************************************************
274 FILE *Output1;
275 double Velo[Points+1], sum1, sum2, ucm;
276 Output1 = fopen(Diagname,"a");
277 
278 X = 0.;
279 eta = Surface(X);
280 
281 for(i=0 ; i <= Points; ++i)
282  {
283  y = (i)*eta/(Points);
284  Point(X, y);
285  Velo[i] = u;
286  }
287 
288 for(i=1, sum1=0; i <= Points-1; i+=2) sum1 += Velo[i];
289 for(i=2, sum2=0; i <= Points-2; i+=2) sum2 += Velo[i];
290 ucm = (Velo[0]+4*sum1+2*sum2+Velo[Points])/3./Points;
291 
292 I = strlen(Theory)+1;
293 for(i=I; i <=8 ; ++i) strcat(Theory," ");
294 fprintf(Output1,"\n%s%2d\t%7.4f\t%8.3f\t%7.3f\t%3.0f\t%7.4f",
295  Theory, n, H, L, 0.5*z[2]/pow(z[1],3), z[2]/z[1]/Highest*100., ucm);
296 *************************************************************************/
297 }
298 
299 // Surface elevation
300 
301 double Surface(double x)
302 {
303 int j;
304 static double kEta;
305 
306 kEta = kd;
307 for ( j = 1 ; j < n ; j++ )
308  kEta += Y[j] * cos(j*x*kd);
309 kEta += 0.5*Y[n] * cos(n*x*kd);
310 return (kEta/kd);
311 }
312 
313 // Velocities, accelerations, and pressure at a point
314 
315 void Point(double X, double y)
316 {
317 int j;
318 double Cosh, Sinh, Sin, Cos;
319 double coshdelta,sinhdelta;
320 
321 u = v = ux = vx = phi = psi = 0.;
322 
323 for ( j = 1 ; j <= n ; j++ )
324  {
325  Cos = cos(j*X*kd);
326  Sin = sin(j*X*kd);
327  coshdelta = cosh(j*kd*(y-1.));
328  sinhdelta = sinh(j*kd*(y-1.));
329  Cosh = coshdelta+sinhdelta*Tanh[j];
330  Sinh = sinhdelta+coshdelta*Tanh[j];
331  phi += B[j] * Cosh * Sin;
332  u += j * B[j] * Cosh * Cos;
333  v += j * B[j] * Sinh * Sin;
334  ux += - j * j * B[j] * Cosh * Sin;
335  vx += j * j * B[j] * Sinh * Cos;
336  }
337 
338 // All PHI, PSI, u, v, ux and vx are dimensionless w.r.t. g & k.
339 // Now convert to dimensionless w.r.t. d.
340 
341 phi /= pow(kd,1.5);
342 u /= pow(kd,0.5);
343 v /= pow(kd,0.5);
344 ux *= pow(kd,0.5);
345 vx *= pow(kd,0.5);
346 
347 u = ce + u;
348 phi = ce * X + phi;
349 dphidt = -c * u;
350 
351 ut = -c * ux;
352 vt = -c * vx;
353 uy = vx;
354 vy = -ux;
355 dudt = ut + u*ux + v*uy;
356 dvdt = vt + u*vx + v*vy;
357 Pressure = R - y - 0.5 * ((u-c)*(u-c)+v*v);
358 Bernoulli_check = dphidt + Pressure + y + 0.5*(u*u+v*v)-(R-0.5*c*c);
359 
360 return;
361 }
362 
Y
Double * Y
Definition: Headers.h:48
ub2
Double ub2
Definition: Headers.h:90
dudt
Double dudt
Definition: Headers.h:61
B
Double * B
Definition: Headers.h:41
Skip
#define Skip
Definition: Headers.h:1
Highest
Double Highest
Definition: Headers.h:67
Input_Data_block
void Input_Data_block(FILE *)
Definition: Inout.cpp:93
kd
Double kd
Definition: Headers.h:70
MaxH
Double MaxH
Definition: Headers.h:73
f
Double f
Definition: Headers.h:64
number
Int number
Definition: Headers.h:33
ut
Double ut
Definition: Headers.h:92
crit
Double crit
Definition: Headers.h:56
s
Double s
Definition: Headers.h:84
Point
void Point(double X, double y)
Definition: Inout.cpp:315
Tanh
Double * Tanh
Definition: Headers.h:47
Pressure
Double Pressure
Definition: Headers.h:77
Surface_points
Int Surface_points
Definition: Headers.h:35
dvdt
Double dvdt
Definition: Headers.h:62
L
Double L
Definition: Headers.h:72
Points
Int Points
Definition: Headers.h:34
n
Int n
Definition: Headers.h:28
q
Double q
Definition: Headers.h:81
phi
Double phi
Definition: Headers.h:76
iff
#define iff(x, y)
Definition: Headers.h:3
ce
Double ce
Definition: Headers.h:55
Current_criterion
Int Current_criterion
Definition: Headers.h:27
ux
Double ux
Definition: Headers.h:93
sxx
Double sxx
Definition: Headers.h:86
R
Double R
Definition: Headers.h:82
H
Double H
Definition: Headers.h:65
vx
Double vx
Definition: Headers.h:97
ubar
Double ubar
Definition: Headers.h:91
v
Double v
Definition: Headers.h:95
Case
char Case[]
vt
Double vt
Definition: Headers.h:96
Bernoulli_check
Double Bernoulli_check
Definition: Headers.h:53
Height
Double Height
Definition: Headers.h:66
uy
Double uy
Definition: Headers.h:94
T
Double T
Definition: Headers.h:87
vy
Double vy
Definition: Headers.h:98
z
Double * z
Definition: Headers.h:49
pe
Double pe
Definition: Headers.h:75
u
Double u
Definition: Headers.h:89
c
Double c
Definition: Headers.h:54
Surface
double Surface(double x)
Definition: Inout.cpp:301
ke
Double ke
Definition: Headers.h:71
dphidt
Double dphidt
Definition: Headers.h:60
Q
Double Q
Definition: Headers.h:80
pulse
Double pulse
Definition: Headers.h:79
Read
#define Read(x, y)
Definition: Headers.h:7
Nprofiles
Int Nprofiles
Definition: Headers.h:29
Headers.h
SU
double SU
Definition: Fourier.cpp:26
Output
void Output(void)
Definition: Inout.cpp:140
r
Double r
Definition: Headers.h:83
Current
Double Current
Definition: Headers.h:59
Title_block
void Title_block(FILE *)
Definition: Inout.cpp:125
pi
const double pi
Definition: createAnalyticGeometry.cpp:477
Read_data
int Read_data(void)
Definition: Inout.cpp:20
LO
#define LO
Definition: Headers.h:5
psi
Double psi
Definition: Headers.h:78
nstep
Int nstep
Definition: Headers.h:31
Readtext
#define Readtext(x)
Definition: Headers.h:6
cs
Double cs
Definition: Headers.h:58