-
Notifications
You must be signed in to change notification settings - Fork 23
Implements MPCs #108
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
Draft
ralberd
wants to merge
8
commits into
sandialabs:main
Choose a base branch
from
SarveshJoshi33:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Implements MPCs #108
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c93434b
Changes in the MeshFixture, FunctionSpace, Objective. Array shape mis…
SarveshJoshi33 309f5ff
MPC based shear test running, need to perform some more testing to ve…
SarveshJoshi33 6254839
Updated the FunctionSpace script with the equinox module
SarveshJoshi33 0de8cb4
Changes regarding the import and the highlight comment for DofManagerMPC
SarveshJoshi33 b6c96d2
Normalized the test directory
SarveshJoshi33 92e09fd
Created the compression test with MPCs. Need to make some changes in …
SarveshJoshi33 918e85e
JAX Tracer Error present in non_homogenous_MPC.py, for creating funct…
SarveshJoshi33 c75566c
Implemented MPCs with Ryan, need to run test cases
SarveshJoshi33 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,21 +1,16 @@ | ||||||
| from jax.scipy.linalg import solve | ||||||
| from jaxtyping import Array, Float | ||||||
| from optimism import Interpolants | ||||||
| from optimism import Mesh | ||||||
| from optimism import QuadratureRule | ||||||
| from typing import Tuple | ||||||
| import equinox as eqx | ||||||
| import jax | ||||||
| import jax.numpy as np | ||||||
| from collections import namedtuple | ||||||
| import numpy as onp | ||||||
|
|
||||||
| import jax | ||||||
| import jax.numpy as np | ||||||
| from jax.scipy.linalg import solve | ||||||
|
|
||||||
| class EssentialBC(eqx.Module): | ||||||
| nodeSet: str | ||||||
| component: int | ||||||
| from optimism import Interpolants | ||||||
| from optimism import Mesh | ||||||
|
|
||||||
|
|
||||||
| class FunctionSpace(eqx.Module): | ||||||
| FunctionSpace = namedtuple('FunctionSpace', ['shapes', 'vols', 'shapeGrads', 'mesh', 'quadratureRule', 'isAxisymmetric']) | ||||||
| FunctionSpace.__doc__ = \ | ||||||
| """Data needed for calculus on functions in the discrete function space. | ||||||
|
|
||||||
| In describing the shape of the attributes, ``ne`` is the number of | ||||||
|
|
@@ -37,12 +32,8 @@ class FunctionSpace(eqx.Module): | |||||
| isAxisymmetric: boolean indicating if the function space data are | ||||||
| axisymmetric. | ||||||
| """ | ||||||
| shapes: Float[Array, "ne nqpe nn"] | ||||||
| vols: Float[Array, "ne nqpe"] | ||||||
| shapeGrads: Float[Array, "ne nqpe nn nd"] | ||||||
| mesh: Mesh.Mesh | ||||||
| quadratureRule: QuadratureRule.QuadratureRule | ||||||
| isAxisymmetric: bool | ||||||
|
|
||||||
| EssentialBC = namedtuple('EssentialBC', ['nodeSet', 'component']) | ||||||
|
|
||||||
|
|
||||||
| def construct_function_space(mesh, quadratureRule, mode2D='cartesian'): | ||||||
|
|
@@ -363,42 +354,49 @@ def integrate_function_on_edges(functionSpace, func, U, quadRule, edges): | |||||
| return np.sum(integrate_on_edges(functionSpace, func, U, quadRule, edges)) | ||||||
|
|
||||||
|
|
||||||
| class DofManager(eqx.Module): | ||||||
| # TODO get type hints below correct | ||||||
| # TODO this one could be moved to jax types if we move towards | ||||||
| # TODO jit safe preconditioners/solvers | ||||||
| fieldShape: Tuple[int, int] | ||||||
| isBc: any | ||||||
| isUnknown: any | ||||||
| ids: any | ||||||
| unknownIndices: any | ||||||
| bcIndices: any | ||||||
| dofToUnknown: any | ||||||
| HessRowCoords: any | ||||||
| HessColCoords: any | ||||||
| hessian_bc_mask: any | ||||||
| def create_nodeset_layers(mesh): | ||||||
| coords = mesh.coords | ||||||
| tol = 1e-8 | ||||||
| # Create unique layers of nodesets along the y-direction | ||||||
| unique_layers = sorted(onp.unique(coords[:,1])) | ||||||
| Ny = int(input("Enter the expected number of nodeset layers in y-direction: ")) | ||||||
| assert len(unique_layers) == Ny, f"ERROR - Expected {Ny} layers, but found {len(unique_layers)}." | ||||||
|
|
||||||
| layer_rows = [] | ||||||
|
|
||||||
| for i, y_val in enumerate(unique_layers): | ||||||
| nodes_in_layer = onp.flatnonzero(onp.abs(coords[:, 1] - y_val) < tol) | ||||||
| layer_rows.append(nodes_in_layer) | ||||||
|
|
||||||
| max_nodes_per_layer = max(len(row) for row in layer_rows) | ||||||
| y_layers = -1 * np.ones((len(layer_rows), max_nodes_per_layer), dtype=int) # Initialize with -1 | ||||||
|
|
||||||
| for i, row in enumerate(layer_rows): | ||||||
| y_layers = y_layers.at[i, :len(row)].set(row) # Fill each row with nodes from the layer | ||||||
|
|
||||||
| # # For debugging | ||||||
| # print("Layers in y-direction: ", y_layers) | ||||||
| return y_layers | ||||||
|
|
||||||
| class DofManager: | ||||||
| def __init__(self, functionSpace, dim, EssentialBCs): | ||||||
| self.fieldShape = Mesh.num_nodes(functionSpace.mesh), dim | ||||||
| isBc = onp.full(self.fieldShape, False, dtype=bool) | ||||||
| self.isBc = onp.full(self.fieldShape, False, dtype=bool) | ||||||
| for ebc in EssentialBCs: | ||||||
| isBc[functionSpace.mesh.nodeSets[ebc.nodeSet], ebc.component] = True | ||||||
| self.isBc = isBc | ||||||
| self.isBc[functionSpace.mesh.nodeSets[ebc.nodeSet], ebc.component] = True | ||||||
| self.isUnknown = ~self.isBc | ||||||
|
|
||||||
| self.ids = onp.arange(self.isBc.size).reshape(self.fieldShape) | ||||||
| self.ids = np.arange(self.isBc.size).reshape(self.fieldShape) | ||||||
|
|
||||||
| self.unknownIndices = self.ids[self.isUnknown] | ||||||
| self.bcIndices = self.ids[self.isBc] | ||||||
|
|
||||||
| ones = onp.ones(self.isBc.size, dtype=int) * -1 | ||||||
| dofToUnknown = ones | ||||||
| dofToUnknown[self.unknownIndices] = onp.arange(self.unknownIndices.size) | ||||||
| self.dofToUnknown = dofToUnknown | ||||||
| ones = np.ones(self.isBc.size, dtype=int) * -1 | ||||||
| self.dofToUnknown = ones.at[self.unknownIndices].set(np.arange(self.unknownIndices.size)) | ||||||
|
|
||||||
| self.HessRowCoords, self.HessColCoords = self._make_hessian_coordinates(functionSpace.mesh.conns) | ||||||
|
|
||||||
| self.hessian_bc_mask = self._make_hessian_bc_mask(onp.array(functionSpace.mesh.conns)) | ||||||
| self.hessian_bc_mask = self._make_hessian_bc_mask(functionSpace.mesh.conns) | ||||||
|
|
||||||
|
|
||||||
| def get_bc_size(self): | ||||||
|
|
@@ -450,7 +448,7 @@ def _make_hessian_coordinates(self, conns): | |||||
| rowCoords[rangeBegin:rangeEnd] = elHessCoords.ravel() | ||||||
| colCoords[rangeBegin:rangeEnd] = elHessCoords.T.ravel() | ||||||
|
|
||||||
| rangeBegin += onp.square(nElUnknowns[e]) | ||||||
| rangeBegin += np.square(nElUnknowns[e]) | ||||||
| return rowCoords, colCoords | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -466,3 +464,157 @@ def _make_hessian_bc_mask(self, conns): | |||||
| hessian_bc_mask[e,eFlag,:] = False | ||||||
| hessian_bc_mask[e,:,eFlag] = False | ||||||
| return hessian_bc_mask | ||||||
|
|
||||||
| # Different class for Multi-Point Constrained Problem | ||||||
|
||||||
| # Different class for Multi-Point Constrained Problem | |
| # DOF Manager for Multi-Point Constrained Problem |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you started making changes to
FunctionSpace.pybefore some newer changes were made to make it an Equinox module. Can you keep the Equinox module implementations ofFunctionSpaceandDofManagerclasses and implement yourDOFManagerMPCclass as a module? Let me know if you want my help.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Ryan,
I updated the DofManagerMPC class using the updated FunctionSpace with the equinox module and pushed it to the forked repository.