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

Moved omega from an attribute of the collision to the input of its callable #100

Merged
merged 1 commit into from
Jan 10, 2025

Conversation

hsalehipour
Copy link
Collaborator

Contributing Guidelines

Description

Moved omega from an attribute of the collision and stepper operation to an input of the methods. Viscosity (or omega) should not be an attribute of the collision or stepper object but rather an input to these methods. For example if you want to change viscosity during runtime after n iterations, you should not need to create another stepper object! Currently, we have all simulation setup configuration and parameters (BC, IC, geometry) seprated from the stepper except for Omega. This PR aims to fix that.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

How Has This Been Tested?

  • All pytest tests pass

Linting and Code Formatting

Make sure the code follows the project's linting and formatting standards. This project uses Ruff for linting.

To run Ruff, execute the following command from the root of the repository:

ruff check .
  • Ruff passes

@mehdiataei
Copy link
Contributor

Is there any practical purpose for this change or is cosmetic? Viscosity of a fluid cannot change physically in the basic Incompressible Navier Stokes flow. If there are any simulations that support this feature (e..g., viscosity change due to thermal, etc.) it should be another solver with its own stepper function that supports this.

It's best to avoid adding buffers to kernels due to performance when ideally and most likely constants, then the compiler can better optmize the kernel.

@hsalehipour
Copy link
Collaborator Author

Thanks for the feedback. No it is not just a cosmetic change. The main reason that prompted me to add this is for ML applications where for instance one wants to create various trajectories of the solution at different values of viscosity. We should not need to re-create the stepper functions for a new parameter. Again all other problem configurations are correctly set up to be independent from the stepper except for omega. Another example is for computational stability reasons: sometimes you mean gradually increase the Reynolds number to arrive at the actual Re.

@hsalehipour
Copy link
Collaborator Author

I agree with your comment about adding constant buffers to kernels. I have made omega static in both warp and jax which should address your comment. But even without making them static, MLUPS numbers were the same as before.

@hsalehipour
Copy link
Collaborator Author

@mehdiataei I am really not sure if the use of wp.static in my latest forced commit does what I intended. Anyway please take a look. Regardless I am fine removing wp.static because MLUPs are not affected.


# Ensure warp type is set correctly for Omega
if self.backend == ComputeBackend.WARP:
self.omega = wp.static(self.precision_policy.compute_precision.wp_dtype(self.omega))
Copy link
Contributor

Choose a reason for hiding this comment

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

The casting should be done in the kernels not here.

Copy link
Contributor

Choose a reason for hiding this comment

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

See other primitive values casting in the kernels

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agreed. fixed it.

@@ -29,6 +29,10 @@ def __init__(self, omega, prescribed_vel, grid_shape, velocity_set, backend, pre
self.boundary_conditions = []
self.prescribed_vel = prescribed_vel

# Ensure warp type is set correctly for Omega
if self.backend == ComputeBackend.WARP:
Copy link
Contributor

Choose a reason for hiding this comment

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

same

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed it.

xlb/default_config.py Show resolved Hide resolved
xlb/operator/collision/bgk.py Show resolved Hide resolved
xlb/operator/collision/bgk.py Show resolved Hide resolved
"""

def __init__(
self,
omega: float,
velocity_set: VelocitySet = None,
precision_policy=None,
compute_backend=None,
):
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the extra line after ):

velocity_set=velocity_set,
precision_policy=precision_policy,
compute_backend=compute_backend,
)

@Operator.register_backend(ComputeBackend.JAX)
@partial(jit, static_argnums=(0,), donate_argnums=(1, 2, 3))
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@hsalehipour hsalehipour force-pushed the omega_stepper_input branch 3 times, most recently from 883a199 to 08e08d1 Compare January 10, 2025 16:34
@hsalehipour
Copy link
Collaborator Author

Pleaes check again. I addressed all comments.

@@ -74,13 +71,17 @@ def jax_implementation(
else:
raise NotImplementedError("Velocity set not supported: {}".format(type(self.velocity_set)))

# Compute required constants based on the input omega (omega is the inverse relaxation time)
beta = self.compute_dtype(0.5 * omega)
Copy link
Contributor

Choose a reason for hiding this comment

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

You must apply compute dtype to both numbers separately before multipication

@@ -278,6 +278,10 @@ def functional(
shear = decompose_shear_d2q9(fneq)
delta_s = shear * rho / self.compute_dtype(4.0)

# Compute required constants based on the input omega (omega is the inverse relaxation time)
_beta = self.compute_dtype(0.5 * omega)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

DONE

@mehdiataei
Copy link
Contributor

Thanks looks great

@hsalehipour hsalehipour merged commit d4a92bc into Autodesk:main Jan 10, 2025
10 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 10, 2025
@hsalehipour hsalehipour deleted the omega_stepper_input branch January 10, 2025 20:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants