Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ SPDX-License-Identifier: Apache-2.0
<table>
<tr>
<td>
<a href="https://github.com/mathLab/PINA/raw/master/readme/pina_logo.png">
<img src="https://github.com/mathLab/PINA/raw/master/readme/pina_logo.png"
<a href="readme/pina_logo.png">
<img src="readme/pina_logo.png"
alt="PINA logo"
style="width: 220px; aspect-ratio: 1 / 1; object-fit: contain;">
</a>
Expand Down
Binary file modified docs/source/index_files/PINA_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion pina/equation/equation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Module for the Equation."""

import inspect

from .equation_interface import EquationInterface


Expand Down Expand Up @@ -49,6 +48,10 @@ def residual(self, input_, output_, params_=None):
:raises RuntimeError: If the underlying equation signature length is not
2 (direct problem) or 3 (inverse problem).
"""
# Move the equation to the input_ device
self.to(input_.device)

# Call the underlying equation based on its signature length
if self.__len_sig == 2:
return self.__equation(input_, output_)
if self.__len_sig == 3:
Expand Down
12 changes: 6 additions & 6 deletions pina/equation/equation_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,19 @@ def equation(input_, output_):
)

# Ensure consistency of c length
if len(self.c) != (len(input_lbl) - 1) and len(self.c) > 1:
if self.c.shape[-1] != len(input_lbl) - 1 and self.c.shape[-1] > 1:
raise ValueError(
"If 'c' is passed as a list, its length must be equal to "
"the number of spatial dimensions."
)

# Repeat c to ensure consistent shape for advection
self.c = self.c.repeat(output_.shape[0], 1)
if self.c.shape[1] != (len(input_lbl) - 1):
self.c = self.c.repeat(1, len(input_lbl) - 1)
c = self.c.repeat(output_.shape[0], 1)
if c.shape[1] != (len(input_lbl) - 1):
c = c.repeat(1, len(input_lbl) - 1)

# Add a dimension to c for the following operations
self.c = self.c.unsqueeze(-1)
c = c.unsqueeze(-1)

# Compute the time derivative and the spatial gradient
time_der = grad(output_, input_, components=None, d="t")
Expand All @@ -262,7 +262,7 @@ def equation(input_, output_):
tmp = tmp.transpose(-1, -2)

# Compute advection term
adv = (tmp * self.c).sum(dim=tmp.tensor.ndim - 2)
adv = (tmp * c).sum(dim=tmp.tensor.ndim - 2)

return time_der + adv

Expand Down
31 changes: 31 additions & 0 deletions pina/equation/equation_interface.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Module for the Equation Interface."""

from abc import ABCMeta, abstractmethod
import torch


class EquationInterface(metaclass=ABCMeta):
Expand Down Expand Up @@ -33,3 +34,33 @@ def residual(self, input_, output_, params_):
:return: The computed residual of the equation.
:rtype: LabelTensor
"""

def to(self, device):
"""
Move all tensor attributes to the specified device.

:param torch.device device: The target device to move the tensors to.
:return: The instance moved to the specified device.
:rtype: EquationInterface
"""
# Iterate over all attributes of the Equation
for key, val in self.__dict__.items():

# Move tensors in dictionaries to the specified device
if isinstance(val, dict):
self.__dict__[key] = {
k: v.to(device) if torch.is_tensor(v) else v
for k, v in val.items()
}

# Move tensors in lists to the specified device
elif isinstance(val, list):
self.__dict__[key] = [
v.to(device) if torch.is_tensor(v) else v for v in val
]

# Move tensor attributes to the specified device
elif torch.is_tensor(val):
self.__dict__[key] = val.to(device)

return self
5 changes: 5 additions & 0 deletions pina/equation/system_equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,18 @@ def residual(self, input_, output_, params_=None):
:return: The aggregated residuals of the system of equations.
:rtype: LabelTensor
"""
# Move the equation to the input_ device
self.to(input_.device)

# Compute the residual for each equation
residual = torch.hstack(
[
equation.residual(input_, output_, params_)
for equation in self.equations
]
)

# Skip reduction if not specified
if self.reduction is None:
return residual

Expand Down
5 changes: 2 additions & 3 deletions pina/problem/zoo/helmholtz.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ def __init__(self, alpha=3.0):
:type alpha: float | int
"""
super().__init__()

self.alpha = alpha
check_consistency(alpha, (int, float))
self.alpha = alpha

def forcing_term(self, input_):
def forcing_term(input_):
"""
Implementation of the forcing term.
"""
Expand Down
4 changes: 1 addition & 3 deletions pina/solver/physics_informed_solver/pinn_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ def setup(self, stage):
"""
# Override the compilation, compiling only for torch < 2.8, see
# related issue at https://github.com/mathLab/PINA/issues/621
if torch.__version__ < "2.8":
self.trainer.compile = True
else:
if torch.__version__ >= "2.8":
self.trainer.compile = False
warnings.warn(
"Compilation is disabled for torch >= 2.8. "
Expand Down
6 changes: 1 addition & 5 deletions pina/solver/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,7 @@ def setup(self, stage):
:return: The result of the parent class ``setup`` method.
:rtype: Any
"""
if stage == "fit" and self.trainer.compile:
self._setup_compile()
if stage == "test" and (
self.trainer.compile and not self._is_compiled()
):
if self.trainer.compile and not self._is_compiled():
self._setup_compile()
return super().setup(stage)

Expand Down
41 changes: 38 additions & 3 deletions pina/trainer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
"""Module for the Trainer."""

import sys
import warnings
import torch
import lightning
from .utils import check_consistency
from .utils import check_consistency, custom_warning_format
from .data import PinaDataModule
from .solver import SolverInterface, PINNInterface

# set the warning for compile options
warnings.formatwarning = custom_warning_format
warnings.filterwarnings("always", category=UserWarning)


class Trainer(lightning.pytorch.Trainer):
"""
Expand Down Expand Up @@ -49,7 +54,8 @@ def __init__(
:param float val_size: The percentage of elements to include in the
validation dataset. Default is ``0.0``.
:param bool compile: If ``True``, the model is compiled before training.
Default is ``False``. For Windows users, it is always disabled.
Default is ``False``. For Windows users, it is always disabled. Not
supported for python version greater or equal than 3.14.
:param bool repeat: Whether to repeat the dataset data in each
condition during training. For further details, see the
:class:`~pina.data.data_module.PinaDataModule` class. Default is
Expand Down Expand Up @@ -104,8 +110,17 @@ def __init__(
super().__init__(**kwargs)

# checking compilation and automatic batching
if compile is None or sys.platform == "win32":
# compilation disabled for Windows and for Python 3.14+
if (
compile is None
or sys.platform == "win32"
or sys.version_info >= (3, 14)
):
compile = False
warnings.warn(
"Compilation is disabled for Python 3.14+ and for Windows.",
UserWarning,
)

repeat = repeat if repeat is not None else False

Expand Down Expand Up @@ -325,3 +340,23 @@ def _check_consistency_and_set_defaults(
if batch_size is not None:
check_consistency(batch_size, int)
return pin_memory, num_workers, shuffle, batch_size

@property
def compile(self):
"""
Whether compilation is required or not.

:return: ``True`` if compilation is required, ``False`` otherwise.
:rtype: bool
"""
return self._compile

@compile.setter
def compile(self, value):
"""
Setting the value of compile.

:param bool value: Whether compilation is required or not.
"""
check_consistency(value, bool)
self._compile = value
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pina-mathlab"
version = "0.2.4"
version = "0.2.5"
description = "Physic Informed Neural networks for Advance modeling."
readme = "README.md"
authors = [
Expand Down
Binary file modified readme/pina_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/test_equation/test_equation_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_advection_equation(c):

# Should fail if c is a list and its length != spatial dimension
with pytest.raises(ValueError):
Advection([1, 2, 3])
equation = Advection([1, 2, 3])
residual = equation.residual(pts, u)


Expand Down
Binary file modified tutorials/static/pina_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.