Skip to content

Create torus_x.py#364

Draft
Talen-Ayers wants to merge 15 commits intoCEMeNT-PSAAP:devfrom
Talen-Ayers:dev-workspace
Draft

Create torus_x.py#364
Talen-Ayers wants to merge 15 commits intoCEMeNT-PSAAP:devfrom
Talen-Ayers:dev-workspace

Conversation

@Talen-Ayers
Copy link

Added the torus surface to this new fork of the dev branch in order to create a draft pull request for the project.

Added the torus surface to this new fork of the dev branch in order to create a draft pull request for the project.
@melekderman melekderman self-requested a review February 2, 2026 19:26
@melekderman melekderman added the enhancement New feature or request label Feb 2, 2026
@ilhamv ilhamv assigned ilhamv and unassigned ilhamv Feb 4, 2026
@ilhamv ilhamv self-requested a review February 4, 2026 11:40
@ilhamv
Copy link
Member

ilhamv commented Feb 4, 2026

Thanks for working on this, @Talen-Ayers !

"""
Torus: Implicit equation of a torus radially symmetric about the z-axis in the cartesian plane

r^2 = f(x, y, z) = ( sqrt( (x - A)^2 + (y - B)^2) - R )^2 + (z - C)^2
Copy link
Member

Choose a reason for hiding this comment

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

Should we move the r^2 to the right-hand side to get the conventional behavior of a surface equation? That is:

  • f(x,y,z) = 0 --> (x,y,z) is on the torus
  • f(x,y,z) < 0 --> (x,y,z) is inside the torus
  • f(x,y,z) > 0 --> (x,y,z) is outside the torus

"""
Torus: Implicit equation of a torus radially symmetric about the z-axis in the cartesian plane

r^2 = f(x, y, z) = ( sqrt( (x - A)^2 + (y - B)^2) - R )^2 + (z - C)^2
Copy link
Member

Choose a reason for hiding this comment

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

Should this be the formula for torus-z?

return INF

# Using the smallest root to get the value of t at the first point of intersection
# If the direction vector is normalized, the distance to intersection is just the value of t
Copy link
Member

Choose a reason for hiding this comment

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

The direction vector is always normalized.

# Due to the sqrt in the derivative functions, if the particle is not in the x-y space where the torus has z values, it will result in complex numbers
# Specificaly the term: math.sqrt(r**2 - (R - inv_root)**2), where inverse root = math.sqrt((A-x)**2 + (B-y)**2)
@njit
def get_normal_component(particle_container, surface):
Copy link
Member

Choose a reason for hiding this comment

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

Note that, whenever this function is called during the transport, the particle position (x,y,z) would yield
f(x,y,z) = 0.0 +- COINCIDENCE_TOLERANCE (the particle is on the surface, or evaluate ~ 0.0)

# Due to the sqrt in the derivative functions, if the particle is not in the x-y space where the torus has z values, it will result in complex numbers
# Specificaly the term: math.sqrt(r**2 - (R - inv_root)**2), where inverse root = math.sqrt((A-x)**2 + (B-y)**2)
@njit
def reflect(particle_container, surface):
Copy link
Member

Choose a reason for hiding this comment

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

I think it is rare to have a reflecting torus boundary condition. But it would be cool to have it when one needs it!

@ilhamv
Copy link
Member

ilhamv commented Feb 4, 2026

I think the next step would be setting up the unit test. You can check this out as a reference.

To manually run the test, we do pytest <the file> on the directory the file is in.
To automatically run all the unit tests, we execute this script.

===

As you can see, now there are only tests for axis-aligned planes. Adding unit tests for the other surfaces for warming up would be great if you are interested! 😉

Changed the math in the evaluate function to allow the function to be called outside the torus x-y space without throwing an error.
@ilhamv
Copy link
Member

ilhamv commented Feb 4, 2026

I think the next step would be setting up the unit test. You can check this out as a reference.

Actually, there is another piece that you need to work on first before setting up the unit test. And that is the torus surface creation. Note that:

  • mcdc/object_/surface.py describes how we store the surface object data (what is still missing now for torus)
  • mcdc/transport/geometry/surface/*py are the functions that use the surface object data during the transport simulation (what you just added)

In mcdc/object_/surface.py, the torus surface would need to be added to the "Type-based creation methods" section and the __repr__ function method for nice-printing.

Note that in the creation method, we store the coefficients in A, B, ..., I, J. This means, you would need to re-letter the coefficients that you expect in your functions; for example, changing "r" and "R" into "I" and "J".

Changed the reflect and get_normal_component functions to use the expanded form of the torus equations. This allows these functions to be called outside the x-y area of the torus.
Imported the torus surface at the top of the file.
    • Import the torus constant
    • Add the torus function under the surface class
    • Add the torus to the decode type function
    • Add the radius constants to the super function and surface class
Add the torus surface constant
    • Import the torus surface
    • Import the torus constant
    • add functions (evaluate, normal comp, reflect, get distance)
Copy link
Member

@melekderman melekderman left a comment

Choose a reason for hiding this comment

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

  • Comments for PR #364

Where R is the radius of the shape as a whole, and r is the radius of the circle that is revolved to create the donut

Removing the square roots leaves you with the following equation:
f (x, y, z) = ( x^2 + y^2 + z^2 + R^2 - a^2 )^2 - 4R^2 * (x^2 + y^2)
Copy link
Member

Choose a reason for hiding this comment

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

f (x, y, z) = ( x^2 + y^2 + z^2 + R^2 - r^2 )^2 - 4R^2 * (x^2 + y^2)

Talen-Ayers and others added 7 commits February 20, 2026 13:41
Changing the torus surface name to accurately reflect its geometry from Torus-X to Torus-Z as the torus is radially symmetric around the z-axis.
Fixed typos and removed debug prints from code.
Changed torus name to Torus-z to accurately reflect its geometry.
Changed Torus-x to Torus-Z, see previous commits.
Added a coincident check to the get_distance_function, needs to be tested.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants