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

Implemented ReflectiveShell as optional boundary #512

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ehlertdo
Copy link

Hi,
I needed a reflective boundary in my 3D simulations but in the form of a sphere instead of the currently available ReflectiveBox. Maybe this feature could be of more general interest since astrophysical environments are often more spherical than they are boxy.
Note that the shell reflects in both directions, i.e. outside particles are kept out and inside particles are kept in.
Best,
Domenik

PS: I copied the distance() and normal() functions from the Sphere class in Geometry.cpp but probably there's a more elegant way to do this with inheritance.

cr_tracks
cr_tracks

Copy link
Member

@JulienDoerner JulienDoerner left a comment

Choose a reason for hiding this comment

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

Hey @ehlertdo,

Thanks for sharing your implementation. In general, the tool looks good for me.

I have tested it and found some deviation when using large steps. I guess this is caused by the position where you calculate the normal. This is given at the current position which is not necessarily a position on the sphere. Would you have an idea how to fix this, or can this be neglected in regular cases?
image

Beside this I would ask you to add your changes to the Changes.md and maybe you can provide a simple test case as a notebook and as unit test.

@JulienDoerner JulienDoerner self-assigned this Feb 26, 2025
@ehlertdo
Copy link
Author

ehlertdo commented Mar 5, 2025

Hi Julien,
I did indeed approximate the true normal to the sphere at the point of intersection by the normal at the final point of the particle. I have now added a few lines to calculate the intersection point (requires solving a quadratic equation) before constructing the normal which, in my tests reduces the error at larger step sizes significantly. I also added a unit test for the ReflectiveShell and updated the changelog and doc page.

@param cen vector corresponding to the center of the sphere
@param r value corresponding to the radius of the shell
*/
ReflectiveShell(Vector3d cen, double r);
Copy link
Member

Choose a reason for hiding this comment

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

Code Style: use full words for variable names: center instead of cen

@@ -40,6 +40,58 @@ std::string PeriodicBox::getDescription() const {
return s.str();
}


ReflectiveShell::ReflectiveShell(Vector3d cen, double r) :
Copy link
Member

Choose a reason for hiding this comment

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

Code Style

Vector3d previousPosition = c->previous.getPosition();
// get point where trajectory intersects the shell boundary
double p_half = previousPosition.dot(currentDirection) / currentDirection.getR2();
double q = (previousPosition.getR2() - radius*radius) / currentDirection.getR2();
Copy link
Member

Choose a reason for hiding this comment

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

Code Style: spaces around operators. Use a * b instead of a*b.
This also happens below. I mark it only once here.

std::string ReflectiveShell::getDescription() const {
std::stringstream ss;
ss << "Reflective sphere: " << std::endl
<< " Center: " << center << std::endl
Copy link
Member

Choose a reason for hiding this comment

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

Is it useful to define some default unit for the printing like kpc or Mpc?


shell.process(&c);

EXPECT_NEAR(89.9965, c.current.getPosition().x, 1e-4);
Copy link
Member

Choose a reason for hiding this comment

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

Please leave a comment in the code on how you get to those expected values (or even calculate them with the trigonometric functions here).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants