Skip to content

Commit

Permalink
working on correct reuse of codes
Browse files Browse the repository at this point in the history
  • Loading branch information
arjenve committed Apr 25, 2017
1 parent 111ffcd commit 614c2e5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/amuse/community/rebound/interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ static int max_id = 0;

typedef std::vector<code_state> ReboundSimulationVector;
static ReboundSimulationVector codes;
static particle_location sentinel = particle_location();
//static particle_location sentinel = particle_location();
static double _time;
static double timestep = 0.0001;

static inline reb_particle* get_particle_from_identity(int index_of_the_particle)
{
struct reb_particle* p;
struct reb_particle* p = NULL;
for( ReboundSimulationVector::iterator i = codes.begin(); i != codes.end(); i++) {
code_state cs = *i;
p = reb_get_particle_by_hash(cs.code, index_of_the_particle);
Expand Down Expand Up @@ -723,6 +723,8 @@ int cleanup_code() {
}
codes.clear();
max_id = 0;
timestep = 0.0001;
_time=0;
return 0;
}

Expand All @@ -731,7 +733,9 @@ int recommit_parameters(){
}

int initialize_code(){
initialize_stopping_conditions();
max_id = 0;
timestep = 0.0001;
_time=0;
#ifdef OPENMP_ENABLED
int nt = omp_get_max_threads();
Expand Down
3 changes: 3 additions & 0 deletions src/amuse/support/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,9 @@ def __init_handlers__(self, legacy_interface, options):

self.setup()

def __del__(self):

This comment has been minimized.

Copy link
@ipelupessy

ipelupessy Apr 25, 2017

Member

may relate to #123

self.stop()

@option(type='boolean', sections=("code", "state",))
def must_handle_state(self):
return True
Expand Down
11 changes: 9 additions & 2 deletions test/codes_tests/test_rebound.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def test1(self):
self.assertEquals(vx, 4)
self.assertEquals(vy, 5)
self.assertEquals(vz, 6)
instance.cleanup_code()
instance.stop()

def test2(self):
Expand Down Expand Up @@ -135,6 +136,7 @@ def test6(self):
index,err=instance.new_particle(0.5, 0.5, 0, 0, 0, 0.5, 0, 0.01, 1)
self.assertEquals(-10, err)

instance.cleanup_code()
instance.stop()


Expand Down Expand Up @@ -374,7 +376,7 @@ def test6(self):
instance.particles.add_particles(particles)
self.assertAlmostRelativeEquals(instance.kinetic_energy, particles.kinetic_energy())
self.assertAlmostRelativeEquals(instance.potential_energy, particles.potential_energy(G = nbody_system.G))

instance.stop()


def test7(self):
Expand Down Expand Up @@ -405,6 +407,7 @@ def test7(self):
self.assertAlmostRelativeEquals(instance.potential_energy, particles.potential_energy(G = nbody_system.G))
self.assertAlmostRelativeEquals(instance.get_kinetic_energy(subset1), particles2.kinetic_energy())
self.assertAlmostRelativeEquals(instance.get_potential_energy(subset1), particles2.potential_energy(G = nbody_system.G))
#instance.stop()



Expand Down Expand Up @@ -437,6 +440,7 @@ def test8(self):
self.assertAlmostRelativeEquals(instance.get_kinetic_energy(subset1), particles2.kinetic_energy(), 2)
self.assertAlmostRelativeEquals(instance.get_potential_energy(subset1), particles2.potential_energy(G = nbody_system.G), 2)
particles_evolved = instance.particles.copy()
instance.stop()

instance1 = self.new_instance_of_an_optional_code(Rebound)
instance1.parameters.epsilon_squared = 0.0 | nbody_system.length**2
Expand All @@ -449,6 +453,7 @@ def test8(self):
print instance1.particles
self.assertAlmostRelativeEquals(instance1.particles.position, particles1evolved.position, 10)
self.assertAlmostRelativeEquals(instance1.particles.velocity, particles1evolved.velocity, 10)
instance1.stop()

instance2 = self.new_instance_of_an_optional_code(Rebound)
instance2.parameters.epsilon_squared = 0.0 | nbody_system.length**2
Expand All @@ -462,7 +467,7 @@ def test8(self):
print instance2.particles
self.assertAlmostRelativeEquals(instance2.particles.position, particles2evolved.position, 10)
self.assertAlmostRelativeEquals(instance2.particles.velocity, particles2evolved.velocity, 10)

instance2.stop()

def test9(self):
instance = self.new_instance_of_an_optional_code(Rebound)
Expand Down Expand Up @@ -491,6 +496,7 @@ def test9(self):
self.assertEquals(instance.particles[5].mass, 0 | nbody_system.mass)
instance.evolve_model(1 | nbody_system.time)
self.assertEquals(instance.particles[4].x, 5 | nbody_system.length)
instance.stop()

def test10(self):
instance = self.new_instance_of_an_optional_code(Rebound)
Expand All @@ -508,5 +514,6 @@ def test10(self):
instance.evolve_model(4 | nbody_system.time)
self.assertAlmostRelativeEquals(instance.particles[0].velocity, [0, 2,0] | nbody_system.speed)
self.assertAlmostRelativeEquals(instance.particles[0].position, [0, 2 * 4, 0] | nbody_system.length, 8)
instance.stop()


0 comments on commit 614c2e5

Please sign in to comment.