You are on page 1of 8

test_particle_EyBz-1.

cpp 4/6/17, 11:32 AM

#include<fstream>
#include<cstdlib>
#include<cstdio>
#include<cmath>

#define mdim 6 // parameter(mdim=6)


double Ey0, Bz0; // common /BE/Ey0,Bz0

void getBE(double , double , double , double , double [], double []);


void funyp_nonRel(double , double [], double []);
void EUDE1st(void(*)(double , double [], double []), double& , double [],
double [], double [], double , int );
void RKDE2nd(void(*)(double , double [], double []), double& , double [],
double [], double [], double , int , double []);
void RKDE4th(void(*)(double , double [], double []), double& , double [],
double [], double [], double , int , double []);

//C=======================================================================
//C23456789012345678901234567890123456789012345678901234567890123456789012
//C***********************************************************************
//C test_particle_EyBz.f : ( main & funyp_nonRel & getBE )
//C***********************************************************************
//C ExB test particle simulation --- Case 1
int main(void){
// Program testCS1
// implicit double precision (a-h, o-z)
// parameter(mdim=6)
// dimension yin(mdim),yout(mdim),yp(mdim),ywork(mdim)
// external funyp_nonRel
// common /BE/Ey0,Bz0
// namelist /iniXVBE/x0,y0,z0,v0,theta0,dt,nend,tplot0,Ey0,Bz0
double yin[mdim],yout[mdim],yp[mdim],ywork[mdim];
double a0=0, a1=1, a10=10, pi, pi180, RELERR, ABSERR;
double x0, y0, z0, v0, theta0, dt, tplot0;
double vx0, vy0, vz0, dth, t0, t, tplot, V;
int nend, k, j, i, ndim;
//
// print *,'enter x0,y0,z0,v0,theta0,dt,nend,tplot0,Ey0,Bz0'
// print *,' Total simulation time=nend*tplot0'
// read(5,*) x0,y0,z0,v0,theta0,dt,nend,tplot0,Ey0,Bz0
printf("enter x0,y0,z0,v0,theta0,dt,nend,tplot0,Ey0,Bz0 \n");
printf("Total simulation time=nend*tplot0 \n");
scanf("%lg %lg %lg %lg %lg %lg %d %lg %lg %lg",
&x0, &y0, &z0, &v0, &theta0, &dt, &nend, &tplot0, &Ey0, &Bz0);
//#include "test_particle_EyBz-read.cpp"
FILE *iniXVBE, *fort11;
iniXVBE = fopen("fort31.txt","w");
fort11 = fopen("fort11.txt","w");
fprintf(iniXVBE, "x0,y0,z0,v0,theta0,dt,nend,tplot0,Ey0,Bz0= %g %g %g %g
%g %g %d %g %g %g \n",
x0,y0,z0,v0,theta0,dt,nend,tplot0,Ey0,Bz0);
fclose(iniXVBE);
// write(31,iniXVBE)
// write(11,30)
// 30 format(' time x y z Vx Vy Vz
V')
fprintf(fort11, " time x y z Vx Vy
Vz V \n");
//
ndim=mdim; // ndim=mdim

Page 1 of 3
test_particle_EyBz-1.cpp 4/6/17, 11:32 AM

pi=atan(a1)*4; // pi=datan(1.d00)*4
pi180=pi/180; // pi180=pi/180.d00
// a0=0
vz0=0; // vz0=0
vx0=v0*cos(theta0*pi180); // vx0=v0*dcos(theta0*pi180)
vy0=v0*sin(theta0*pi180); // vy0=v0*dsin(theta0*pi180)
dth=dt/2; // dth=dt*0.5d0
t0=0; // t0=0
yin[0]=x0; // yin(1)=x0
yin[1]=y0; // yin(2)=y0
yin[2]=z0; // yin(3)=z0
yin[3]=vx0; // yin(4)=vx0
yin[4]=vy0; // yin(5)=vy0
yin[5]=vz0; // yin(6)=vz0
t=t0; // t=t0
k=0; // k=0
RELERR=pow(a10,-10); // RELERR=1.0D-10
ABSERR=pow(a10,-9); // ABSERR=1.0D-09
for(j=0; j<nend; j++){ // do j=1,nend
tplot=tplot0*(j+1); // tplot=tplot0*j
//C--------------
continue1: // 1 continue
RKDE4th((* funyp_nonRel),t,yin,yout,yp,dt,ndim,ywork);
// call RKDE4th(funyp_nonRel,t,yin,yout,yp,dt,ndim,ywork)
//C call RKDE2nd(funyp_nonRel,t,yin,yout,yp,dt,ndim,ywork)
//C call EUDE1st(funyp_nonRel,t,yin,yout,yp,dt,ndim)
//C
//C Check stagnation point
//C
for(i=0; i<ndim; i++){ // do i=1,ndim
if(fabs(yout[i]-yin[i])>RELERR*fabs(yin[i])) {goto continue10;}
//if(dabs(yout(i)-yin(i)).gt.RELERR*dabs(yin(i))) go to 10
} // enddo
printf("program stop due to dY/dt=0 at stagnation point \n");
fclose(fort11);
return 1;
//print *,'program stop due to dY/dt=0 at stagnation point'
//pause
//stop
//C
continue10: // 10 continue
for(i=0; i<ndim; i++){ // do i=1,ndim
yin[i]=yout[i]; // yin(i)=yout(i)
} // enddo
if(fabs(t-tplot)>dth) {goto continue1;}
//if(dabs(t-tplot).gt.dth) go to 1
//C
//C Out put Y
//C
V=sqrt(yout[3]*yout[3]+yout[4]*yout[4]+yout[5]*yout[5]);
//V=dsqrt(yout(4)**2+yout(5)**2+yout(6)**2)
//C Write(11,11) t,yout, V
// 11 format((8(1x,F15.8)))
fprintf(fort11, "%f %f %f %f %f %f %f %f \n",
t, yout[0], yout[1], yout[2], yout[3], yout[4], yout[5], V)
;
} // enddo
fclose(fort11);
return 0; // stop
} // end

Page 2 of 3
test_particle_EyBz-1.cpp 4/6/17, 11:32 AM

//C-----------------------------------------------------------------------
//C Sub. funyp_nonRel
//C-----------------------------------------------------------------------
//C ytotal=[x,y,z,vx,vy,vz]
//C yptotal=d(ytotal)/dt
//C dv/dt = E + v cross B
//C
void funyp_nonRel(double t, double ytotal[], double yptotal[]){
double x, y, z, vx, vy, vz, Ex, Ey, Ez, Bx, By, Bz;
double E[3], B[3];
// subroutine funyp_nonRel(t,ytotal,yptotal)
// parameter(mdim=6)
// implicit double precision (a-h, o-z)
// dimension ytotal(mdim),yptotal(mdim)
x=ytotal[0]; // x=ytotal(1)
y=ytotal[1]; // y=ytotal(2)
z=ytotal[2]; // z=ytotal(3)
vx=ytotal[3]; // vx=ytotal(4)
vy=ytotal[4]; // vy=ytotal(5)
vz=ytotal[5]; // vz=ytotal(6)
//C q=1
//C m=1
getBE(x,y,z,t, B, E); // call getBE(x,y,z,t,Bx,By,Bz,Ex,Ey,Ez)
Bx=B[0];By=B[1];Bz=B[2];Ex=E[0];Ey=E[1];Ez=E[2];
yptotal[0]=vx; // yptotal(1)=vx
yptotal[1]=vy; // yptotal(2)=vy
yptotal[2]=vz; // yptotal(3)=vz
yptotal[3]=Ex+vy*Bz-vz*By; // yptotal(4)=Ex+vy*Bz-vz*By
yptotal[4]=Ey+vz*Bx-vx*Bz; // yptotal(5)=Ey+vz*Bx-vx*Bz
yptotal[5]=Ez+vx*By-vy*Bx; // yptotal(6)=Ez+vx*By-vy*Bx
} // end
//C-----------------------------------------------------------------------
//C-----------------------------------------------------------------------
//C Sub. getBE
//C
void getBE(double x, double y, double z, double t,
double B[], double E[]){
// subroutine getBE(x,y,z,t,Bx,By,Bz,Ex,Ey,Ez)
// implicit double precision (a-h, o-z)
// common /BE/Ey0,Bz0
B[0]=0; // Bx=0
B[1]=0; // By=0
B[2]=Bz0; // Bz=Bz0
E[0]=0; // Ex=0
E[1]=Ey0; // Ey=Ey0
E[2]=0; // Ez=0
// return
} // end
//c-------------------------------
#include "dODE124.cpp" // Include 'dODE124.f'
//C-----------------------------------------------------------------------

Page 3 of 3
dODE124.cpp 4/6/17, 11:33 AM

//void EUDE1st(void(*)(double , double [], double []), double& , double [],


double [], double [], double , int );
//void RKDE2nd(void(*)(double , double [], double []), double& , double [],
double [], double [], double , int , double []);
//void RKDE4th(void(*)(double , double [], double []), double& , double [],
double [], double [], double , int , double []);

