commit d856e07363afbd999ce4a98ccd109a86a13e45f7
parent eff39df93aa04c4858b33363827bd7b27fa607aa
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date:   Sat,  6 Jul 2019 10:11:07 +0200
Use general functions for producing output to stdout and files
Diffstat:
5 files changed, 80 insertions(+), 38 deletions(-)
diff --git a/arrays.c b/arrays.c
@@ -217,3 +217,27 @@ copy_values(const double* in, double* out, const int n)
 	for (i=0; i<n; ++i)
 		out[i] = in[i];
 }
+
+double*
+copy(const double* in, const int n)
+{
+	double *out;
+	out = empty(n);
+	copy_values(in, out, n);
+	return out;
+}
+
+double*
+normalize(const double* in, const int n)
+{
+	int i;
+	double max_val;
+	double *out;
+
+	out = malloc(n*sizeof(double));
+	copy_values(in, out, n);
+	max_val = max(out, n);
+	for (i=0; i<n; ++i)
+		out[i] /= max_val;
+	return out;
+}
diff --git a/arrays.h b/arrays.h
@@ -48,5 +48,7 @@ void fprint_three_arrays(
         const int n);
 
 void copy_values(const double* in, double* out, const int n);
+double* copy(const double* in, const int n);
+double* normalize(const double* in, const int n);
 
 #endif
diff --git a/main.c b/main.c
@@ -91,16 +91,16 @@ version(void)
 int
 main(int argc, char* argv[])
 {
-	int normalize, opt, i;
+	int norm, opt, i;
 	struct simulation sim;
 	const char* optstring;
 	unsigned long iter;
-	double new_phi, new_k, filetimeclock, max_v_x;
+	double new_phi, new_k, filetimeclock;
 
 	/* load with default values */
 	sim = init_sim();
 
-	normalize = 0;
+	norm = 0;
 
 	optstring = "hvNn:G:P:m:V:A:b:f:Fp:d:r:o:L:c:i:R:k:O:a:q:H:t:T:D:I:";
 	const struct option longopts[] = {
@@ -157,7 +157,7 @@ main(int argc, char* argv[])
 				sim.nz = atoi(optarg);
 				break;
 			case 'N':
-				normalize = 1;
+				norm = 1;
 				break;
 			case 'G':
 				sim.G = atof(optarg);
@@ -289,26 +289,15 @@ main(int argc, char* argv[])
 		iter++;
 
 		if (filetimeclock >= sim.file_dt || iter == 0) {
-			write_output_file(&sim, normalize);
+			write_output_file(&sim, norm);
 			filetimeclock = 0.0;
 		}
 	}
 
-	if (normalize) {
-		max_v_x = max(sim.v_x, sim.nz);
-		for (i=0; i<sim.nz; ++i)
-			sim.v_x[i] /= max_v_x;
-	}
-
 	if (sim.fluid)
-		for (i=0; i<sim.nz; ++i)
-			printf("%.17g\t%.17g\t%.17g\t%.17g\n",
-			        sim.z[i],
-			        sim.v_x[i],
-			        sim.sigma_n_eff[i],
-			        sim.p_f_ghost[idx1g(i)]);
+		print_wet_output(stdout, &sim, norm);
 	else
-		print_three_arrays(sim.z, sim.v_x, sim.sigma_n_eff, sim.nz);
+		print_dry_output(stdout, &sim, norm);
 
 	free_arrays(&sim);
 	return 0;
diff --git a/simulation.c b/simulation.c
@@ -437,34 +437,59 @@ implicit_1d_jacobian_poisson_solver(struct simulation* sim,
 void
 write_output_file(struct simulation* sim, const int normalize)
 {
-	int i;
 	char outfile[200];
 	FILE *fp;
-	double *v_x_out;
-	double max_v_x;
 
 	sprintf(outfile, "%s.output%05d.txt", sim->name, sim->n_file++);
 
-	v_x_out = malloc(sim->nz*sizeof(double));
-	copy_values(sim->v_x, v_x_out, sim->nz);
-	if (normalize) {
-		max_v_x = max(v_x_out, sim->nz);
-		for (i=0; i<sim->nz; ++i)
-			v_x_out[i] /= max_v_x;
-	}
-
 	fp = fopen(outfile, "w");
 	if (sim->fluid)
-		for (i=0; i<sim->nz; ++i)
-			fprintf(fp, "%.17g\t%.17g\t%.17g\t%.17g\t%.17g\n",
-			        sim->z[i],
-			        v_x_out[i],
-			        sim->sigma_n_eff[i],
-			        sim->p_f_ghost[idx1g(i)],
-			        sim->mu[i]);
+		print_wet_output(fp, sim, normalize);
 	else
-		fprint_three_arrays(fp, sim->z, v_x_out, sim->sigma_n_eff, sim->nz);
+		print_dry_output(fp, sim, normalize);
 
-	free(v_x_out);
 	fclose(fp);
 }
+
+void
+print_dry_output(FILE* fp, struct simulation* sim, const int norm)
+{
+	int i;
+	double *v_x_out;
+
+	if (norm)
+		v_x_out = normalize(sim->v_x, sim->nz);
+	else
+		v_x_out = copy(sim->v_x, sim->nz);
+
+	for (i=0; i<sim->nz; ++i)
+		fprintf(fp, "%.17g\t%.17g\t%.17g\n",
+		        sim->z[i],
+		        v_x_out[i],
+		        sim->sigma_n_eff[i]);
+
+	free(v_x_out);
+}
+
+void
+print_wet_output(FILE* fp, struct simulation* sim, const int norm)
+{
+	int i;
+	double *v_x_out;
+
+	if (norm)
+		v_x_out = normalize(sim->v_x, sim->nz);
+	else
+		v_x_out = copy(sim->v_x, sim->nz);
+
+	for (i=0; i<sim->nz; ++i)
+		fprintf(fp, "%.17g\t%.17g\t%.17g\t%.17g\t%.17g\t%.17g\n",
+		        sim->z[i],
+		        v_x_out[i],
+		        sim->sigma_n_eff[i],
+		        sim->p_f_ghost[idx1g(i)],
+		        sim->mu[i],
+		        sim->gamma_dot_p[i]);
+
+	free(v_x_out);
+}
diff --git a/simulation.h b/simulation.h
@@ -124,5 +124,7 @@ int implicit_1d_jacobian_poisson_solver(
         const double rel_tol);
 
 void write_output_file(struct simulation* sim, const int normalize);
+void print_dry_output(FILE* fp, struct simulation* sim, const int normalize);
+void print_wet_output(FILE* fp, struct simulation* sim, const int normalize);
 
 #endif