Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rebound interface to Rebound release version 4.0.1 #1019

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 32 additions & 27 deletions src/amuse/community/rebound/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,37 @@ OBJS = interface.o

CODELIB = src/librebound.a
CODEDIR = src/rebound
LIB_OBJ = $(CODEDIR)/src/rebound.o $(CODEDIR)/src/tree.o $(CODEDIR)/src/particle.o $(CODEDIR)/src/gravity.o \
$(CODEDIR)/src/integrator.o $(CODEDIR)/src/integrator_whfast.o $(CODEDIR)/src/integrator_whfasthelio.o \
$(CODEDIR)/src/integrator_ias15.o $(CODEDIR)/src/integrator_sei.o $(CODEDIR)/src/integrator_leapfrog.o \
$(CODEDIR)/src/integrator_hermes.o $(CODEDIR)/src/boundary.o \
$(CODEDIR)/src/collision.o $(CODEDIR)/src/tools.o \
$(CODEDIR)/src/communication_mpi.o $(CODEDIR)/src/display.o $(CODEDIR)/src/derivatives.o \
$(CODEDIR)/src/glad.o $(CODEDIR)/src/integrator_janus.o $(CODEDIR)/src/transformations.o \
$(CODEDIR)/src/simulationarchive.o $(CODEDIR)/src/output.o \
$(CODEDIR)/src/input.o \
LIB_OBJ = \
$(CODEDIR)/src/rebound.o \
$(CODEDIR)/src/tree.o \
$(CODEDIR)/src/particle.o \
$(CODEDIR)/src/gravity.o \
$(CODEDIR)/src/integrator.o \
$(CODEDIR)/src/integrator_whfast.o \
$(CODEDIR)/src/integrator_whfast512.o \
$(CODEDIR)/src/integrator_saba.o \
$(CODEDIR)/src/integrator_ias15.o \
$(CODEDIR)/src/integrator_sei.o \
$(CODEDIR)/src/integrator_bs.o \
$(CODEDIR)/src/integrator_leapfrog.o \
$(CODEDIR)/src/integrator_mercurius.o \
$(CODEDIR)/src/integrator_eos.o \
$(CODEDIR)/src/boundary.o \
$(CODEDIR)/src/input.o \
$(CODEDIR)/src/binarydiff.o \
$(CODEDIR)/src/output.o \
$(CODEDIR)/src/collision.o \
$(CODEDIR)/src/communication_mpi.o \
$(CODEDIR)/src/display.o \
$(CODEDIR)/src/tools.o \
$(CODEDIR)/src/rotations.o \
$(CODEDIR)/src/derivatives.o \
$(CODEDIR)/src/simulationarchive.o \
$(CODEDIR)/src/glad.o \
$(CODEDIR)/src/integrator_janus.o \
$(CODEDIR)/src/transformations.o \
$(CODEDIR)/src/fmemopen.o \
$(CODEDIR)/src/server.o

DOWNLOAD_FROM_WEB = $(PYTHON) ./download.py
PATCH_FILES = $(PYTHON) ./patch_files.py
Expand All @@ -39,25 +61,8 @@ AM_CFLAGS = -I$(AMUSE_DIR)/lib/amuse_mpi

all: rebound_worker

DOWNLOAD_CODES?=1

ifdef DOWNLOAD_CODES
$(CODEDIR)/Makefile:
make -C . download
else
$(CODEDIR)/Makefile:
@echo ""
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
@echo ""
@echo "DOWNLOAD_CODES is not set. Rebound will not be downloaded and built."
@echo "If you do want Rebound, set DOWNLOAD_CODES to 1."
@echo "bash> export DOWNLOAD_CODES=1"
@echo "csh> setenv DOWNLOAD_CODES 1"
@echo ""
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
@echo ""
@make -s --no-print-directory -C . raise_error
endif

download:
$(RM) -Rf .pc
Expand Down Expand Up @@ -85,7 +90,7 @@ distclean:
$(RM) -Rf .pc

$(CODELIB): $(CODEDIR)/Makefile
make -C $(CODEDIR) all CC=$(CC)
make -C $(CODEDIR) librebound CC=$(CC)
ar rv $(CODELIB) $(LIB_OBJ)
ranlib $(CODELIB)

Expand Down
80 changes: 48 additions & 32 deletions src/amuse/community/rebound/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,91 @@

import subprocess
import os
import sys
import time
import argparse
import urllib.request
import urllib.parse
import urllib.error
from optparse import OptionParser
from shutil import which


class GetCodeFromHttp(object):
url_template = "https://github.com/hannorein/rebound/archive/{version}.tar.gz"
class GetCodeFromHttp:
filename_template = "{version}.tar.gz"
version = ""
name = ["rebound"]
url_template = [
"https://github.com/hannorein/rebound/archive/{version}.tar.gz"
]
version = [
"",
]

def directory(self):
return os.path.abspath(os.path.dirname(__file__))

def src_directory(self):
return os.path.join(self.directory(), "src")

def unpack_downloaded_file(self, filename):
print("unpacking", filename)
def unpack_downloaded_file(self, filename, name, version):
print(f"unpacking {filename}")
arguments = ["tar", "-xf"]
arguments.append(filename)
subprocess.call(arguments, cwd=os.path.join(self.src_directory()))
subprocess.call(
["mv", "rebound-{version}".format(version=self.version), "rebound"],
["mv", f"{name}-{version}", name],
cwd=os.path.join(self.src_directory()),
)
print("done")

def start(self):
if os.path.exists("src"):
counter = 0
while os.path.exists("src.{0}".format(counter)):
while os.path.exists(f"src.{counter}"):
counter += 1
if counter > 100:
print("too many backup directories")
break
os.rename("src", "src.{0}".format(counter))
os.rename("src", f"src.{counter}")

os.mkdir("src")

url = self.url_template.format(version=self.version)
filename = self.filename_template.format(version=self.version)
filepath = os.path.join(self.src_directory(), filename)
print("downloading version", self.version, "from", url, "to", filename)
urllib.request.urlretrieve(url, filepath)
print("downloading finished")
self.unpack_downloaded_file(filename)
for i, url_template in enumerate(self.url_template):
url = url_template.format(version=self.version[i])
filename = self.filename_template.format(version=self.version[i])
filepath = os.path.join(self.src_directory(), filename)
print(f"downloading version {self.version[i]} from {url} to {filename}")
if which("wget") is not None:
arguments = ["wget", url]
subprocess.call(arguments, cwd=os.path.join(self.src_directory()))
elif which("curl") is not None:
arguments = ["curl", "-L", "-O", url]
subprocess.call(arguments, cwd=os.path.join(self.src_directory()))
else:
urllib.request.urlretrieve(url, filepath)
print("downloading finished")
self.unpack_downloaded_file(filename, self.name[i], self.version[i])


def new_argument_parser():
result = argparse.ArgumentParser()
result.add_argument(
"--rebound-version",
default="111bcd50de54e4cc70e1d4915918226a4adbe188",
# default="4df2b4603058e7d767aee9c26b586b55a5f4de65",
dest="rebound_version",
help="Rebound commit hash to download",
type=str,
)
return result


def main(version=""):
arguments = new_argument_parser().parse_args()
version = [
arguments.rebound_version,
]
instance = GetCodeFromHttp()
instance.version = version
instance.start()


def new_option_parser():
result = OptionParser()
result.add_option(
"--version",
default="31d117bdc92182073d0941c331f76e95f515bfc6",
dest="version",
help="version number to download",
type="string",
)
return result


