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

Particle masses bugfix #89

Merged
merged 69 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
f47382f
Adds has_attribute() and has_constant() methods to the Particle and P…
stefanarridge Apr 13, 2021
5efcce4
EnzoMethodPmDeposit treats particle mass as mass always
stefanarridge Apr 13, 2021
1ea6e9c
Changed/ removed comments in EnzoMethodPmDeposit
stefanarridge Apr 28, 2021
b9d7d0c
Changed 'divide by inv_vol' to 'multiply by inv_vol' in EnzoMethodPmD…
stefanarridge Apr 28, 2021
fdedf5e
Changed 'Edited' to '@author'
stefanarridge May 5, 2021
aad8a46
Removes a comment from EnzoMethodPmDeposit
stefanarridge May 18, 2021
a8c5e3d
Merged with main
stefanarridge Jun 2, 2021
c7ff73c
EnzoMethodPmDeposit reports negative densities for rank = 1,2,3, and …
stefanarridge Jun 10, 2021
3df4172
Fixed bug in ParticleDescr is_attribute and is_constant methods
stefanarridge Jun 10, 2021
79963a5
Moved lines of code which calculate the inverse cell volume in EnzoMe…
stefanarridge Jun 10, 2021
0fc15c0
EnzoMethodPmDeposit allocates the pmass array once per particle type …
stefanarridge Jun 10, 2021
39b80f0
In EnzoMethodPmDeposit, the timestep used to advance the gas density …
stefanarridge Jun 23, 2021
cc7a53d
Merge branch 'main' into particle_masses
stefanarridge Jan 24, 2022
af82abf
pm_deposit method treats the gas density field the same as in the mai…
stefanarridge Jan 24, 2022
694100e
Fixed typo in src/Enzo/enzo_EnzoMethodPmDepositDeposit.cpp which prev…
stefanarridge Feb 14, 2022
af96ff2
Merge branch 'main' into particle_masses
stefanarridge Feb 14, 2022
616d3f0
Renamed ParticleDescr::has_constant to ParticleDescr::is_constant and…
stefanarridge Feb 14, 2022
124104f
Simplified ParticleDescr::is_attribute. No longer uses the variable.…
stefanarridge Feb 14, 2022
3a6670c
Simplified ParticleDescr::is_attribute. No longer uses the const vari…
stefanarridge Feb 14, 2022
4f22911
Merge branch 'particle-descr-helper-functions-fix' of github.com:stef…
stefanarridge Feb 15, 2022
0a54a03
Merge branch 'main' into particle_masses
stefanarridge Feb 24, 2022
f287107
Merge branch 'main' into particle-descr-helper-functions-fix
stefanarridge Feb 24, 2022
698d437
Merge branch 'particle-descr-helper-functions-fix' into particle_masses
stefanarridge Feb 24, 2022
53c00f7
Renamed ParticleDescr::is_constant and ParticleDescr::is_attribute fu…
stefanarridge Feb 25, 2022
916625a
pm_deposit method allows particles in the "has_mass" group to have a …
stefanarridge Feb 25, 2022
26272c9
Renamed "has_mass" particle group to "is_gravitating"
stefanarridge Mar 1, 2022
798917a
Added check for whether particle types in "is_gravitating" group have…
stefanarridge Mar 1, 2022
bd6c90f
Edited parameter files to change "mass" attributes / constants to "de…
stefanarridge Mar 1, 2022
0df1737
Fixed mistake in ASSERT call in constructor of EnzoMethodPmDeposit, w…
stefanarridge Mar 1, 2022
1ab275a
Removed input/MergeStars/merge_stars_test.in
stefanarridge Mar 1, 2022
3e3b63b
Removed star particles from "is_gravitating" group in input/MergeStar…
stefanarridge Mar 1, 2022
bfcd977
Changed "mass" to "is_gravitating" in preamble of src/Enzo/enzo_EnzoM…
stefanarridge Mar 2, 2022
6145676
pm_deposit method pre-computes a "density scale factor", equal to 2^(…
stefanarridge Mar 2, 2022
2878937
Cleaned up src/Enzo/enzo_EnzoMethodPmDeposit.cpp
stefanarridge Mar 2, 2022
0e859f0
Added check for whether star particles exist and whether they have a …
stefanarridge Mar 3, 2022
a213537
Added check for whether star particles exist and whether they have a …
stefanarridge Mar 3, 2022
d4af1e8
Added check for whether star particles exist and whether they have a …
stefanarridge Mar 3, 2022
c09ea84
Renamed "particle with mass" to "gravitating particles" and "num_mass…
stefanarridge Mar 3, 2022
27d8c37
Added check for whether star particles exist and whether they have a …
stefanarridge Mar 4, 2022
cdc4c95
Added check for whether star particles exist and whether they have a …
stefanarridge Mar 4, 2022
917ef3f
Added check for whether star particles exist and whether they have a …
stefanarridge Mar 4, 2022
807fa89
Added check for whether star particles exist and whether they have a …
stefanarridge Mar 4, 2022
95c26f9
Added check for whether star particles exist and whether they have a …
stefanarridge Mar 4, 2022
616cbef
Updated user guide for pm_deposit, pm_update, and merge_stars methods.
stefanarridge Mar 8, 2022
e0fe776
Updated reference guide for merge_stars_test initializer.
stefanarridge Mar 8, 2022
089cbd8
Update src/Enzo/enzo_EnzoMethodStarMaker.cpp - remove whitespace in l…
stefanarridge Apr 5, 2022
43d8b60
Update src/Cello/data_Particle.hpp - removed whitespace in line 129
stefanarridge Apr 5, 2022
c5d0d44
Update src/Enzo/enzo_EnzoMethodPmDeposit.cpp - removed whitespace in …
stefanarridge Apr 5, 2022
023f925
Update src/Enzo/enzo_EnzoMethodPmDeposit.cpp - removed whitespace in …
stefanarridge Apr 5, 2022
ba65b19
Update src/Enzo/enzo_EnzoMethodPmDeposit.cpp - remove whitespace in l…
stefanarridge Apr 5, 2022
6d2f6ae
Update src/Enzo/enzo_EnzoMethodPmDeposit.cpp - remove whitepace in li…
stefanarridge Apr 5, 2022
de8db5e
Update src/Enzo/enzo_EnzoMethodPmDeposit.cpp - removed whitespace in …
stefanarridge Apr 5, 2022
ba089f9
Update src/Enzo/enzo_EnzoMethodPmDeposit.cpp - removed whitespace in …
stefanarridge Apr 5, 2022
a660481
Update src/Enzo/enzo_EnzoMethodPmDeposit.cpp - removed whitespace in …
stefanarridge Apr 5, 2022
232532e
Update src/Enzo/enzo_EnzoMethodPmDeposit.cpp - removed whitespace in …
stefanarridge Apr 5, 2022
3fbe230
Update src/Enzo/enzo_EnzoMethodPmDeposit.cpp - removed whitespace in …
stefanarridge Apr 5, 2022
6b7af1b
Update src/Enzo/enzo_EnzoMethodPmDeposit.cpp - removed whitespace in …
stefanarridge Apr 5, 2022
95e487c
Update src/Enzo/enzo_EnzoMethodPmDeposit.cpp -fixed indentation on li…
stefanarridge Apr 5, 2022
86c6658
Update src/Enzo/enzo_EnzoMethodPmDeposit.cpp - removed whitespace on …
stefanarridge Apr 5, 2022
8b16384
Update src/Enzo/enzo_EnzoMethodPmDeposit.cpp - removed whitespace in …
stefanarridge Apr 5, 2022
0e4737e
Update src/Enzo/enzo_EnzoMethodPmDeposit.cpp - removed whitespace in …
stefanarridge Apr 5, 2022
1405739
Update src/Enzo/enzo_EnzoMethodPmDeposit.cpp - removed whitespace in …
stefanarridge Apr 5, 2022
76cef5d
Added check_particle_attribute to enzo namespace, which takes the nam…
stefanarridge Apr 6, 2022
33b4dee
Changed EnzoMethodPmDeposit::compute so that it uses the WARNING2 mac…
stefanarridge Apr 6, 2022
1296b65
Moved "check_particle_attribute" from the enzo namespace to a member …
stefanarridge Apr 13, 2022
af6e42f
The ".libconfig" and ".parameters" output files now always contain a …
stefanarridge May 11, 2022
06f4fe8
Updated "pm_deposit" method to remove the option for particles to hav…
stefanarridge May 12, 2022
2ff1cdf
Edited input/Cosmology/method_cosmology.incl, so that the particles h…
stefanarridge May 12, 2022
41bd6e0
Updated documention.
stefanarridge May 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Cello/data_Particle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ class Particle {
int num_attributes(int it) const
{ return particle_descr_->num_attributes(it); }

/// Check if attribute exists

bool has_attribute (int it, std::string attribute) const
{ return particle_descr_->has_attribute(it,attribute); }

/// Return the index for the given attribute

int attribute_index (int it, std::string attribute) const
Expand Down Expand Up @@ -197,6 +202,10 @@ class Particle {
int constant_offset(int it, int ic) const
{ return particle_descr_->constant_offset(it,ic); }

/// Check if given constant exists

bool has_constant(int it, std::string constant) const
{ return particle_descr_->has_constant(it,constant); }

//--------------------------------------------------
// BYTES
Expand Down
35 changes: 35 additions & 0 deletions src/Cello/data_ParticleDescr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,24 @@ int ParticleDescr::attribute_index (int it, std::string attribute_name) const

//----------------------------------------------------------------------

bool ParticleDescr::has_attribute(int it, std::string attribute_name) const
stefanarridge marked this conversation as resolved.
Show resolved Hide resolved
{
ASSERT1("ParticleDescr::is_attribute",
Copy link
Contributor

@jwise77 jwise77 May 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mismatch, but I'd just go with is_attribute to be consistent with #49

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I named has_attribute as such to maintain consistency with the has_constant method. I have now decided to name the methods 'is_attribute' and 'is_constant' instead.

"Trying to access unknown particle type '%s'",
type_name(it).c_str(),
mabruzzo marked this conversation as resolved.
Show resolved Hide resolved
(0 <= it && it < num_types()));

auto iter=attribute_index_[it].find(attribute_name);

int index = (iter != attribute_index_[it].end()) ? iter->second : -1;

static int count[CONFIG_NODE_SIZE] = {0};

return !((index == -1) && (count[cello::index_static()]++ < 10));
}

//----------------------------------------------------------------------

std::string ParticleDescr::attribute_name (int it, int ia) const
{
ASSERT2("ParticleDescr::attribute_name",
Expand Down Expand Up @@ -390,6 +408,23 @@ int ParticleDescr::constant_offset (int it, int ic) const
return constant_offset_[it][ic];
}

//----------------------------------------------------------------------

bool ParticleDescr::has_constant(int it, std::string constant_name) const
{
ASSERT1("ParticleDescr::constant_index",
"Trying to access unknown particle type '%s'",
mabruzzo marked this conversation as resolved.
Show resolved Hide resolved
type_name(it).c_str(),
(0 <= it && it < num_types()));

auto iter=constant_index_[it].find(constant_name);

bool check = (iter != constant_index_[it].end()) ? true : false;

return check;
}


//----------------------------------------------------------------------
// Bytes
//----------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions src/Cello/data_ParticleDescr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class ParticleDescr {
/// Return a pointer to the given constant for the given type
char * constant_value (int it, int ic);

/// Check if given constant exists for the given particle type

bool has_constant (int it, std::string constant) const;

//--------------------------------------------------
// ATTRIBUTES
//--------------------------------------------------
Expand All @@ -94,6 +98,10 @@ class ParticleDescr {

int num_attributes(int it) const;

/// Check if attribute exists

bool has_attribute (int it, std::string attribute) const;

/// Return the index for the given attribute

int attribute_index (int it, std::string attribute) const;
Expand Down
170 changes: 103 additions & 67 deletions src/Enzo/enzo_EnzoMethodPmDeposit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/// @file enzo_EnzoMethodPmDeposit.cpp
/// @author James Bordner (jobordner@ucsd.edu)
/// @author Stefan Arridge (stefan.arridge@gmail.com)
/// @date Fri Apr 2 17:05:23 PDT 2010
/// @brief Implements the EnzoMethodPmDeposit class
///
Expand Down Expand Up @@ -104,22 +105,38 @@ void EnzoMethodPmDeposit::compute ( Block * block) throw()
block->lower(&xm,&ym,&zm);
block->upper(&xp,&yp,&zp);
block->cell_width(&hx,&hy,&hz);


// declare particle position arrays

//--------------------------------------------------
// Add "dark" particle density
//--------------------------------------------------
// Get cosmological scale factors, if cosmology is turned on
enzo_float cosmo_a=1.0;
enzo_float cosmo_dadt=0.0;
EnzoPhysicsCosmology * cosmology = enzo::cosmology();

stefanarridge marked this conversation as resolved.
Show resolved Hide resolved
if (particle.type_exists ("dark")) {
const int it = particle.type_index ("dark");
if (cosmology) {
cosmology->compute_expansion_factor(&cosmo_a,&cosmo_dadt,
block->time() + alpha_*block->dt());
}

const int ic_mass = particle.constant_index (it,"mass");
double inv_vol = 1.0;
if (rank >= 1) inv_vol /= hx;
if (rank >= 2) inv_vol /= hy;
if (rank >= 3) inv_vol /= hz;
mabruzzo marked this conversation as resolved.
Show resolved Hide resolved

enzo_float dens = *((enzo_float *)(particle.constant_value (it,ic_mass)));
const double dt = alpha_ * block->dt() / cosmo_a;

// check precisions match

// Get the number of particle types in the 'has_mass' group
ParticleDescr * particle_descr = cello::particle_descr();
Grouping * particle_groups = particle_descr->groups();
int num_mass = particle_groups->size("has_mass");

// Loop over all particles that have mass
for (int ipt = 0; ipt < num_mass; ipt++) {
const int it = particle.type_index(particle_groups->item("has_mass",ipt));

// Index for mass attribute / constant
int im = 0;

// check correct precision for position
int ia = particle.attribute_index(it,"x");
int ba = particle.attribute_bytes(it,ia); // "bytes (actual)"
int be = sizeof(enzo_float); // "bytes (expected)"
Expand All @@ -132,40 +149,51 @@ void EnzoMethodPmDeposit::compute ( Block * block) throw()
((be == 4) ? "single" : ((be == 8) ? "double" : "quadruple")),
(ba == be));

// Accumulate particle density using CIC

enzo_float cosmo_a=1.0;
enzo_float cosmo_dadt=0.0;
EnzoPhysicsCosmology * cosmology = enzo::cosmology();

if (cosmology) {

double time = block->time();
double dt = block->dt();
cosmology->compute_expansion_factor (&cosmo_a,&cosmo_dadt,time+alpha_*dt);

}
if (rank >= 1) hx *= cosmo_a;
if (rank >= 2) hy *= cosmo_a;
if (rank >= 3) hz *= cosmo_a;

const double dt = alpha_ * block->dt() / cosmo_a;

// Scale mass by volume if particle value is mass instead of density

int level = block->level();

// Required for Cosmology ("mass" is mass)
// Not for Collapse ("mass" is density)
if (cosmology) {
dens *= std::pow(2.0,rank*level);
}
// Loop over batches
for (int ib=0; ib<particle.num_batches(it); ib++) {
// Pointer to array of particle masses
enzo_float * pmass = NULL;
const int np = particle.num_particles(it,ib);

// Accumulated single velocity array for Baryon deposit
// Particle masses can be either a constant value for all particles,
// or an attribrute array
if (particle.has_constant (it,"mass")){

for (int ib=0; ib<particle.num_batches(it); ib++) {
// Check if particle also has a mass attribute
ASSERT1("EnzoMethodPmDeposit::compute",
"Particle type %s has both a constant and an attribute called"
"'mass'. Exiting.", particle.type_name(it).c_str(),
particle.has_attribute(it,"mass"));

// In this case we fill an array of length np with the constant
// value
pmass = new enzo_float[np];
im = particle.constant_index(it,"mass");
for (int ip = 0; ip<np; ip++)
pmass[ip] = *((enzo_float *)(particle.constant_value (it,im)));
mabruzzo marked this conversation as resolved.
Show resolved Hide resolved

} else if (particle.has_attribute(it,"mass")) {

const int np = particle.num_particles(it,ib);
// In this case we set to pointer to the attribute array
im = particle.attribute_index(it,"mass");
pmass = (enzo_float *) particle.attribute_array( it, im, ib);
}
else {
ERROR1("EnzoMethodPmDeposit::compute",
"Particle type %s has neither a constant nor an attribute"
"called 'mass', yet is in the 'has_mass' group",
particle.type_name(it).c_str());
}

// If mass is a constant, we simply loop through the pmass
// array, and so dm = 1. If its an attribute, we need to get
// the stride length
const int dm = particle.has_attribute(it,"mass") ?
particle.stride(it,im) : 1;


// Allocate particle masses to the grid with CIC scheme

if (rank == 1) {

Expand All @@ -180,6 +208,9 @@ void EnzoMethodPmDeposit::compute ( Block * block) throw()

for (int ip=0; ip<np; ip++) {

// Stefan: Unsure about this line, doesn't match what's in the
// Enzo paper, which says that particles are drifted by half a timestep
// before being deposited to the grid
double x = xa[ip*dp] + vxa[ip*dv]*dt;

double tx = nx*(x - xm) / (xp - xm) - 0.5;
Expand All @@ -191,8 +222,8 @@ void EnzoMethodPmDeposit::compute ( Block * block) throw()
double x0 = 1.0 - (tx - floor(tx));
double x1 = 1.0 - x0;

de_p[ix0] += dens*x0;
de_p[ix1] += dens*x1;
de_p[ix0] += pmass[ip*dm]*inv_vol*x0;
de_p[ix1] += pmass[ip*dm]*inv_vol*x1;

}

Expand All @@ -215,7 +246,7 @@ void EnzoMethodPmDeposit::compute ( Block * block) throw()

const int dp = particle.stride(it,ia_x);
const int dv = particle.stride(it,ia_vx);

for (int ip=0; ip<np; ip++) {

double x = xa[ip*dp] + vxa[ip*dv]*dt;
Expand All @@ -236,30 +267,31 @@ void EnzoMethodPmDeposit::compute ( Block * block) throw()
double x1 = 1.0 - x0;
double y1 = 1.0 - y0;

if ( dens < 0.0) {
CkPrintf ("%s:%d ERROR: dens = %f\n", __FILE__,__LINE__,dens);
if ( pmass[ip*dm]*inv_vol < 0.0) {
CkPrintf ("%s:%d ERROR: pmass[%d]*inv_vol = %f\n",
__FILE__,__LINE__,ip*dm,pmass[ip*dm]*inv_vol);
}

de_p[ix0+mx*iy0] += dens*x0*y0;
de_p[ix1+mx*iy0] += dens*x1*y0;
de_p[ix0+mx*iy1] += dens*x0*y1;
de_p[ix1+mx*iy1] += dens*x1*y1;
de_p[ix0+mx*iy0] += pmass[ip*dm]*inv_vol*x0*y0;
de_p[ix1+mx*iy0] += pmass[ip*dm]*inv_vol*x1*y0;
de_p[ix0+mx*iy1] += pmass[ip*dm]*inv_vol*x0*y1;
de_p[ix1+mx*iy1] += pmass[ip*dm]*inv_vol*x1*y1;

if ( de_p[ix0+mx*iy0] < 0.0) {
CkPrintf ("%s:%d ERROR: de_p %d %d = %f\n",
__FILE__,__LINE__,ix0,iy0,dens);
__FILE__,__LINE__,ix0,iy0,pmass[ip*dm]*inv_vol);
}
if ( de_p[ix1+mx*iy0] < 0.0) {
CkPrintf ("%s:%d ERROR: de_p %d %d = %f\n",
__FILE__,__LINE__,ix1,iy0,dens);
__FILE__,__LINE__,ix1,iy0,pmass[ip*dm]*inv_vol);
}
if ( de_p[ix0+mx*iy1] < 0.0) {
CkPrintf ("%s:%d ERROR: de_p %d %d = %f\n",
__FILE__,__LINE__,ix0,iy1,dens);
__FILE__,__LINE__,ix0,iy1,pmass[ip*dm]*inv_vol);
}
if ( de_p[ix1+mx*iy1] < 0.0) {
CkPrintf ("%s:%d ERROR: de_p %d %d = %f\n",
__FILE__,__LINE__,ix1,iy1,dens);
__FILE__,__LINE__,ix1,iy1,pmass[ip*dm]*inv_vol);
}
}

Expand Down Expand Up @@ -318,19 +350,23 @@ void EnzoMethodPmDeposit::compute ( Block * block) throw()
double y1 = 1.0 - y0;
double z1 = 1.0 - z0;

de_p[ix0+mx*(iy0+my*iz0)] += dens*x0*y0*z0;
de_p[ix1+mx*(iy0+my*iz0)] += dens*x1*y0*z0;
de_p[ix0+mx*(iy1+my*iz0)] += dens*x0*y1*z0;
de_p[ix1+mx*(iy1+my*iz0)] += dens*x1*y1*z0;
de_p[ix0+mx*(iy0+my*iz1)] += dens*x0*y0*z1;
de_p[ix1+mx*(iy0+my*iz1)] += dens*x1*y0*z1;
de_p[ix0+mx*(iy1+my*iz1)] += dens*x0*y1*z1;
de_p[ix1+mx*(iy1+my*iz1)] += dens*x1*y1*z1;

}
}
}
}
de_p[ix0+mx*(iy0+my*iz0)] += pmass[ip*dm]*inv_vol*x0*y0*z0;
de_p[ix1+mx*(iy0+my*iz0)] += pmass[ip*dm]*inv_vol*x1*y0*z0;
de_p[ix0+mx*(iy1+my*iz0)] += pmass[ip*dm]*inv_vol*x0*y1*z0;
de_p[ix1+mx*(iy1+my*iz0)] += pmass[ip*dm]*inv_vol*x1*y1*z0;
de_p[ix0+mx*(iy0+my*iz1)] += pmass[ip*dm]*inv_vol*x0*y0*z1;
de_p[ix1+mx*(iy0+my*iz1)] += pmass[ip*dm]*inv_vol*x1*y0*z1;
de_p[ix0+mx*(iy1+my*iz1)] += pmass[ip*dm]*inv_vol*x0*y1*z1;
de_p[ix1+mx*(iy1+my*iz1)] += pmass[ip*dm]*inv_vol*x1*y1*z1;

} // Loop over particle in batch
} // if rank == 3

// If constant mass, delete the pmass array
if (particle.has_constant(it,"mass")) delete [] pmass;

} // Loop over batches
} // Loop over particle types in 'has_mass' group

//--------------------------------------------------
// Add gas density
Expand Down