From 6e4f517a4ede4aae7dc2f35f1dc02882019aab45 Mon Sep 17 00:00:00 2001 From: ErwanH29 Date: Wed, 29 May 2024 15:13:31 +0200 Subject: [PATCH 1/3] Added collision handler for Huayno OK algorithm --- src/amuse/community/huayno/src/evolve_ok.c | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/amuse/community/huayno/src/evolve_ok.c b/src/amuse/community/huayno/src/evolve_ok.c index 00d66d5fcb..12e25d1076 100644 --- a/src/amuse/community/huayno/src/evolve_ok.c +++ b/src/amuse/community/huayno/src/evolve_ok.c @@ -7,8 +7,13 @@ #include #include #include +#ifdef _OPENMP +#include +#endif #include "evolve.h" #include "evolve_ok.h" +// AMUSE STOPPING CONDITIONS SUPPORT +#include struct forces zeroforces = {0, NULL}; @@ -150,6 +155,8 @@ static void ok_kick(int clevel,struct forces f, DOUBLE dt) void evolve_ok2(int clevel,struct sys s, struct forces f, DOUBLE stime, DOUBLE etime, DOUBLE dt, int calc_timestep) { + int is_collision_detection_enabled; + is_stopping_condition_enabled(COLLISION_DETECTION, &is_collision_detection_enabled); if (IS_ZEROFORCES(f) && clevel == 0) { f = ok_main_forces; } CHECK_TIMESTEP(etime,stime,dt,clevel); // all particles are drifted together @@ -165,5 +172,40 @@ void evolve_ok2(int clevel,struct sys s, struct forces f, DOUBLE stime, DOUBLE e ok_split((FLOAT) dt, f, &slowf, &fastf); evolve_ok2(clevel+1,s, fastf, stime, stime+dt/2, dt/2, 0); ok_kick(clevel,slowf, dt); + + if (is_collision_detection_enabled) { + UINT i, j; + FLOAT dx[3], dr2, radius_sum; + struct particle *ipart, *jpart; + #pragma omp parallel for if((ULONG) s.n*s.n>MPWORKLIMIT && !omp_in_parallel()) default(none) \ + private(i,j,dx,dr2,radius_sum, ipart, jpart) \ + shared(s) + for (i=0; imass > 0){ + for (j=i+1; jpos[0] - jpart->pos[0]; + dx[1] = ipart->pos[1] - jpart->pos[1]; + dx[2] = ipart->pos[2] - jpart->pos[2]; + dr2 = dx[0]*dx[0] + dx[1]*dx[1] + dx[2]*dx[2]; + radius_sum = ipart->radius + jpart->radius; + if (dr2 <= radius_sum*radius_sum) { + #pragma omp critical + { + int stopping_index = next_index_for_stopping_condition(); + if (stopping_index >= 0) { + set_stopping_condition_info(stopping_index, COLLISION_DETECTION); + set_stopping_condition_particle_index(stopping_index, 0, ipart->id); + set_stopping_condition_particle_index(stopping_index, 1, jpart->id); + return; + } + } + } + } + } + } + } + evolve_ok2(clevel+1,s, fastf, stime+dt/2, etime, dt/2, 1); } From d395e4423885266a5bd28c6f7315a9fd0dfd8f5e Mon Sep 17 00:00:00 2001 From: ErwanH29 Date: Thu, 30 May 2024 09:45:03 +0200 Subject: [PATCH 2/3] Moved collision algorithms to separate file --- src/amuse/community/huayno/src/Makefile | 3 +- .../community/huayno/src/collision_check.c | 42 ++++++++++++++++ .../community/huayno/src/collision_check.h | 6 +++ src/amuse/community/huayno/src/evolve_ok.c | 48 ++++++++++--------- .../huayno/src/evolve_shared_collisions.c | 33 +------------ 5 files changed, 76 insertions(+), 56 deletions(-) create mode 100644 src/amuse/community/huayno/src/collision_check.c create mode 100644 src/amuse/community/huayno/src/collision_check.h diff --git a/src/amuse/community/huayno/src/Makefile b/src/amuse/community/huayno/src/Makefile index 6aa244b3ba..d08bbf04c2 100644 --- a/src/amuse/community/huayno/src/Makefile +++ b/src/amuse/community/huayno/src/Makefile @@ -26,7 +26,8 @@ RM = rm OBJS = evolve.o evolve_shared.o evolve_sf.o evolve_cc.o \ evolve_ok.o evolve_kepler.o universal_variable_kepler.o evolve_bs.o \ - evolve_shared_collisions.o evolve_error_control.o simple_map.o simple_hash.o + evolve_shared_collisions.o evolve_error_control.o simple_map.o simple_hash.o \ + collision_check.o all: libhuayno.a diff --git a/src/amuse/community/huayno/src/collision_check.c b/src/amuse/community/huayno/src/collision_check.c new file mode 100644 index 0000000000..6fdaa0aabb --- /dev/null +++ b/src/amuse/community/huayno/src/collision_check.c @@ -0,0 +1,42 @@ +/* + * Algorithm to detect collisions between particles + */ +#include "evolve.h" +#include "evolve_ok.h" +#include "collision_check.h" +// AMUSE STOPPING CONDITIONS SUPPORT +#include + + +void detect_collisions(struct sys s){ + UINT i, j; + FLOAT dx[3], dr2, radius_sum; + struct particle *ipart, *jpart; + #pragma omp parallel for if((ULONG) s.n*s.n>MPWORKLIMIT && !omp_in_parallel()) default(none) \ + private(i,j,dx,dr2,radius_sum, ipart, jpart) \ + shared(s) + for (i=0; imass > 0){ + for (j=i+1; jpos[0] - jpart->pos[0]; + dx[1] = ipart->pos[1] - jpart->pos[1]; + dx[2] = ipart->pos[2] - jpart->pos[2]; + dr2 = dx[0]*dx[0] + dx[1]*dx[1] + dx[2]*dx[2]; + radius_sum = ipart->radius + jpart->radius; + if (dr2 <= radius_sum*radius_sum) { + #pragma omp critical + { + int stopping_index = next_index_for_stopping_condition(); + if (stopping_index >= 0) { + set_stopping_condition_info(stopping_index, COLLISION_DETECTION); + set_stopping_condition_particle_index(stopping_index, 0, ipart->id); + set_stopping_condition_particle_index(stopping_index, 1, jpart->id); + } + } + } + } + } + } +} \ No newline at end of file diff --git a/src/amuse/community/huayno/src/collision_check.h b/src/amuse/community/huayno/src/collision_check.h new file mode 100644 index 0000000000..3f77458222 --- /dev/null +++ b/src/amuse/community/huayno/src/collision_check.h @@ -0,0 +1,6 @@ +#ifndef COLLISION_CHECK_H +#define COLLISION_CHECK_H + +void detect_collisions(struct sys s); + +#endif // COLLISION_CHECK_H \ No newline at end of file diff --git a/src/amuse/community/huayno/src/evolve_ok.c b/src/amuse/community/huayno/src/evolve_ok.c index 12e25d1076..13400c61c4 100644 --- a/src/amuse/community/huayno/src/evolve_ok.c +++ b/src/amuse/community/huayno/src/evolve_ok.c @@ -153,27 +153,7 @@ static void ok_kick(int clevel,struct forces f, DOUBLE dt) diag->kcount[clevel] += f.n; } -void evolve_ok2(int clevel,struct sys s, struct forces f, DOUBLE stime, DOUBLE etime, DOUBLE dt, int calc_timestep) -{ - int is_collision_detection_enabled; - is_stopping_condition_enabled(COLLISION_DETECTION, &is_collision_detection_enabled); - if (IS_ZEROFORCES(f) && clevel == 0) { f = ok_main_forces; } - CHECK_TIMESTEP(etime,stime,dt,clevel); - // all particles are drifted together - if (f.n == 0) - { - diag->deepsteps++; - diag->simtime += dt; - drift(clevel,s, etime, dt); - return; - } - if (calc_timestep) ok_timestep_cpu(clevel,f, dt); - struct forces slowf = zeroforces, fastf = zeroforces; - ok_split((FLOAT) dt, f, &slowf, &fastf); - evolve_ok2(clevel+1,s, fastf, stime, stime+dt/2, dt/2, 0); - ok_kick(clevel,slowf, dt); - - if (is_collision_detection_enabled) { +static void detect_collisions(struct sys s){ UINT i, j; FLOAT dx[3], dr2, radius_sum; struct particle *ipart, *jpart; @@ -198,14 +178,36 @@ void evolve_ok2(int clevel,struct sys s, struct forces f, DOUBLE stime, DOUBLE e set_stopping_condition_info(stopping_index, COLLISION_DETECTION); set_stopping_condition_particle_index(stopping_index, 0, ipart->id); set_stopping_condition_particle_index(stopping_index, 1, jpart->id); - return; } } } } } } +} + +void evolve_ok2(int clevel,struct sys s, struct forces f, DOUBLE stime, DOUBLE etime, DOUBLE dt, int calc_timestep) +{ + int is_collision_detection_enabled; + is_stopping_condition_enabled(COLLISION_DETECTION, &is_collision_detection_enabled); + if (IS_ZEROFORCES(f) && clevel == 0) { f = ok_main_forces; } + CHECK_TIMESTEP(etime,stime,dt,clevel); + // all particles are drifted together + if (f.n == 0) + { + diag->deepsteps++; + diag->simtime += dt; + drift(clevel,s, etime, dt); + return; + } + if (calc_timestep) ok_timestep_cpu(clevel,f, dt); + struct forces slowf = zeroforces, fastf = zeroforces; + ok_split((FLOAT) dt, f, &slowf, &fastf); + evolve_ok2(clevel+1,s, fastf, stime, stime+dt/2, dt/2, 0); + ok_kick(clevel,slowf, dt); + if (is_collision_detection_enabled) { + detect_collisions(s); + if (set_conditions & enabled_conditions) return; } - evolve_ok2(clevel+1,s, fastf, stime+dt/2, etime, dt/2, 1); } diff --git a/src/amuse/community/huayno/src/evolve_shared_collisions.c b/src/amuse/community/huayno/src/evolve_shared_collisions.c index a51c7c1ff6..a2426a5573 100644 --- a/src/amuse/community/huayno/src/evolve_shared_collisions.c +++ b/src/amuse/community/huayno/src/evolve_shared_collisions.c @@ -10,6 +10,7 @@ #endif #include "evolve.h" #include "integrators_shared.h" +#include "collision_check.h" // AMUSE STOPPING CONDITIONS SUPPORT #include @@ -43,37 +44,6 @@ static int update_steps_and_get_next_level(int *steps, int current_level) { return current_level; } -static void detect_collisions(struct sys s) { - UINT i, j; - FLOAT dx[3], dr2, radius_sum; - struct particle *ipart, *jpart; -#pragma omp parallel for if((ULONG) s.n*s.n>MPWORKLIMIT && !omp_in_parallel()) default(none) \ - private(i,j,dx,dr2,radius_sum, ipart, jpart) \ - shared(s) - for (i=0; ipos[0] - jpart->pos[0]; - dx[1] = ipart->pos[1] - jpart->pos[1]; - dx[2] = ipart->pos[2] - jpart->pos[2]; - dr2 = dx[0]*dx[0] + dx[1]*dx[1] + dx[2]*dx[2]; - radius_sum = ipart->radius + jpart->radius; - if (dr2 <= radius_sum*radius_sum) { - #pragma omp critical - { - int stopping_index = next_index_for_stopping_condition(); - if (stopping_index >= 0) { - set_stopping_condition_info(stopping_index, COLLISION_DETECTION); - set_stopping_condition_particle_index(stopping_index, 0, ipart->id); - set_stopping_condition_particle_index(stopping_index, 1, jpart->id); - } - } - } - } - } -} - static void evolve_shared_collision_detection(struct sys s, DOUBLE dt, void (*dkd_func)(int, struct sys, DOUBLE, DOUBLE, DOUBLE)) { FLOAT dtsys; int next_level, current_level = 0; @@ -133,4 +103,3 @@ void evolve_shared8_collision_detection(struct sys s, DOUBLE dt) { void evolve_shared10_collision_detection(struct sys s, DOUBLE dt) { evolve_shared_collision_detection(s, dt, dkd10); } - From aa3335fda612f0710ca7d38459f301e540f4d0ca Mon Sep 17 00:00:00 2001 From: ErwanH29 Date: Thu, 30 May 2024 09:54:56 +0200 Subject: [PATCH 3/3] Removed the repeated collision function from evolve_ok.c --- src/amuse/community/huayno/src/evolve_ok.c | 33 ---------------------- 1 file changed, 33 deletions(-) diff --git a/src/amuse/community/huayno/src/evolve_ok.c b/src/amuse/community/huayno/src/evolve_ok.c index 13400c61c4..2c2f01e53c 100644 --- a/src/amuse/community/huayno/src/evolve_ok.c +++ b/src/amuse/community/huayno/src/evolve_ok.c @@ -153,39 +153,6 @@ static void ok_kick(int clevel,struct forces f, DOUBLE dt) diag->kcount[clevel] += f.n; } -static void detect_collisions(struct sys s){ - UINT i, j; - FLOAT dx[3], dr2, radius_sum; - struct particle *ipart, *jpart; - #pragma omp parallel for if((ULONG) s.n*s.n>MPWORKLIMIT && !omp_in_parallel()) default(none) \ - private(i,j,dx,dr2,radius_sum, ipart, jpart) \ - shared(s) - for (i=0; imass > 0){ - for (j=i+1; jpos[0] - jpart->pos[0]; - dx[1] = ipart->pos[1] - jpart->pos[1]; - dx[2] = ipart->pos[2] - jpart->pos[2]; - dr2 = dx[0]*dx[0] + dx[1]*dx[1] + dx[2]*dx[2]; - radius_sum = ipart->radius + jpart->radius; - if (dr2 <= radius_sum*radius_sum) { - #pragma omp critical - { - int stopping_index = next_index_for_stopping_condition(); - if (stopping_index >= 0) { - set_stopping_condition_info(stopping_index, COLLISION_DETECTION); - set_stopping_condition_particle_index(stopping_index, 0, ipart->id); - set_stopping_condition_particle_index(stopping_index, 1, jpart->id); - } - } - } - } - } - } -} - void evolve_ok2(int clevel,struct sys s, struct forces f, DOUBLE stime, DOUBLE etime, DOUBLE dt, int calc_timestep) { int is_collision_detection_enabled;