Skip to content

Commit 6510b63

Browse files
authored
Fix bug of adding particles in the middle of simulations for PeTar (#988)
1 parent 0e05aa5 commit 6510b63

5 files changed

Lines changed: 134 additions & 20 deletions

File tree

src/amuse/community/petar/Makefile

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ LDFLAGS += -lm $(MUSE_LD_FLAGS)
4040

4141
OBJS = interface.o
4242

43-
FILELIST=interface.cc interface.py interface.h
43+
DIRLIST=src/PeTar src/SDAR src/FDPS
4444

4545
CODELIB =
4646

@@ -60,18 +60,16 @@ distclean: clean
6060

6161
DOWNLOAD_FROM_WEB = $(PYTHON) ./download.py
6262

63-
download:
64-
$(RM) -Rf .pc
65-
$(RM) -Rf src
66-
mkdir src
63+
$(DIRLIST):
64+
# $(RM) -Rf .pc
65+
# $(RM) -Rf src
66+
# mkdir src
6767
$(DOWNLOAD_FROM_WEB)
6868

6969
__init__.py:
7070
touch $@
7171

72-
$(FILELIST): |download
73-
74-
src/PeTar/src/get_version.hpp: src/PeTar/src/get_version.hpp.in |src/PeTar
72+
src/PeTar/src/get_version.hpp: |src/PeTar
7573
sed 's/@VERSION@/'`cat src/PeTar/VERSION`'_'`cat src/SDAR/VERSION`'/g' src/PeTar/src/get_version.hpp.in >src/PeTar/src/get_version.hpp
7674

7775
test_interface: $(OBJS) test_interface.o
@@ -86,5 +84,5 @@ worker_code.h: interface.py
8684
petar_worker: worker_code.cc worker_code.h $(CODELIB) $(OBJS) interface.h
8785
$(MPICXX) $(CXXFLAGS) $(SC_FLAGS) $(AM_CFLAGS) $(LDFLAGS) $< $(OBJS) $(CODELIB) -o $@ $(SC_MPI_CLIBS) $(LIBS) $(AM_LIBS)
8886

89-
interface.o: interface.cc src/PeTar/src/get_version.hpp
87+
interface.o: interface.cc src/PeTar/src/get_version.hpp |$(DIRLIST)
9088
$(MPICXX) $(CXXFLAGS) $(SC_FLAGS) -c -o $@ $<

src/amuse/community/petar/download.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,25 @@ def unpack_downloaded_file(self, filename, name, version):
4747
print("done")
4848

4949
def start(self):
50-
if os.path.exists('src'):
51-
counter = 0
52-
while os.path.exists('src.{0}'.format(counter)):
53-
counter += 1
54-
if counter > 100:
55-
print("too many backup directories")
56-
break
57-
os.rename('src', 'src.{0}'.format(counter))
58-
59-
os.mkdir('src')
50+
if not os.path.exists('src'):
51+
os.mkdir('src')
52+
# if not update_flag:
53+
# return
54+
# counter = 0
55+
# while os.path.exists('src.{0}'.format(counter)):
56+
# counter += 1
57+
# if counter > 100:
58+
# print("too many backup directories")
59+
# break
60+
# os.rename('src', 'src.{0}'.format(counter))
6061

6162
for i, url_template in enumerate(self.url_template):
6263
url = url_template.format(version=self.version[i])
6364
filename = self.filename_template.format(version=self.version[i])
6465
filepath = os.path.join(self.src_directory(), filename)
66+
if os.path.exists('src/'+self.name[i]):
67+
print("src/%s exist" % self.name[i])
68+
continue
6569
print(
6670
"downloading version", self.version[i],
6771
"from", url, "to", filename

src/amuse/community/petar/interface.cc

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern "C" {
2323

2424
// flags
2525
static bool particle_list_change_flag=true;
26+
static bool initial_particle_flag=false;
2627

2728
// common
2829

@@ -102,6 +103,8 @@ extern "C" {
102103
if(ptr->my_rank==0) std::cout<<"PETAR: recommit_parameters start\n";
103104
#endif
104105
ptr->input_parameters.n_glb.value = ptr->stat.n_real_glb;
106+
ptr->input_parameters.update_changeover_flag = true;
107+
ptr->input_parameters.update_rsearch_flag = true;
105108
ptr->initialParameters();
106109
ptr->initial_step_flag = false;
107110

@@ -123,6 +126,15 @@ extern "C" {
123126

124127
int new_particle(int* index_of_the_particle, double mass, double x, double y, double z, double vx, double vy, double vz, double radius) {
125128
// if not yet initial the system
129+
#ifdef INTERFACE_DEBUG_PRINT
130+
if (!ptr->read_data_flag && ptr->my_rank==0) {
131+
std::cout<<"New particle, rank "<<ptr->my_rank<<std::endl;
132+
std::cout<<std::setw(20)<<"ID";
133+
ParticleBase::printColumnTitle(std::cout);
134+
std::cout<<std::endl;
135+
}
136+
#endif
137+
126138
ptr->read_data_flag = true;
127139

128140
PS::S64 id_offset = ptr->input_parameters.id_offset.value;
@@ -152,10 +164,25 @@ extern "C" {
152164
p.rank_org = ptr->my_rank;
153165
p.adr = n_loc;
154166

167+
if (initial_particle_flag) {
168+
PS::F64 m_fac = p.mass*Ptcl::mean_mass_inv;
169+
PS::F64 r_out = ptr->input_parameters.r_out.value;
170+
PS::F64 r_in = r_out*ptr->input_parameters.ratio_r_cut.value;
171+
p.changeover.setR(m_fac, r_in, r_out);
172+
p.calcRSearch(ptr->input_parameters.dt_soft.value);
173+
}
174+
155175
ptr->system_soft.addOneParticle(p);
156176

157177
ptr->stat.n_real_loc++;
158178
*index_of_the_particle = p.id;
179+
#ifdef INTERFACE_DEBUG_PRINT
180+
std::cout<<std::setprecision(14);
181+
std::cout<<std::setw(20)<<p.id;
182+
p.ParticleBase::printColumn(std::cout);
183+
std::cout<<std::endl;
184+
#endif
185+
159186
}
160187
ptr->stat.n_real_glb++;
161188

@@ -548,7 +575,7 @@ extern "C" {
548575

549576
int evolve_model(double time_next) {
550577
#ifdef INTERFACE_DEBUG_PRINT
551-
if(ptr->my_rank==0) std::cout<<"PETAR: evolve models start\n";
578+
if(ptr->my_rank==0) std::cout<<"PETAR: evolve models to "<<time_next<< "start\n";
552579
#endif
553580

554581
if (ptr->stat.n_real_glb==0) {// escape if no particle
@@ -731,6 +758,7 @@ extern "C" {
731758
ptr->initialStep();
732759
ptr->reconstructIdAdrMap();
733760
particle_list_change_flag = false;
761+
initial_particle_flag = true;
734762
#ifdef INTERFACE_DEBUG_PRINT
735763
if(ptr->my_rank==0) std::cout<<"PETAR: commit_particles end\n";
736764
#endif
@@ -859,6 +887,17 @@ extern "C" {
859887
return 0;
860888
}
861889

890+
int set_output_step(double dt_snap) {
891+
ptr->input_parameters.dt_snap.value = dt_snap;
892+
return 0;
893+
}
894+
895+
int get_output_step(double* dt_snap) {
896+
*dt_snap = ptr->input_parameters.dt_snap.value;
897+
return 0;
898+
}
899+
900+
862901
//// set gravitational constant
863902
//int set_gravitational_constant(double G) {
864903
// ptr->input_parameters.unit_set.value=-1;

src/amuse/community/petar/interface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ int get_tree_step(double * dt_soft);
8787

8888
int set_tree_step(double dt_soft);
8989

90+
int get_output_step(double * dt_output);
91+
92+
int set_output_step(double dt_output);
93+
9094
int get_kinetic_energy(double * kinetic_energy);
9195

9296
int get_potential_energy(double * potential_energy);

src/amuse/community/petar/interface.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,48 @@ def set_tree_step():
356356
"""
357357
return function
358358

359+
@legacy_function
360+
def get_output_step():
361+
"""
362+
Get dt_output, the PeTar internal output time step (0.125)
363+
"""
364+
function = LegacyFunctionSpecification()
365+
function.addParameter(
366+
'dt_output', dtype='float64', direction=function.OUT,
367+
description=(
368+
"dt_output, the PeTar internal output time step (0.125)"
369+
)
370+
)
371+
function.result_type = 'int32'
372+
function.result_doc = """
373+
0 - OK
374+
the parameter was retrieved
375+
-1 - ERROR
376+
could not retrieve parameter
377+
"""
378+
return function
379+
380+
@legacy_function
381+
def set_output_step():
382+
"""
383+
Set dt_output, the PeTar internal output time step (0.125)
384+
"""
385+
function = LegacyFunctionSpecification()
386+
function.addParameter(
387+
'dt_output', dtype='float64', direction=function.IN,
388+
description=(
389+
"dt_output, the PeTar internal output time step (0.125)"
390+
)
391+
)
392+
function.result_type = 'int32'
393+
function.result_doc = """
394+
0 - OK
395+
the parameter was set
396+
-1 - ERROR
397+
could not set parameter
398+
"""
399+
return function
400+
359401

360402
class petar(GravitationalDynamics, GravityFieldCode):
361403

@@ -443,6 +485,14 @@ def define_parameters(self, handler):
443485
default_value=0.0 | nbody_system.time
444486
)
445487

488+
handler.add_method_parameter(
489+
"get_output_step",
490+
"set_output_step",
491+
"dt_output",
492+
"PeTar internal output time step (0.125)",
493+
default_value=0.125 | nbody_system.time
494+
)
495+
446496
def define_methods(self, handler):
447497
GravitationalDynamics.define_methods(self, handler)
448498
self.stopping_conditions.define_methods(handler)
@@ -542,6 +592,25 @@ def define_methods(self, handler):
542592
)
543593
)
544594

595+
handler.add_method(
596+
"set_output_step",
597+
(
598+
nbody_system.time,
599+
),
600+
(
601+
handler.ERROR_CODE,
602+
)
603+
)
604+
605+
handler.add_method(
606+
"get_output_step",
607+
(),
608+
(
609+
nbody_system.time,
610+
handler.ERROR_CODE,
611+
)
612+
)
613+
545614
def define_particle_sets(self, handler):
546615
GravitationalDynamics.define_particle_sets(self, handler)
547616
self.stopping_conditions.define_particle_set(handler)

0 commit comments

Comments
 (0)