fmod commond

Hi
I have problem with fmod in my loop. I don't know why it can not count more than 4-5 steps. please help me.
Please post the code you have problem with.
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
#include<stdio.h>
#include<cmath>
int i;
const int ie=250;
const double cl=1000,dl=500,g=9.81,dt=0.3,te=29,h1=10,h2=1;
double t,dx,h[ie+2],q[ie+2],f1[ie+2],f2[ie+2],hs,hl,hr,ql,qr,ul,ur,al,ar,ll,lr,sl,sr,f2l,f2r;
void main() {
FILE* Depths;
FILE* Velocities;
Depths=fopen("Depths.xls","w");
Velocities=fopen("Velocities.xls","w");
dx=cl/ie;
for(i=0;i<dl/dx+1;i++) q[i]=0,h[i]=h1;
for(i=dl/dx+1;i<ie+2;i++) q[i]=0,h[i]=h2;
for (t=0;t<te+dt;t+=dt){
if (fmod(t,.6==0)){
printf("t=%f\n",t);
for(i=0;i<ie+1;i++)fprintf(Depths,"%7.4f\t",h[i]),fprintf(Velocities,"%5.2f\t",q[i]/h[i]);
}
for(i=0;i<ie+2;i++){
hl=h[i],ql=q[i],ul=ql/hl,al=sqrt(g*hl);
hr=h[i+1],qr=q[i+1],ur=qr/hr,ar=sqrt(g*hr);
hs=(hl+hr)*(0.5-0.25*(ur-ul)/(al+ar));
if(hl<hs)ll=sqrt((hs+hl)*hs/(2*hl*hl)); else ll=1;
if(hr<hs)lr=sqrt((hs+hr)*hs/(2*hr*hr)); else lr=1;
sl=ul-al*ll;
sr=ur+ar*lr;
f2l=ql*ql/hl+g*hl*hl/2;
f2r=qr*qr/hr+g*hr*hr/2;
if (sl>=0) f1[i]=ql;else if(sr>=0) f1[i]=(sr*ql-sl*qr+sr*sl*(hr-hl))/(sr-sl); else f1[i]=qr;
if (sl>=0) f2[i]=f2l;else if(sr>=0) f2[i]=(sr*f2l-sl*f2r+sr*sl*(qr-ql))/(sr-sl); else f2[i]=f2r;
}
for(i=1;i<ie+1;i++){
h[i]=h[i]-(dt/dx)*(f1[i]-f1[i-1]);
q[i]=q[i]-(dt/dx)*(f2[i]-f2[i-1]);
}
h[0]=h[1],q[0]=q[1];
h[ie+2]=h[ie+1],q[ie+2]=q[ie+1];
fprintf(Depths,"\n");
fprintf(Velocities,"\n");
}
fclose (Depths);
fclose (Velocities);

}
Last edited on
i modified as your comment, but the t parameters gives zero mod just for 4 time steps :(.
problem not solved yet, some body plz help me.
Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/



if (fmod(t,.6==0)){ // <- you comparison is .6==0 i.e. parenthesis problem

Do not compare floating point values directly. Due to the lack of precision the result of fmod might be close to 0, but not exactly 0.

do something like this: if (fmod(t,.6) < 0.0001){ // 0.0001 or whatever precision is required
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
#include<stdio.h>
#include<cmath>
int i;
const int ie=250;
const double cl=1000,dl=500,g=9.81,dt=0.3,te=29,h1=10,h2=1;
double t,dx,h[ie+2],q[ie+2],f1[ie+2],f2[ie+2],hs,hl,hr,ql,qr,ul,ur,al,ar,ll,lr,sl,sr,f2l,f2r;
void main() {
FILE* Depths;
FILE* Velocities;
Depths=fopen("Depths.xls","w");
Velocities=fopen("Velocities.xls","w");
dx=cl/ie;
for(i=0;i<dl/dx+1;i++) q[i]=0,h[i]=h1;
for(i=dl/dx+1;i<ie+2;i++) q[i]=0,h[i]=h2;
for (t=0;t<te+dt;t+=dt){
if (fmod(t,.6==0)){
printf("t=%f\n",t);
for(i=0;i<ie+1;i++)fprintf(Depths,"%7.4f\t",h[i]),fprintf(Velocities,"%5.2f\t",q[i]/h[i]);
}
for(i=0;i<ie+2;i++){
hl=h[i],ql=q[i],ul=ql/hl,al=sqrt(g*hl);
hr=h[i+1],qr=q[i+1],ur=qr/hr,ar=sqrt(g*hr);
hs=(hl+hr)*(0.5-0.25*(ur-ul)/(al+ar));
if(hl<hs)ll=sqrt((hs+hl)*hs/(2*hl*hl)); else ll=1;
if(hr<hs)lr=sqrt((hs+hr)*hs/(2*hr*hr)); else lr=1;
sl=ul-al*ll;
sr=ur+ar*lr;
f2l=ql*ql/hl+g*hl*hl/2;
f2r=qr*qr/hr+g*hr*hr/2;
if (sl>=0) f1[i]=ql;else if(sr>=0) f1[i]=(sr*ql-sl*qr+sr*sl*(hr-hl))/(sr-sl); else f1[i]=qr;
if (sl>=0) f2[i]=f2l;else if(sr>=0) f2[i]=(sr*f2l-sl*f2r+sr*sl*(qr-ql))/(sr-sl); else f2[i]=f2r;
}
for(i=1;i<ie+1;i++){
h[i]=h[i]-(dt/dx)*(f1[i]-f1[i-1]);
q[i]=q[i]-(dt/dx)*(f2[i]-f2[i-1]);
}
h[0]=h[1],q[0]=q[1];
h[ie+2]=h[ie+1],q[ie+2]=q[ie+1];
fprintf(Depths,"\n");
fprintf(Velocities,"\n");
}
fclose (Depths);
fclose (Velocities);

}
thanks for ur responds.
Topic archived. No new replies allowed.