Example #1
0
int main(int argc, char *argv[])
{
	exit_if_false(argc == 2,"exactly one input argument required");

	timer_start();

	//-------------------------------------------------------------------//
	
	// counters
	int i, n;

	// file paths
	char *input_file_path, *geometry_file_path, *case_file_path, *data_file_path, *data_numbered_file_path, *display_file_path, *display_numbered_file_path;
	exit_if_false(geometry_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating geometry file path");
	exit_if_false(case_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating case file path");
	exit_if_false(data_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating data file path");
	exit_if_false(data_numbered_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating data numbered file path");
	exit_if_false(display_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating display file path");
	exit_if_false(display_numbered_file_path = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating display numbered file path");

	// print string
	char *print;
	exit_if_false(print = (char *)malloc(MAX_STRING_LENGTH * sizeof(char)),"allocating the print string");

	// mesh structures
	int n_nodes, n_faces, n_elements, n_boundaries_old = 0, n_boundaries, n_terms;
	struct NODE *node;
	struct FACE *face;
	struct ELEMENT *element;
	struct BOUNDARY *boundary_old = NULL, *boundary;
	struct TERM *term;
	EXPRESSION *initial = NULL;
	
	// solution vectors
	int n_u_old = 0, n_u;
	double *u_old = NULL, *u;

	// files
	FILE *input_file, *case_file, *data_file, *geometry_file, *display_file;

	//-------------------------------------------------------------------//

	// opening the input file
	print_info("opening the input file %s",argv[1]);
	input_file_path = argv[1];
	input_file = fopen(input_file_path,"r");
	exit_if_false(input_file != NULL,"opening %s",input_file_path);

	// reading the case file path
	print_info("reading the case file path");
	exit_if_false(fetch_value(input_file,"case_file_path",'s',case_file_path) == FETCH_SUCCESS,"reading case_file_path from %s",input_file_path);
	print_continue(case_file_path);

	// reading the number of variables, variable names and orders
	print_info("reading the variables");
	int n_variables_old = 0, n_variables;
	exit_if_false(fetch_value(input_file,"number_of_variables",'i',&n_variables) == FETCH_SUCCESS,"reading number_of_variables from %s",input_file_path);
	int *variable_order_old = NULL, *variable_order = (int *)malloc(n_variables * sizeof(int));
	exit_if_false(variable_order != NULL,"allocating orders");
	exit_if_false(fetch_vector(input_file, "variable_order", 'i', n_variables, variable_order) == FETCH_SUCCESS,"reading variable_order from %s",input_file_path);
	char **variable_name = allocate_character_matrix(NULL,n_variables,MAX_STRING_LENGTH);
	exit_if_false(variable_name != NULL,"allocating variable names");
	warn_if_false(fetch_vector(input_file,"variable_name",'s',n_variables,variable_name) == FETCH_SUCCESS,"reading variable_name from %s",input_file_path);
	for(i = 0; i < n_variables; i ++) print_continue("%s order %i",variable_name[i],variable_order[i]);

	// reading the number of inner and outer iterations to perform
	print_info("reading the numbers of iterations");
	int outer_iteration = 0, inner_iteration;
	int n_outer_iterations, n_inner_iterations, data_n_outer_iterations, display_n_outer_iterations;
	exit_if_false(fetch_value(input_file,"number_of_inner_iterations",'i',&n_inner_iterations) == FETCH_SUCCESS,"reading number_of_inner_iterations from %s",input_file_path);
	exit_if_false(fetch_value(input_file,"number_of_outer_iterations",'i',&n_outer_iterations) == FETCH_SUCCESS,"reading number_of_outer_iterations from %s",input_file_path);
	print_continue("%i outer of %i inner iterations to be done",n_outer_iterations,n_inner_iterations);

	// read existing case and data
	case_file = fopen(case_file_path,"r");
	if(case_file != NULL)
	{
		print_info("reading existing case file %s",case_file_path);
		read_case(case_file, &n_variables_old, &variable_order_old, &n_nodes, &node, &n_faces, &face, &n_elements, &element, &n_boundaries_old, &boundary_old);
		fclose(case_file);
		n = 0; for(i = 0; i < n_variables; i ++) n += n_elements*ORDER_TO_N_BASIS(variable_order[i]);

		if(fetch_value(input_file,"initial_data_file_path",'s',data_file_path) == FETCH_SUCCESS)
		{
			print_info("reading existing data file %s",data_file_path);
			exit_if_false(data_file = fopen(data_file_path,"r"),"opening %s",data_file_path);
			read_data(data_file, &n_u_old, &u_old, &outer_iteration);
			fclose(data_file);
			exit_if_false(n_u_old == n,"case and initial data does not match");
		}
	}

	// construct new case
	else
	{
		print_info("reading the geometry file path");
		exit_if_false(fetch_value(input_file,"geometry_file_path",'s',geometry_file_path) == FETCH_SUCCESS,"reading geometry_file_path from %s",input_file_path);
		print_continue(geometry_file_path);

		print_info("reading the geometry file %s",geometry_file_path);
		exit_if_false(geometry_file = fopen(geometry_file_path,"r"),"opening %s",geometry_file_path);
		read_geometry(geometry_file, &n_nodes, &node, &n_faces, &face, &n_elements, &element);
		fclose(geometry_file);
		print_continue("%i nodes, %i faces and %i elements",n_nodes,n_faces,n_elements);

		print_info("generating additional connectivity and geometry");
		process_geometry(n_nodes, node, n_faces, face, n_elements, element);
	}

	// read the data file path and output frequency
	print_info("reading the data file path and output frequency");
	exit_if_false(fetch_value(input_file,"data_file_path",'s',data_file_path) == FETCH_SUCCESS,"reading data_file_path from %s",input_file_path);
	if(fetch_value(input_file,"data_number_of_outer_iterations",'i',&data_n_outer_iterations) != FETCH_SUCCESS)
		data_n_outer_iterations = n_outer_iterations + outer_iteration;
	print_continue("data to be written to %s every %i outer iterations",data_file_path,data_n_outer_iterations);

	// read boundaries
	print_info("reading boundaries");
	boundaries_input(input_file, n_faces, face, &n_boundaries, &boundary);
	print_continue("%i boundaries",n_boundaries);

	// read terms
	print_info("reading PDE terms");
	terms_input(input_file, &n_terms, &term);
	print_continue("%i terms",n_terms);

	// update unknown indices and values
	print_info("updating the numbering of the degrees of freedom");
	update_element_unknowns(n_variables_old, n_variables, variable_order_old, variable_order, n_elements, element, n_u_old, &n_u, u_old, &u);
	print_continue("%i degrees of freedom",n_u);

	// update face boundaries
	print_info("updating the face boundary associations");
	update_face_boundaries(n_variables, n_faces, face, n_boundaries, boundary);

	// update integration
	print_info("updating integration");
	i = update_face_integration(n_variables_old, n_variables, variable_order_old, variable_order, n_faces, face);
	if(i) print_continue("updated %i face quadratures",i);
	i = update_element_integration(n_variables_old, n_variables, variable_order_old, variable_order, n_elements, element);
	if(i) print_continue("updated %i element quadratures",i);

	// update numerics
	print_info("updating numerics");
	i = update_face_numerics(n_variables_old, n_variables, variable_order_old, variable_order, n_faces, face, n_boundaries_old, boundary_old);
	if(i) print_continue("updated %i face interpolations",i);
	i = update_element_numerics(n_variables_old, n_variables, variable_order_old, variable_order, n_elements, element);
	if(i) print_continue("updated %i element interpolations",i);

	// write case file
	print_info("writing case file %s",case_file_path);
	exit_if_false(case_file = fopen(case_file_path,"w"),"opening %s",case_file_path);
	write_case(case_file, n_variables, variable_order, n_nodes, node, n_faces, face, n_elements, element, n_boundaries, boundary);
	fclose(case_file);

	// read the display file path and output frequency
	print_info("reading the display file path and output frequency");
	if(
			fetch_value(input_file,"display_file_path",'s',display_file_path) == FETCH_SUCCESS &&
			fetch_value(input_file,"display_number_of_outer_iterations",'i',&display_n_outer_iterations) == FETCH_SUCCESS
	  )
		print_continue("display to be written to %s every %i outer iterations",display_file_path,display_n_outer_iterations);
	else
	{
		display_n_outer_iterations = 0;
		warn_if_false(0,"display files will not be written");
	}

	// initialise
	if(initial_input(input_file, n_variables, &initial))
	{
		print_info("initialising the degrees of freedom");
		initialise_values(n_variables, variable_order, n_elements, element, initial, u);
	}

	//-------------------------------------------------------------------//
	
	// allocate and initialise the system
	print_info("allocating and initialising the system");
	SPARSE system = NULL;
	initialise_system(n_variables, variable_order, n_elements, element, n_u, &system);
	double *residual, *max_residual, *du, *max_du;
	exit_if_false(residual = (double *)malloc(n_u * sizeof(double)),"allocating the residuals");
	exit_if_false(max_residual = (double *)malloc(n_variables * sizeof(double)),"allocating the maximum residuals");
	exit_if_false(du = (double *)malloc(n_u * sizeof(double)),"allocating du");
	exit_if_false(max_du = (double *)malloc(n_variables * sizeof(double)),"allocating the maximum changes");
	exit_if_false(u_old = (double *)realloc(u_old, n_u * sizeof(double)),"re-allocating u_old");

	timer_reset();

	// iterate
	print_info("iterating");
	n_outer_iterations += outer_iteration;
	for(; outer_iteration < n_outer_iterations; outer_iteration ++)
	{
		print_output("iteration %i", outer_iteration);

		for(i = 0; i < n_u; i ++) u_old[i] = u[i];

		for(inner_iteration = 0; inner_iteration < n_inner_iterations; inner_iteration ++)
		{
			calculate_system(n_variables, variable_order, n_faces, face, n_elements, element, n_terms, term, n_u, u_old, u, system, residual);

			exit_if_false(sparse_solve(system, du, residual) == SPARSE_SUCCESS,"solving system");
			for(i = 0; i < n_u; i ++) u[i] -= du[i];

			calculate_maximum_changes_and_residuals(n_variables, variable_order, n_elements, element, du, max_du, residual, max_residual);
			for(i = 0; i < n_variables; i ++) sprintf(&print[26*i],"%.6e|%.6e ",max_du[i],max_residual[i]);
			print_continue("%s",print);
		}

		slope_limit(n_variables, variable_order, n_nodes, node, n_elements, element, n_boundaries, boundary, u);

		if(data_n_outer_iterations && outer_iteration % data_n_outer_iterations == 0)
		{
			generate_numbered_file_path(data_numbered_file_path, data_file_path, outer_iteration);
			print_info("writing data to %s",data_numbered_file_path);
			exit_if_false(data_file = fopen(data_numbered_file_path,"w"),"opening %s",data_numbered_file_path);
			write_data(data_file, n_u, u, outer_iteration);
			fclose(data_file);
		}

		if(display_n_outer_iterations && outer_iteration % display_n_outer_iterations == 0)
		{
			generate_numbered_file_path(display_numbered_file_path, display_file_path, outer_iteration);
			print_info("writing display to %s",data_numbered_file_path);
			exit_if_false(display_file = fopen(display_numbered_file_path,"w"),"opening %s",display_numbered_file_path);
			write_display(display_file, n_variables, variable_name, variable_order, n_elements, element, n_u, u);
			fclose(display_file);
		}

		timer_print();
	}

	//-------------------------------------------------------------------//
	
	print_info("freeing all memory");

	fclose(input_file);

	free(geometry_file_path);
	free(case_file_path);
	free(data_file_path);
	free(data_numbered_file_path);
	free(display_file_path);
	free(display_numbered_file_path);

	free(print);

	destroy_nodes(n_nodes,node);
	destroy_faces(n_faces,face,n_variables);
	destroy_elements(n_elements,element,n_variables);
	destroy_boundaries(n_boundaries_old,boundary_old);
	destroy_boundaries(n_boundaries,boundary);
	destroy_terms(n_terms,term);
	destroy_initial(n_variables,initial);

	free(variable_order_old);
	free(variable_order);
	destroy_matrix((void *)variable_name);

	free(u_old);
	free(u);

	sparse_destroy(system);
	free(residual);
	free(max_residual);
	free(du);
	free(max_du);

	return 0;
}
Example #2
0
int main(int argc, char **argv) {

	/*
	** Variables
	*/

	GI *gi;
	PARTICLE *bh;
	SI *bulge;
	SI *halo;
	CHAR FILENAME[STRINGSIZE];
	FILE *file;

	/*
	** Initialise structures for reading parameters and start clock
	*/

	gi = malloc(sizeof(GI));
	assert(gi != NULL);
	bh = malloc(sizeof(PARTICLE));
	assert(bh != NULL);
	bulge = malloc(sizeof(SI));
	assert(bulge != NULL);
	halo = malloc(sizeof(SI));
	assert(halo != NULL);

	gi->t[0] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);

	/*
	** Set standard values for parameters
	*/

	sprintf(gi->outputname,"none");
	sprintf(bulge->systemname,"bulge");
	sprintf(halo->systemname,"halo");

	initialise_general(gi);
	initialise_particle(bh);
	initialise_system(bulge);
	initialise_system(halo);

	/*
	** Read in and process arguments
	*/

	process_arguments(argc,argv,gi,bh,bulge,halo);

	fprintf(stderr,"Checking parameters, calculating halo properties and initialising grid in r... \n");

	/*
	** Initialise random number generator
	*/

	srand(gi->randomseed);

	/*
	** Check main input parameters
	*/

	check_main_parameters_general(gi);
	if (gi->do_bulge == 1) {
		check_main_parameters_system(bulge);
		}
	if (gi->do_halo == 1) {
		check_main_parameters_system(halo);
		}

	/*
	** Allocate memory for the structures
	*/

	allocate_general(gi);
	if (gi->do_bulge == 1) {
		allocate_system(gi,bulge);
		}
	if (gi->do_halo == 1) {
		allocate_system(gi,halo);
		}

	/*
	** Calculate parameters
	*/

	calculate_parameters_general(gi);
	if (gi->do_bulge == 1) {
		calculate_parameters_system(gi,bulge);
		}
	if (gi->do_halo == 1) {
		calculate_parameters_system(gi,halo);
		}

	/*
	** Initialise gridr
	*/

	initialise_gridr(gi,bh,bulge,halo);

	if (gi->output_gridr == 1) {
		sprintf(FILENAME,"%s.gridr.total.dat",gi->outputname);
		file = fopen(FILENAME,"w");
		assert(file != NULL);
		write_gridr_total(file,gi);
		fclose(file);
		if (gi->do_bulge == 1) {
			sprintf(FILENAME,"%s.gridr.bulge.dat",gi->outputname);
			file = fopen(FILENAME,"w");
			write_gridr_system(file,gi,bulge);
			fclose(file);
			}
		if (gi->do_halo == 1) {
			sprintf(FILENAME,"%s.gridr.halo.dat",gi->outputname);
			file = fopen(FILENAME,"w");
			write_gridr_system(file,gi,halo);
			fclose(file);
			}
		}

	/*
	** Calculate virial stuff for finite mass models and cutoff models
	*/

	if (gi->do_bulge == 1) {
		calculate_virial_stuff(gi,bulge);
		}
	if (gi->do_halo == 1) {
		calculate_virial_stuff(gi,halo);
		}

	/*
	** Set remaining parameters
	*/

	if (gi->do_bulge == 1) {
		set_remaining_parameters(gi,bulge);
		}
	if (gi->do_halo == 1) {
		set_remaining_parameters(gi,halo);
		}

	/*
	** Check some more things
	*/

	if (gi->do_bulge == 1) {
		check_more_parameters_system(gi,bulge);
		}
	if (gi->do_halo == 1) {
		check_more_parameters_system(gi,halo);
		}

	/*
	** Initialise griddf
	*/

	gi->t[1] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds.\nInitialising grid for distribution function... \n",gi->t[1]-gi->t[0]);

	if (gi->positionsonly == 0) {
		if (gi->do_bulge == 1) {
			initialise_griddf(gi,bulge);
			if (gi->output_griddf == 1) {
				sprintf(FILENAME,"%s.griddf.bulge.dat",gi->outputname);
				file = fopen(FILENAME,"w");
				assert(file != NULL);
				write_griddf_system(file,gi,bulge);
				fclose(file);
				}
			}
		if (gi->do_halo == 1) {
			initialise_griddf(gi,halo);
			if (gi->output_griddf == 1) {
				sprintf(FILENAME,"%s.griddf.halo.dat",gi->outputname);
				file = fopen(FILENAME,"w");
				assert(file != NULL);
				write_griddf_system(file,gi,halo);
				fclose(file);
				}
			}
		}

	/*
	** Initialise shell
	*/

	gi->t[2] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds\nInitialising shells... \n",gi->t[2]-gi->t[1]);

	if (gi->do_bulge == 1) {
		initialise_shell(gi,bulge);
		}
	if (gi->do_halo == 1) {
		initialise_shell(gi,halo);
		}

	/*
	** Set particle positions
	*/

	gi->t[3] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds.\nSetting particle positions... \n",gi->t[3]-gi->t[2]);

	if (gi->do_bulge == 1) {
		set_positions(gi,bulge);
		}
	if (gi->do_halo == 1) {
		set_positions(gi,halo);
		}

	/*
	** Set particle velocities
	*/

	gi->t[4] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds.\nSetting particle velocities... \n",gi->t[4]-gi->t[3]);

	if (gi->positionsonly == 0) {
		if (gi->do_bulge == 1) {
			set_velocities(gi,bulge);
			}
		if (gi->do_halo == 1) {
			set_velocities(gi,halo);
			}
		}
	else {
		if (gi->do_bulge == 1) {
			set_velocities_zero(bulge);
			}
		if (gi->do_halo == 1) {
			set_velocities_zero(halo);
			}
		}

	/*
	** Set remaining attributes
	*/

	gi->t[5] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds.\nSetting remaining particle attributes... \n",gi->t[5]-gi->t[4]);

	if (gi->do_bulge == 1) {
		set_attributes(gi,bulge);
		}
	if (gi->do_halo == 1) {
		set_attributes(gi,halo);
		}

	/*
	** Do orbit dependent refining
	*/

	gi->t[6] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds.\nDoing orbit dependent refining... \n",gi->t[6]-gi->t[5]);

	if (gi->do_bulge == 1) {
		refine(gi,bulge);
		}
	if (gi->do_halo == 1) {
		refine(gi,halo);
		}

	/*
	** Calculate a few things and do center of mass correction
	*/

	gi->t[7] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds\nCalculating a few things and correct center of mass position and velocity... \n",gi->t[7]-gi->t[6]);

	if (gi->do_bulge == 1) {
		double_particles(bulge);
		calculate_samplinginfo_system(gi,bulge);
		}
	if (gi->do_halo == 1) {
		double_particles(halo);
		calculate_samplinginfo_system(gi,halo);
		}
	calculate_samplinginfo_general(gi,bh);

	/*
	** Write Output
	*/

	gi->t[8] = ((DOUBLE) clock())/((DOUBLE) CLOCKS_PER_SEC);
	fprintf(stderr,"Done in "OFD1" seconds\nWriting output... \n",gi->t[8]-gi->t[7]);

	if (gi->output_tipsy_standard == 1) {
		sprintf(FILENAME,"%s.tipsy.std",gi->outputname);
		file = fopen(FILENAME,"w");
		assert(file != NULL);
		write_tipsy_xdr_halogen(file,gi,bh,bulge,halo);
		fclose(file);
		}
	if (gi->output_tipsy_standard_dpp == 1) {
		sprintf(FILENAME,"%s.tipsy.dpp.std",gi->outputname);
		file = fopen(FILENAME,"w");
		assert(file != NULL);
		write_tipsy_xdr_dpp_halogen(file,gi,bh,bulge,halo);
		fclose(file);
		}

	/* 
	** Print some output in file
	*/

	sprintf(FILENAME,"%s.info.dat",gi->outputname);
	file = fopen(FILENAME,"w");
	assert(file != NULL);
	write_general_output(file,argc,argv,gi,bh,bulge,halo);
	fclose(file);

	fprintf(stderr,"Done in "OFD1" seconds\nTotal time needed was "OFD1" seconds\n",gi->t[9]-gi->t[8],gi->t[9]-gi->t[0]);

	free(gi);
	free(bh);
	free(halo);
	free(bulge);

	exit(0);
	} /* end of main function */