if __name__ == "__main__":
options, arguments = new_option_parser().parse_args()
main(**options.__dict__)
main()
59 changes: 38 additions & 21 deletions src/amuse/community/rebound/interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static inline particle_location get_particle_from_identity(int index_of_the_part
for( ReboundSimulationVector::iterator i = codes.begin(); i != codes.end(); i++) {
code_state cs = *i;
particle.code = cs.code;
particle.p = reb_get_particle_by_hash(particle.code, index_of_the_particle);
particle.p = reb_simulation_particle_by_hash(particle.code, index_of_the_particle);
if (particle.p != NULL) break;
//*i = cs;
}
Expand Down Expand Up @@ -124,7 +124,7 @@ int set_mass(int index_of_the_particle, double mass){

if(p->m==0){
if(mass>0){
int index_old=reb_get_particle_index(p);
int index_old=reb_simulation_particle_index(p);
if(index_old!=code->N_active){
struct reb_particle tmp = code->particles[index_old];
for(int j=index_old; j>code->N_active; j--){
Expand All @@ -137,7 +137,7 @@ int set_mass(int index_of_the_particle, double mass){
}
else {
if(mass==0){
int index_old=reb_get_particle_index(p);
int index_old=reb_simulation_particle_index(p);
code->N_active--;

if(index_old!=code->N_active){
Expand Down Expand Up @@ -178,7 +178,7 @@ int new_particle(int * index_of_the_particle, double mass, double x,
pt.m = mass;
pt.r = radius;
pt.hash = new_hash;
reb_add(codes[code_index].code, pt);
reb_simulation_add(codes[code_index].code, pt);
//std::cout<<"new particle :"<<pt.id<< " << "<<code_index<<" << "<<pt.x<<std::endl;
*index_of_the_particle = new_hash;

Expand Down Expand Up @@ -275,10 +275,10 @@ int _evolve_code(double _tmax, code_state * cs){
return(1);
}

reb_step(code); // 0 to not do timing within step
reb_simulation_step(code); // 0 to not do timing within step

if ((code->t+code->dt)*dtsign>=tmax*dtsign && exact_finish_time==1){
reb_integrator_synchronize(code);
reb_simulation_synchronize(code);
code->dt = tmax-code->t;
last_step++;
}else{
Expand Down Expand Up @@ -383,7 +383,7 @@ int _evolve_code(double _tmax, code_state * cs){
}
}
}
reb_integrator_synchronize(code);
reb_simulation_synchronize(code);
code->dt = code->dt_last_done;
get_kinetic_energy(cs->subset, &ke1);
//printf("Code time: %d , %f -> %f (%f,%f)\n",cs->subset , code->t, tmax, ke1, (ke1-ke)/ke);
Expand Down Expand Up @@ -437,7 +437,7 @@ int _delete_particle(int index_of_the_particle, int code_index){
if(code_index < 0 || code_index >= (signed) codes.size()){
return -10;
}
reb_remove_by_hash(codes[code_index].code, index_of_the_particle, keepSorted);
reb_simulation_remove_particle_by_hash(codes[code_index].code, index_of_the_particle, keepSorted);
return 0;
}

Expand Down Expand Up @@ -495,7 +495,7 @@ int get_state(int index_of_the_particle, double * mass, double * x,
#endif // COLLISIONS_NONE
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);
p = reb_simulation_particle_by_hash(cs.code, index_of_the_particle);
if (p != NULL) {
*subset = cs.subset;
break;
Expand Down Expand Up @@ -608,7 +608,7 @@ int get_subset(int index_of_the_particle, int * subset){
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);
p = reb_simulation_particle_by_hash(cs.code, index_of_the_particle);
if (p != NULL) {
*subset = cs.subset;
break;
Expand Down Expand Up @@ -638,7 +638,7 @@ int set_subset(int index_of_the_particle, int subset){
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);
p = reb_simulation_particle_by_hash(cs.code, index_of_the_particle);
if (p != NULL) {
if(cs.subset != subset) {return -2;}
break;
Expand All @@ -653,8 +653,8 @@ int cleanup_code() {
for( ReboundSimulationVector::iterator i = codes.begin(); i != codes.end(); i++) {
code_state cs = *i;
if(cs.code){
reb_remove_all(cs.code);
reb_free_simulation(cs.code);
reb_simulation_remove_all_particles(cs.code);
reb_simulation_free(cs.code);
cs.code = 0;
*i = cs;
}
Expand All @@ -679,7 +679,7 @@ int initialize_code(){
int nt = omp_get_max_threads();
omp_set_num_threads(nt);
#endif
reb_simulation * code = reb_create_simulation();
reb_simulation * code = reb_simulation_create();
codes.push_back(code_state(code));
code->integrator = reb_simulation::REB_INTEGRATOR_IAS15;
code->N_active = 0;
Expand Down Expand Up @@ -789,8 +789,8 @@ int set_velocity(int index_of_the_particle, double vx, double vy,
}

int new_subset(int * index, double time_offset) {
reb_simulation * code = reb_create_simulation();
reb_integrator_reset(code);
reb_simulation * code = reb_simulation_create();
reb_simulation_reset_integrator(code);
code->dt = timestep;
if(time_offset < 0) {time_offset = _time;}
code->integrator = reb_simulation::REB_INTEGRATOR_IAS15;
Expand All @@ -811,8 +811,8 @@ int stop_subset(int code_index) {
code_state cs = codes[code_index];
if(cs.code) {
reb_simulation * code = cs.code;
reb_remove_all(code);
reb_free_simulation(code);
reb_simulation_remove_all_particles(code);
reb_simulation_free(code);
cs.code = 0;
codes[code_index] = cs;
}
Expand Down Expand Up @@ -845,17 +845,34 @@ int _set_integrator(int value, int code_index){
code->integrator = reb_simulation::REB_INTEGRATOR_LEAPFROG;
break;
case 5:
code->integrator = reb_simulation::REB_INTEGRATOR_HERMES;
// This integrator was removed
return -1;
break;
case 6:
code->integrator = reb_simulation::REB_INTEGRATOR_WHFASTHELIO;
// This integrator was removed
return -1;
break;
case 7:
code->integrator = reb_simulation::REB_INTEGRATOR_NONE;
break;
case 8:
code->integrator = reb_simulation::REB_INTEGRATOR_JANUS;
break;
case 9:
code->integrator = reb_simulation::REB_INTEGRATOR_WHFAST512;
break;
case 10:
code->integrator = reb_simulation::REB_INTEGRATOR_SABA;
break;
case 11:
code->integrator = reb_simulation::REB_INTEGRATOR_MERCURIUS;
break;
case 12:
code->integrator = reb_simulation::REB_INTEGRATOR_EOS;
break;
case 13:
code->integrator = reb_simulation::REB_INTEGRATOR_BS;
break;
default:
code->integrator = reb_simulation::REB_INTEGRATOR_NONE;
return -1;
Expand Down Expand Up @@ -1014,7 +1031,7 @@ int set_boundary_size(double boundary_size, int code_index){
return -11;
}
reb_simulation * code = codes[code_index].code;
reb_configure_box(code,boundary_size,1,1,1);
reb_simulation_configure_box(code,boundary_size,1,1,1);
return 0;
}