Exemple #1
0
void one_step()
{
    
    printf("provaonestep");
    
    
    
	switch(method){
  	case 1:
		euler();
    	break;
  	case 2:
		rk2();
    	break;
  	case 3:
		rk4();
    	break;
	}
}
Exemple #2
0
int main(){
  int i;

  float h = (XMAX - XMIN)/(1.0*NSTEP); /* stepsize for integration */
  float yin, yout;             /* value of y before and after a step */
  float x[NSTEP+1], y[NSTEP+1]; 

  for (i = 0; i <= NSTEP; i++) /* Define array of x values */
    x[i] = XMIN + h * i;
 
  y[0] = Y0; /* initial value */

  printf("%f %f\n", x[0], y[0]);
  for (i = 0; i < NSTEP; i++){ 
    yin = y[i];
    yout = rk2(x[i], yin, h); /* do one integration step */
    y[i+1] = yout; 
   
    printf("%f %f\n", x[i+1], y[i+1]);
  }

  return 0;
}
main()
{
 
	int M = 2; 
// specify the no. of variables
 
	double t, x[M], h, tn, t0, en; 
// x is an array of size M
 
	void derivs(); 
// derivs computes the derivatives
 
	int n, i, j, k;

	double x_max[3] = {0.1, 1, 10};
// maximum amplitudes which will be applied to rk2

for (k = 0; k < 3; k++)
{
	x[0] = x_max[k];
	//implements maximum amplitude

	printf("\nMaximum amplitude: %.2f\n", x[0]);
	printf(" Time   Position  Momentum\n");
	
	x[1] = 0; //initializes p = 0
	t0 = 0; 
	n = 50000; //number of steps
	h = .001; //length of each step
	t = t0;

	/*loop below identifies where the first root of the oscillating
	function is located, which can then be multiplied by four to find
	the length of its period.*/

	for (i = 0; i < n; i++)
	{
		rk2(t, x, derivs, h, M); 
		// Call RK2
		if (x[0] >= 0)
		{
			t += h;
		}
		else
			break;
	}

	/*now that the period is found, can define tn and subsequently h*/

	tn = 4 * t;
	n = 50;
	t0 = 0;
	h = (tn - t0) / n;
	t = t0;

	for (j = 0; j < n; j++)
	{
		//Calls rk2
		rk2(t, x, derivs, h, M);
		//increments time by h until full period is reached
		t += h;
		printf("%.3f   %.3f   %.3f\n", t, x[0], x[1]);
 	}
}

return 0;
}
Exemple #4
0
double integrate(void){
	FILE *out;
	
	/*Initial Values*/
	double  t = 0.0;
	double vx = 0.0;
	double vy = -1.0;
	double  x = 1.0;
	double  y = 0.0;

	double x_new, y_new, vx_new, vy_new;
	double E0, E, dE;
	double L0, L, dL;
	double l2sq = 0.0;

	int i;

	if (method == 0){
		out = fopen("gravityEuler.txt", "w");
	} else if (method == 1){
		out = fopen("gravityRK2.txt", "w");
	} else if (method == 2){
		out = fopen("gravityRK4.txt", "w");
	}
	
	/* Initial values of energy (E =0.5 * m (vx^2 + vy^2) + GM/r) and angular momentum
	 * (L = r x p = x*m*vy - y*m*vx). Note that the mass of the particle m=1. */
	E0 = -0.5; 
	L0 = 1.0;

	/* Now begins the actual integrating. Depending on the method called, this function
	 * uses any of the 3 methods mentioned above, as well as calculating the energy and
	 * angular momentum at each time step to be used in calculating the error over time.*/
	for (i = 0; i < N; i++){
	
		E = 0.5*(pow(vx,2) + pow(vy,2)) - GM/( pow( pow(x,2) + pow(y,2), 0.5));
		dE = fabs((E - E0) / E0);

		L = x * vy - y * vx;
		dL = fabs((L - L0) / L0);

	/*	printf("%f %f %f %f %f %f %f %d\n",t, x, y, vx, vy, dE, dL, i);  */
		fprintf(out, "%f %25.16f %25.16f %25.16f %25.16f %25.16f %25.16f %d\n",t, x, y, vx, vy, dE, dL, i);

		if (method == 0){
			euler(t, x, y, vx, vy, &x_new, &y_new, &vx_new, &vy_new);
		} else if (method == 1){
			rk2(t, x, y, vx, vy, &x_new, &y_new, &vx_new, &vy_new);
		} else if (method == 2){
			rk4(t, x, y, vx, vy, &x_new, &y_new, &vx_new, &vy_new);
		}
		
		if (t > T){
			break;
		}
		
		/*Find the L2 norm as a function of step size, h.
		 * L2 = sqrt(int_(0)^(T) || r_num(t) - r_real(t)||^2 dt)
		 * r_real(t) = 1 for circular orbit with initial conditions
		 * r0 = (1,0), v0 = (0,1).
		 */  

		double r_num =  pow( (pow(x,2) + pow(y,2)) , 0.5);
		l2sq += fabs( pow((r_num - 1), 2)) *dt; 
	

		t  = t+dt;
		x  = x_new;
		y  = y_new;
		vx = vx_new;
		vy = vy_new;
	}
	fclose(out);
	return pow(l2sq, 0.5);
}
void Coagulation::coag_simulate()
{
	double coag_cur_state[num_factors] = {};
	double coag_next_state[num_factors] = {};
	double dt[] = { 1.0e-10, 1.0e-8, 1.0e-5 };
	double t_max[] = { 1.0e-8, 1.0e-6, 1.0e0 };
	int incr[] = { 10, 10, 100 };
	double t = 0.0;
	int c = 0;

	Initialize(coag_cur_state);

	//sw.WriteLine("time" + ',' + "XII" + ',' + " XIIa" + ',' + " VIII" + ',' + " VIIIa" + ',' + " IX" + ',' + " IXa" + ',' + " XI" + ',' + " XIa" + ',' + " VII" + ',' + " VIIa" + ',' + " X" + ',' + " Xa" + ',' + "V" + ',' + " Va" + ',' + " Xa_Va" + ',' + " II" + ',' + " IIa" + ',' + " TAT" + ',' + " Fg" + ',' + " F" + ',' + " XF" + ',' + " FDP" + ',' + " D" + ',' + " XIII" + ',' + "XIIIa" + ',' + "Pg" + ',' + "P" + ',' + "PC" + ',' + " APC" + ',' + " Tmod" + ',' + " IIa_Tmod" + ',' + " IXa_VIIIa" + ',' + " TF" + ',' + " VII_TF" + ',' + " VIIa_TF" + ',' + "TFPI" + ',' + "Xa_TFPI" + ',' + " VIIa_TF_Xa_TFPI" + ',' + " PS" + ',' + " APC_PS" + ',' + "Pk" + ',' + "K" + ',' + "VK" + ',' + "VKH2" + ',' + "VKO" + ',' + "VK_p" + ',' + "Awarf" + ',' + "Cwarf" + ',' + "ATIII_Heparin" + ',' + "DP");
	for (size_t i = 0; i < ARRAY_SIZE(dt); i++)
	{
		while (t < t_max[i])
		{
			rk2(coag_next_state, coag_cur_state, dt[i], coag_ode);
			memcpy(coag_cur_state, coag_next_state, sizeof(coag_cur_state));
			t += dt[i];
			c++;

			if (c == incr[i])
			{
				create_output(coag_cur_state, t);
				c = 0;
			}
		}
	}

	std::cout << std::setprecision(15);
	std::cout <<"Fg: " << coag_cur_state[Fg]<< std::endl;
	std::cout <<"F: " << coag_cur_state[F]<< std::endl;
	std::cout <<"XF: " << coag_cur_state[XF]<< std::endl;
	std::cout <<"DegProd: " << coag_cur_state[DegProd]<< std::endl;
	std::cout <<"II: " << coag_cur_state[II]<< std::endl;
	std::cout <<"IIa: " << coag_cur_state[IIa]<< std::endl;
	std::cout <<"IIa_Tmod: " << coag_cur_state[IIa_Tmod] << std::endl;
	std::cout <<"PC: " << coag_cur_state[PC]<< std::endl;
	std::cout <<"APC: " << coag_cur_state[APC]<< std::endl;
	std::cout <<"PS: " << coag_cur_state[PS]<< std::endl;
	std::cout <<"APC_PS: " << coag_cur_state[APC_PS]<< std::endl;
	std::cout <<"TAT: " << coag_cur_state[TAT]<< std::endl;
	std::cout <<"Pk: " << coag_cur_state[Pk]<< std::endl;
	std::cout <<"K: " << coag_cur_state[K]<< std::endl;
	std::cout <<"VII: " << coag_cur_state[VII]<< std::endl;
	std::cout <<"VII_TF: " << coag_cur_state[VII_TF]<< std::endl;
	std::cout <<"VIIa: " << coag_cur_state[VIIa]<< std::endl;
	std::cout <<"VIIa_TF: " << coag_cur_state[VIIa_TF]<< std::endl;
	std::cout <<"VIII: " << coag_cur_state[VIII]<< std::endl;
	std::cout <<"VIIIa: " << coag_cur_state[VIIIa]<< std::endl;
	std::cout <<"IX: " << coag_cur_state[IX]<< std::endl;
	std::cout <<"IXa: " << coag_cur_state[IXa]<< std::endl;
	std::cout <<"IXa_VIIIa: " << coag_cur_state[IXa_VIIIa]<< std::endl;
	std::cout <<"X: " << coag_cur_state[X]<< std::endl;
	std::cout <<"Xa: " << coag_cur_state[Xa]<< std::endl;
	std::cout <<"Xa_Va: " << coag_cur_state[Xa_Va]<< std::endl;
	std::cout <<"Xa_TFPI: " << coag_cur_state[Xa_TFPI]<< std::endl;
	std::cout <<"VIIa_TF_Xa_TFPI: " << coag_cur_state[VIIa_TF_Xa_TFPI]<< std::endl;
	std::cout <<"XI: " << coag_cur_state[XI]<< std::endl;
	std::cout <<"XIa: " << coag_cur_state[XIa]<< std::endl;
	std::cout <<"XII: " << coag_cur_state[XII]<< std::endl;
	std::cout <<"XIIa: " << coag_cur_state[XIIa]<< std::endl;
	std::cout << std::endl;
	std::cout <<"TF: " << coag_cur_state[TF]<< std::endl;
	std::cout <<"CA: " << coag_cur_state[CA]<< std::endl;
}