forked from liebannam/pipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testCltracking.cpp
170 lines (159 loc) · 5.59 KB
/
testCltracking.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
#include "setupandrun.h"
int main(int argc, char* argv[])
{
clock_t start_t, end_t;
double A = 0.5;
double B = 0.5;
double KCl = 0.55;
double kw = 0.0;
char *finp = argv[1];
char *fconfig = argv[2];
int M, Mi;
double T,L,u,e2=0;
int channeltype = 1;
Network * Ntwk = setupNetwork(finp, fconfig, M,Mi,T, channeltype);
int Nedges = Ntwk->Nedges;
int N = Ntwk->channels[0]->N;
double dt = T/(double)M;
double Clvals0[M+1];
double Clvals1[M+1];
double pi = 4.*atan(1.);
u = Ntwk->channels[0]->q[Ntwk->channels[0]->N]/Ntwk->channels[0]->q[0];
double Lloc = Ntwk->channels[0]->L;
if (Nedges<2){ L =Lloc*Nedges;}
else {L = Lloc*2;}
double t,dx;
for (int i = 0; i<M+1; i++)
{
t = dt*(float)i;
Clvals0[i] = exp(-KCl*t)*(A+B*cos(-2*pi*u/L*t));
Clvals1[i] = exp(-KCl*t)*(A+B*cos(2*pi/L*(L-u*t)));
}
if (Nedges==3)
{
int Nf = N*2;
double c0[Nf];
dx = Lloc/(double)N;
for (int i = 0; i<Nf; i++)
{
c0[i] = A+B*cos(2*pi*dx/Lloc*(double)i);
}
for (int i = 0; i<M+1;i++)
{
Clvals0[i] = exp(-KCl*t)*(A+B*cos(-2*pi*u/Lloc*t));
Clvals1[i] = 0.;
}
for (int i =0; i<N; i++)
{
Ntwk->channels[0]->Cl[i] = c0[i];
Ntwk->channels[0]->Cl0[i] = c0[i];
Ntwk->channels[1]->Cl[i] = c0[i+N];
Ntwk->channels[1]->Cl0[i] = c0[i+N];
Ntwk->channels[2]->Cl[i] = c0[i+N];
Ntwk->channels[2]->Cl0[i] = c0[i+N];
}
Ntwk->junction1s[0]->setClbVal(Clvals0);
Ntwk->junction1s[1]->setClbVal(Clvals1);
Ntwk->junction1s[2]->setClbVal(Clvals1);
Ntwk->channels[0]->setClkw(kw);
Ntwk->channels[1]->setClkw(kw);
Ntwk->channels[2]->setClkw(kw);
Ntwk->runForwardProblem(dt);
double times[1] = {0};
int which[1] = {0};
for (int j=0; j<3;j++)
{printf("j = %d\n\n",j);
Ntwk->channels[j]->quickWrite(times, which, 1,T,1);
}
printf("Elapsed simulation time is %f\n", dt*(double)(M));
double err, Cest, Ctrue,x;
for(int j =0; j<3; j++)
{
int jfake = min(j,1);
//printf("jfake=%d\n\n!\n",jfake);
for(int k = 0; k<Ntwk->channels[0]->N;k++)
{
Cest = Ntwk->channels[j]->Cl[k];
x = dx*(float)k+jfake*Lloc;
Ctrue =exp(-KCl*T)*(A+B*cos(2*pi/Lloc*(x- u*T)));
err = Cest-Ctrue;
e2+=dx*err*err;
printf("%f %f %f %f %f\n",(float)k*dx, c0[k], Cest, Ctrue, err);
}
}
}
else
{
int Nf =N*Nedges;
double c0[Nf];
dx = Lloc/(double)N;
if (Nedges==2)
{
printf("N = %d Nf = %d \n",N,Nf);
// printf("whoops, check your .inp file. this network has %d pipes\n", N);
for (int i = 0; i<Nf; i++)
{
c0[i] = A+B*cos(2*pi*dx/L*(double)i);
}
for (int i =0; i<N; i++)
{
Ntwk->channels[0]->Cl[i] = c0[i];
Ntwk->channels[0]->Cl0[i] = c0[i];
Ntwk->channels[1]->Cl[i] = c0[i+N];
Ntwk->channels[1]->Cl0[i] = c0[i+N];
}
Ntwk->junction1s[0]->setClbVal(Clvals0);
Ntwk->junction1s[1]->setClbVal(Clvals1);
Ntwk->channels[0]->setClkw(kw);
Ntwk->channels[1]->setClkw(kw);
}
else
{
for (int i = 0; i<N; i++)
{
c0[i] = A+B*cos(2*pi*dx/L*(double)i);
Ntwk->channels[0]->Cl[i] = c0[i];
Ntwk->channels[0]->Cl0[i] = c0[i];
}
Ntwk->junction1s[0]->setClbVal(Clvals0);
Ntwk->junction1s[1]->setClbVal(Clvals1);
Ntwk->channels[0]->setClkw(kw);
}
double V0=Ntwk->getTotalVolume();
start_t = clock();
for(int k=0; k<Nedges; k++)
{
printf("Channel %d info:\n",k);
Ntwk->channels[k]->showGeom();
}
for(int k = 0; k<N;k++)printf("%f %f %f\n",(float)k*dx, Ntwk->channels[0]->Cl[k], exp(-KCl*0.)*(A+B*cos(2*pi/L*(dx*(float)k))));
Ntwk->runForwardProblem(dt);
printf("Elapsed simulation time is %f\n", dt*(double)(M));
double f = 0;
for (int i=0; i<M+1; i++)f+=pow(dt*Ntwk->getAveGradH(i),2.)/2.;
double V = Ntwk->getTotalVolume();
cout<<"initial volume "<<V0<< " "<<"Final Volume " <<V<< endl;
cout<<"dV = "<<V-V0<<endl;
cout<<"f = "<<f<<endl;
cout<<"maximum wave speed is "<<Ntwk->channels[0]->Cgrav(PI*.25/4.,false)<<endl;
printf("Af is %f\n", Ntwk->channels[0]->At);
printf("dt = %f , dx = %f, CFL = %f\n",dt, dx, dt/dx*Ntwk->channels[0]->a);
double Cest, Ctrue, err=0., x;
// Ntwk->channels[0]->quickWrite(times, which, 1,T,1);
for(int j =0; j<Nedges; j++)
{
for(int k = 0; k<Ntwk->channels[0]->N;k++)
{
Cest = Ntwk->channels[j]->Cl[k];
x = dx*(float)k+j*Lloc;
Ctrue =exp(-KCl*T)*(A+B*cos(2*pi/L*(x- u*T)));
err = Cest-Ctrue;
e2+=dx*err*err;
printf("%f %f %f %f %f\n",(float)k*dx+j*Lloc, c0[k+j*N], Cest, Ctrue, err);
}
}
}
printf("Npipes = %d\n",Nedges);
printf("Chlorine decay const = %f and u = %f and dx = %f\n",KCl,u,dx);
printf("||c-ctrue||_2 = %.16f\n",e2);
}