//C=======================================================================
//C23456789012345678901234567890123456789012345678901234567890123456789012
//C***********************************************************************
//C dODE124.f : ( EUDE1st & RKDE2nd & RKDE4th )
//C***********************************************************************
//C Sub. EUDE1st
//C-----------------------------------------------------------------------
//C Euler method for solving system ODEs dy/dt=yp (based on funyp)
// t : input and output time (t=t+dt)
// yin[ndim] : input array y at time=t
// yout[ndim] : output array y at time=t+dt
// yp[ndim] : working array (dy/dt) of EUDE1st (output array of funyp)
// dt : input, size of time step
// ndim : array size
//C-----------------------------------------------------------------------
void EUDE1st(void(*funyp)(double ,double [], double []), double& t,
double yin[], double yout[], double yp[], double dt, int ndim){
// subroutine EUDE1st(funyp,t,yin,yout,yp,dt,ndim)
// implicit double precision (a-h, o-z)
// dimension yin(1),yout(1),yp(1)
// external funyp
//C------------------------------------------------------------------------
int i;
funyp(t,yin,yp); // call funyp(t,yin,yp)
for (i=0;i<ndim;i++){ // do i=1,ndim
yout[i]=yin[i]+dt*yp[i]; // yout(i)=yin(i)+dt*yp(i)
} // enddo
t=t+dt; // t=t+dt
// return
} // end
//C-----------------------------------------------------------------------
//C-----------------------------------------------------------------------
//C Sub. RKDE2nd
//C-----------------------------------------------------------------------
//C Second order Runge-Kutta method for solving system ODEs dy/dt=yp
//C (based on funyp)
// t : input and output time (t=t+dt)
// yin[ndim] : input array y at time=t
// yout[ndim] : output array y at time=t+dt
// yp[ndim] : working array (dy/dt) of RKDE2nd (output array of funyp)
// dt : input, size of time step
// ndim : array size
// ywork : working array
//C-----------------------------------------------------------------------
void RKDE2nd(void(*funyp)(double ,double [], double []), double& t,
double yin[], double yout[], double yp[], double dt, int ndim,
double ywork[]){
// subroutine RKDE2nd(funyp,t,yin,yout,yp,dt,ndim,ywork)
// implicit double precision (a-h, o-z)
// dimension yin(1),yout(1),yp(1),ywork(1)
// external funyp
//C------------------------------------------------------------------------
int i;

Page 1 of 3
dODE124.cpp 4/6/17, 11:33 AM

double ah=0.5, twork; // ah=0.5d00


//c
funyp(t,yin,yp); // call funyp(t,yin,yp)
for (i=0;i<ndim;i++){ // do i=1,ndim
ywork[i]=yin[i]+dt*ah*yp[i]; // ywork(i)=yin(i)+dt*ah*yp(i)
} // enddo
//c
twork=t+dt*ah; // twork=t+dt*ah
funyp(twork,ywork,yp); // call funyp(twork,ywork,yp)
for (i=0;i<ndim;i++){ // do i=1,ndim
yout[i]=yin[i]+dt*yp[i]; // yout(i)=yin(i)+dt*yp(i)
} // enddo
//c
t=t+dt; // t=t+dt
// return
} // end
//C-----------------------------------------------------------------------
//C-----------------------------------------------------------------------
//C Sub. RKDE4th
//C-----------------------------------------------------------------------
//C Fourth order Runge-Kutta method for solving system ODEs dy/dt=yp
//C (based on funyp)
// t : input and output time (t=t+dt)
// yin[ndim] : input array y at time=t
// yout[ndim] : output array y at time=t+dt
// yp[ndim] : working array (dy/dt) of RKDE4th (output array of funyp)
// dt : input, size of time step
// ndim : array size
// ywork : working array
//C-----------------------------------------------------------------------
void RKDE4th(void(*funyp)(double ,double [], double []), double& t,
double yin[], double yout[], double yp[], double dt, int ndim,
double ywork[]){
// subroutine RKDE4th(funyp,t,yin,yout,yp,dt,ndim,ywork)
// implicit double precision (a-h, o-z)
// dimension yin(1),yout(1),yp(1),ywork(1)
// external funyp
//C------------------------------------------------------------------------
int i;
double ah=0.5, twork; // ah=0.5d00
double a1v3=1.0/3; // a1v3=1.d00/3.d00
double a1v6=1.0/6; // a1v6=1.d00/6.d00
for(i=0; i<ndim; i++){ // do i=1,ndim
yout[i]=yin[i]; // yout(i)=yin(i)
} // enddo
//c
funyp(t,yin,yp); // call funyp(t,yin,yp)
for(i=0; i<ndim; i++){ // do i=1,ndim
ywork[i]=yin[i]+dt*ah*yp[i]; // ywork(i)=yin(i)+dt*ah*yp(i)
yout[i]=yout[i]+dt*a1v6*yp[i];// yout(i)=yout(i)+dt*a1v6*yp(i)
} // enddo
//c
twork=t+dt*ah; // twork=t+dt*ah
funyp(twork,ywork,yp); // call funyp(twork,ywork,yp)
for(i=0; i<ndim; i++){ // do i=1,ndim
ywork[i]=yin[i]+dt*ah*yp[i]; // ywork(i)=yin(i)+dt*ah*yp(i)
yout[i]=yout[i]+dt*a1v3*yp[i];// yout(i)=yout(i)+dt*a1v3*yp(i)
} // enddo
//c
//c twork=t+dt*ah

Page 2 of 3
dODE124.cpp 4/6/17, 11:33 AM

funyp(twork,ywork,yp); // call funyp(twork,ywork,yp)


for(i=0; i<ndim; i++){ // do i=1,ndim
ywork[i]=yin[i]+dt*yp[i]; // ywork(i)=yin(i)+dt*yp(i)
yout[i]=yout[i]+dt*a1v3*yp[i];// yout(i)=yout(i)+dt*a1v3*yp(i)
} // enddo
//c
twork=t+dt; // twork=t+dt
funyp(twork,ywork,yp); // call funyp(twork,ywork,yp)
for(i=0; i<ndim; i++){ // do i=1,ndim
yout[i]=yout[i]+dt*a1v6*yp[i];// yout(i)=yout(i)+dt*a1v6*yp(i)
} // enddo
//c
t=t+dt; // t=t+dt
// return
} // end
//C-----------------------------------------------------------------------

