-
Notifications
You must be signed in to change notification settings - Fork 644
Sym upstream pr1 #1567
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
Open
ktangsali
wants to merge
10
commits into
NVIDIA:main
Choose a base branch
from
ktangsali:sym-upstream-pr1
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.
Open
Sym upstream pr1 #1567
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
af00635
PhysicsInformer: symbolic PDE residual computation using physicsnemo …
ktangsali a9ad394
address oliver's comments
ktangsali ecea106
in anticipation of corey's comment
ktangsali 3d7d13c
Merge branch 'main' into sym-upstream-pr1
ktangsali ff0af94
update lock file and address a few feedback comments
ktangsali 4de47c3
Merge remote-tracking branch 'ktangsali/sym-upstream-pr1' into sym-up…
ktangsali 2620856
update importlinter contract for sym
ktangsali 75538ab
compute connectivity via mesh
ktangsali 3a74914
update lock file
ktangsali 6aaa979
Merge branch 'main' into sym-upstream-pr1
ktangsali 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| PhysicsNeMo Sym | ||
| =============== | ||
|
|
||
| Symbolic PDE residual computation for physics-informed training. | ||
|
|
||
| .. autoclass:: physicsnemo.sym.eq.pde.PDE | ||
| :members: | ||
| :show-inheritance: | ||
|
|
||
| .. autoclass:: physicsnemo.sym.eq.phy_informer.PhysicsInformer | ||
| :members: | ||
| :show-inheritance: | ||
|
|
||
| .. autoclass:: physicsnemo.sym.eq.gradients.GradientCalculator | ||
| :members: | ||
|
|
||
| .. autofunction:: physicsnemo.sym.eq.gradients.compute_connectivity_tensor | ||
|
|
||
| .. autofunction:: physicsnemo.sym.eq.gradients.compute_stencil3d |
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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2023 - 2026 NVIDIA CORPORATION & AFFILIATES. | ||
| # SPDX-FileCopyrightText: All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """PhysicsNeMo Sym: symbolic PDE residual computation. | ||
|
|
||
| Example | ||
| ------- | ||
| >>> from sympy import Symbol, Function | ||
| >>> from physicsnemo.sym.eq.pde import PDE | ||
| >>> from physicsnemo.sym.eq.phy_informer import PhysicsInformer | ||
| >>> | ||
| >>> class Poisson(PDE): | ||
| ... def __init__(self): | ||
| ... self.dim = 2 | ||
| ... x, y = Symbol("x"), Symbol("y") | ||
| ... u = Function("u")(x, y) | ||
| ... self.equations = {"poisson": u.diff(x, 2) + u.diff(y, 2)} | ||
| ... | ||
| >>> pde = Poisson() | ||
| >>> pi = PhysicsInformer(["poisson"], pde, grad_method="autodiff") | ||
| >>> sorted(pi.required_inputs) | ||
| ['coordinates', 'u'] | ||
| """ | ||
|
|
||
| from physicsnemo.sym.eq.gradients import ( | ||
| GradientCalculator, | ||
| compute_connectivity_tensor, | ||
| compute_stencil3d, | ||
| ) | ||
| from physicsnemo.sym.eq.pde import PDE | ||
| from physicsnemo.sym.eq.phy_informer import PhysicsInformer | ||
|
|
||
| __all__ = [ | ||
| "GradientCalculator", | ||
| "PDE", | ||
| "PhysicsInformer", | ||
| "compute_connectivity_tensor", | ||
| "compute_stencil3d", | ||
| ] | ||
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 |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2023 - 2026 NVIDIA CORPORATION & AFFILIATES. | ||
| # SPDX-FileCopyrightText: All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Lightweight replacement for Sym's Node — wraps a callable with string-based I/O metadata.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from physicsnemo.sym.constants import diff_str | ||
|
|
||
|
|
||
| class Computation: | ||
|
ktangsali marked this conversation as resolved.
|
||
| """A named unit in the computational graph. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| inputs : list[str] | ||
| Non-derivative input names (e.g. ``["u", "v", "p"]``). | ||
| outputs : list[str] | ||
| Output names produced by this computation. | ||
| evaluate : callable | ||
| A callable (typically ``torch.nn.Module``) mapping | ||
| ``Dict[str, Tensor] → Dict[str, Tensor]``. | ||
| name : str | ||
| Human-readable label for debugging. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| inputs: list[str], | ||
| outputs: list[str], | ||
| evaluate, | ||
| name: str = "Computation", | ||
| ): | ||
| if isinstance(inputs, str): | ||
| inputs = [inputs] | ||
| all_inputs = [str(x) for x in inputs] | ||
| self._inputs = [x for x in all_inputs if diff_str not in x] | ||
| self._derivatives = [x for x in all_inputs if diff_str in x] | ||
| self._outputs = [str(x) for x in outputs] | ||
| self.evaluate = evaluate | ||
| self._name = name | ||
|
|
||
| @classmethod | ||
| def from_sympy(cls, eq, out_name, freeze_terms=None, detach_names=None): | ||
| """Build a Computation from a SymPy expression.""" | ||
| from physicsnemo.sym.utils.sympy.torch_printer import ( | ||
| SympyToTorch, | ||
| _subs_derivatives, | ||
| ) | ||
|
|
||
| if freeze_terms is None: | ||
| freeze_terms = [] | ||
| if detach_names is None: | ||
| detach_names = [] | ||
|
|
||
| sub_eq = _subs_derivatives(eq) | ||
| evaluate = SympyToTorch(sub_eq, out_name, freeze_terms, detach_names) | ||
| inputs = list(evaluate.keys) | ||
| outputs = [out_name] | ||
| return cls(inputs, outputs, evaluate, name="Sympy Computation: " + out_name) | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| """Human-readable label for this computation.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def outputs(self) -> list[str]: | ||
| """Output names produced by this computation.""" | ||
| return self._outputs | ||
|
|
||
| @property | ||
| def inputs(self) -> list[str]: | ||
| """Non-derivative input names.""" | ||
| return self._inputs | ||
|
|
||
| @property | ||
| def derivatives(self) -> list[str]: | ||
| """Derivative input names (contain ``__``).""" | ||
| return self._derivatives | ||
|
|
||
| def __str__(self) -> str: | ||
| return ( | ||
| f"computation: {self.name}\n" | ||
| f" inputs: {self.inputs}\n" | ||
| f" derivatives: {self.derivatives}\n" | ||
| f" outputs: {self.outputs}" | ||
| ) | ||
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 |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2023 - 2026 NVIDIA CORPORATION & AFFILIATES. | ||
| # SPDX-FileCopyrightText: All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import numpy as np | ||
| import torch | ||
|
|
||
| diff_str: str = "__" | ||
|
ktangsali marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def diff(y: str, x: str, degree: int = 1) -> str: | ||
| """Build a derivative name string: ``diff('u', 'x')`` → ``'u__x'``.""" | ||
| return diff_str.join([y] + degree * [x]) | ||
|
|
||
|
|
||
| tf_dt = torch.float32 | ||
| np_dt = np.float32 | ||
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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2023 - 2026 NVIDIA CORPORATION & AFFILIATES. | ||
| # SPDX-FileCopyrightText: All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from physicsnemo.sym.eq.gradients import ( | ||
| GradientCalculator, | ||
| compute_connectivity_tensor, | ||
| compute_stencil3d, | ||
| ) | ||
| from physicsnemo.sym.eq.pde import PDE | ||
| from physicsnemo.sym.eq.phy_informer import PhysicsInformer | ||
|
|
||
| __all__ = [ | ||
| "GradientCalculator", | ||
| "PDE", | ||
| "PhysicsInformer", | ||
| "compute_connectivity_tensor", | ||
| "compute_stencil3d", | ||
| ] |
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.
Uh oh!
There was an error while loading. Please reload this page.