#include <v_lib.h>
#include "phasesp.h"
#include "ctrl.h"
#include "psys.h"

particle_system_p
alloc_particle_system(u32_t num_particles)
{
	particle_system_p ps;
	ps = memAllocZ(sizeof(particle_system_t));
	ps->num_particles = num_particles;
	ps->particle_radius = 
		(vec_p)memAllocZ(num_particles * sizeof(vec_t));
	ps->particle_density = 
		(vec_p)memAllocZ(num_particles * sizeof(vec_t));
	ps->particle_mass = 
		(vec_p)memAllocZ(num_particles * sizeof(vec_t));
	ps->particle_reciprocal_mass = 
		(vec_p)memAllocZ(num_particles * sizeof(vec_t));
	ps->particle_6_pi_eta_r = 
		(vec_p)memAllocZ(num_particles * sizeof(vec_t));
	ps->phase = 
		(vec_p)memAllocZ(num_particles * sizeof(vec_t) * PHASESPACE_DIM);
	ps->force = 
		(vec3_t*)memAllocZ(num_particles * sizeof(vec3_t));
	ps->saves_per_second = 10;
	ps->solution_RK_tolerance = 1e-2;
	ps->solution_RK_timeout = 10;
	ps->rt_timer = timerCreate();
	return ps;
}

void
free_particle_system(particle_system_p ps)
{
	if(!ps)
		return;
	if(ps->rt_timer != LIBV_INVALID_HANDLE)
		timerDestroy(ps->rt_timer);
	if(ps->particle_radius)
		memFree(ps->particle_radius);
	if(ps->particle_density)
		memFree(ps->particle_density);
	if(ps->particle_mass)
		memFree(ps->particle_mass);
	if(ps->particle_reciprocal_mass)
		memFree(ps->particle_reciprocal_mass);
	if(ps->particle_6_pi_eta_r)
		memFree(ps->particle_6_pi_eta_r);
	if(ps->phase)
		memFree(ps->phase);
	if(ps->force)
		memFree(ps->force);
	memFree(ps);
}

