Esempio n. 1
0
/*
Coolects user data for calculating the standard deviation
*/
void program(){
	printf("\nPlease input the value of n (integer)\n");
	int size = 0;
	scanf("%i", &size);
	printf("%i entered \n", size);
	
	my_initialize_heap((int)((size * psize) + msize + psize));
	int* int_array = my_alloc((int)(size * psize));
	
	int i;
	for(i = 0; i < (int)(size); i++){
		int data = 0;
		printf("Please input integer number %d:\n", i);
		scanf("%i", &data);
		*(int_array + i) = (int)data;
	}
	std_deviation(int_array, size);
	free(buffer);
}
int main(void) {

	FILE *fp;	// create file stream
	char filename[] = "outputfile.txt";	// file name here
	// open the file:

	if ((fp = fopen(filename, "w")) == NULL ) {
		printf("I can't open the file for data save !!!\n");
		exit(1);
	}

	/***************** the begining of the GSL part **************************/
	/*kappa_0, kappa_1, kappa_2,  c_0,                  c_H               k_1,     k_3,             k_4, 	                 rho_0 = \tilde{k}_4,                          d_0*/

	struct rparams p =

	{ 0.0, 0.0, 0.0, 0.0 * 0.000075, REDUCTOR_PREFACTOR * 0.001, 0.0 * 1.188,
			0.0387, STALA_DO_A_R * 7.53 * 100000, STALA_DO_B_R * 7.53 * 100
			* REDUCTOR_PREFACTOR, 0.000075 };

	gsl_odeiv2_system sys = { func, NULL, S + 4, &p }; /*gsl_odeiv2_step_rkf45 */
	gsl_odeiv2_driver * d = gsl_odeiv2_driver_alloc_y_new(&sys,
			gsl_odeiv2_step_rk8pd, 1e-16, 1e-16, 0.0);

	int i;
	double t = 0.0;

	double y[S + 4]; /* tablica zmiennych zaleznych (stezenia, momenty) */

	long double stand_M_0;
	long double stand_M_1;
	long double stand_M_2;

	long double aux_stand_M_0;
	long double aux_stand_M_1;
	long double aux_stand_M_2;

	/* S + 4 variables: y[0] = c_p, y[i] = \xi_i, i = 1, ......., S-1, y[S] = M_0, y[S+1] = M_0, y[S+2] = M_2, y[S+3] = c_a */

	/************* zerowanie reczne elem.  ************************/

	for (i = 0; i < S + 4; i++) {
		y[i] = 0.0;

		/*	printf("# y[%d] = % .16Lf", i, y[i]); */
	}

	/*******inicjalizacja tablicy zmiennych ******************/

	y[0] = p.c4; /* c_p(0) = c_0 */
	y[S + 3] = p.c10; /* c_a(0) = d_0 */

	fprintf(fp,
			"# Values of the model parameters: \n#\n# Coagulation kernel parameters: kappa_0 = %.1f, kappa_1 = %.1f, kappa_2 = %.1f \n#\n# Precursors: c_0 = %.10f, d_0 = %.10f, Reductor: c_H = %.10f \n#\n# (exponential notation: c_0 = %.6e, d_0 = %.6e, c_H = %.6e)\n#\n# Reaction rate constants (bare!!): k_1 = %.10f, k_3 = %.10f, k_4 = %.10f, b_R = rho_0 = %.10f \n#\n# (exponential notation: k_1 = %.7e, k_3 = %.7e, k_4 = %.7e) \n#\n",
			p.c1, p.c2, p.c3, p.c4, p.c10, p.c5, p.c4, p.c10, p.c5, p.c6, p.c7,
			p.c8, p.c9, p.c6, p.c7, p.c8);

	fprintf(fp,
			"# A_R = %.1f, B_R = %.1f \n#\n# N = %d, t1 = %.0f s \n#\n# co_ile_krokow_wypisuje = %d  \n#\n# s = %d\n#\n#\n#\n#\n#\n#\n",
			STALA_DO_A_R, STALA_DO_B_R, N, t1, co_ile_krokow_wypisuje, S);

	fprintf(fp,
			"# State parameters: \n# \n#  t            c_p                c_a                 M_0                 M_1                  M_2               M_0^{S}	       M_1^{S}	          M_2^{S}        c_p + c_a + M_1      M_1/M_0     std_dev(size)           xi_1,             xi_2....\n#\n");

	for (i = 1; i <= N; i++) /*N = 100 in the orginal example */
	{

		double ti = i * t1 / N;
		int status = gsl_odeiv2_driver_apply(d, &t, ti, y);

		if (status != GSL_SUCCESS) {
			fprintf(fp,"error, return value=%d\n", status);
			break;
		}

		stand_M_0 = 0.0;
		stand_M_1 = 0.0;
		stand_M_2 = 0.0;

		aux_stand_M_0 = 0.0;
		aux_stand_M_1 = 0.0;
		aux_stand_M_2 = 0.0;

		for (k = 1; k < S; k++) {
			aux_stand_M_0 = aux_stand_M_0 + y[k];
			aux_stand_M_1 = aux_stand_M_1 + (long double) k * y[k];
			aux_stand_M_2 = aux_stand_M_2 + pow((long double) k, 2.0) * y[k];

		}
		stand_M_0 = aux_stand_M_0 + y[S];
		stand_M_1 = aux_stand_M_1 + y[S + 1];
		stand_M_2 = aux_stand_M_2 + y[S + 2];

		if (i % co_ile_krokow_wypisuje == 0) {

			fprintf(fp,"% .4f % .16f % .16f ", t, y[0], y[S + 3]);
			fprintf(fp,"% .16Lf % .16Lf % .16Lf ", stand_M_0, stand_M_1, stand_M_2);
			fprintf(fp,"% .16f % .16f % .16f ", y[S], y[S + 1], y[S + 2]);
			fprintf(fp,"% .12Lf ", y[0] + y[S + 3] + stand_M_1);
			fprintf(fp,"% .12Lf ", mean(stand_M_0, stand_M_1, stand_M_2));
			fprintf(fp,"% .12Lf ", std_deviation(stand_M_0, stand_M_1, stand_M_2));
			/*
			 for(k = 1; k < S ; k++)
			 {
			 printf("% .16f ", y[k]);
			 }
			 */

			for (k = 1; k < S; k = 2 * k) /*printf(" x[%d]=% .16f ", k, y[k]);*/
			{
				fprintf(fp,"% .16f ", y[k]);
			}
			fprintf(fp," \n");

		} /* od 'if' od krokow czasowych */

		if (i == N) {
			fprintf(fp,"# \n");
			fprintf(fp,
					"# Teraz wypisuje asymptotyczne wartosci wszystkich stezen klastrow \n");
			for (k = 1; k < S; k++) /*printf(" x[%d]=% .16f ", k, y[k]);*/
			{
				fprintf(fp,"#7# %d  % .16f \n", k, y[k]);
			}
			fprintf(fp,"#M_0=#  % .16f \n", y[S]);
		}

	} /* for po 'i', glowna petla (kroki czasowe) */

	gsl_odeiv2_driver_free(d);

	/***************** the end of the GSL part **************************/

	fprintf(fp,
			"# State parameters: \n# \n#  t            c_p                c_a                 M_0                 M_1                  M_2               M_0^{S}	       M_1^{S}	          M_2^{S}        c_p + c_a + M_1      M_1/M_0     std_dev(size)           xi_1,             xi_2....\n#\n");

	// close file:
	fclose(fp);
	return 0;
} /*koniec funkcji main*/