Page 3 of 3
test_io.cpp 4/6/17, 11:33 AM

//C-----------------------------
//C test_io.cpp
//C-----------
//#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
//C==============================

int main ()
{
FILE * pFile;
char buffer[] = { 'x' , 'y' , 'z' };
double aa[5]={2.,1.,3.,4.,5.}, a[5];
float a21, a22;
double a31, a32;
int ia4;
unsigned i8=8, i5=5;
//
//std format input/output
//
printf("enter int ia4 \n" );
scanf("%d", &ia4 );
printf("a4= %d \n", ia4 );

printf("enter float a21, a22 \n" );


scanf("%g %g", &a21, &a22 ); //good for "float " , but no good for
"double "
//scanf("%f", &a21 ); //no good for both "float " and "double
"
printf("a21, a22= %g %g\n", a21, a22 );

printf("enter double a31, a32 \n" );


scanf("%lg %lg", &a31, &a32 ); //good for "double "
printf("a31, a32= %g %g\n", a31, a32 );

printf("enter double array a[] \n" );


scanf("%lg %lg %lg %lg %lg", &a[0], &a[1], &a[2], &a[3], &a[4] ); //
good for "double "
printf("input a[1],a[2], a[3]= %g %g %g \n", a[0],a[1],a[2] );
//
//binary input/output
//
pFile = fopen ("myfile.bin", "wb");
fwrite (buffer , sizeof(char), sizeof(buffer), pFile);
fclose (pFile);

FILE *iniXVBE, *fort11;


iniXVBE = fopen("fort31.txt","wb");
fort11 = fopen("fort11.txt","w");
fwrite(aa,i8,i5,iniXVBE);
fclose(iniXVBE);
iniXVBE = fopen("fort31.txt","rb");
fread(a,i8,i5,iniXVBE);
//fprintf(iniXVBE, "%a \n", *aa );
fprintf(fort11, "%f %f %f \n", a[0],a[1],a[2] );

fclose(iniXVBE);
fclose(fort11);

Page 1 of 2
test_io.cpp 4/6/17, 11:33 AM

return 0;
}

Page 2 of 2

You might also like