Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
s9latimm committed Oct 15, 2024
1 parent 0417da5 commit 79c4dfb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 33 deletions.
48 changes: 34 additions & 14 deletions src/base/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,51 @@ class Real:
def __init__(self, value: float) -> None:
self.__value: int = int((value + self.DELTA) / self.EPS)

def __eq__(self, other: Real) -> bool:
return self.__value == other.__value
def __eq__(self, other: Real | float) -> bool:
# pylint: disable=protected-access
return self.__value == Real(float(other)).__value

def __gt__(self, other: Real) -> bool:
return self.__value > other.__value
def __gt__(self, other: Real | float) -> bool:
# pylint: disable=protected-access
return self.__value > Real(float(other)).__value

def __ge__(self, other: Real) -> bool:
return self.__value >= other.__value
def __ge__(self, other: Real | float) -> bool:
# pylint: disable=protected-access
return self.__value >= Real(float(other)).__value

def __lt__(self, other: Real) -> bool:
return self.__value <= other.__value
def __lt__(self, other: Real | float) -> bool:
# pylint: disable=protected-access
return self.__value <= Real(float(other)).__value

def __le__(self, other: Real) -> bool:
return self.__value <= other.__value
def __le__(self, other: Real | float) -> bool:
# pylint: disable=protected-access
return self.__value <= Real(float(other)).__value

def __add__(self, other: Real) -> Real:
def __add__(self, other: Real | float) -> Real:
return Real(float(self) + float(other))

def __sub__(self, other: Real) -> Real:
def __radd__(self, other) -> Real:
return self.__add__(other)

def __sub__(self, other: Real | float) -> Real:
return Real(float(self) - float(other))

def __mul__(self, other: Real) -> Real:
def __rsub__(self, other) -> Real:
return Real(float(other) - float(self))

def __mul__(self, other: Real | float) -> Real:
return Real(float(self) * float(other))

def __float__(self):
def __rmul__(self, other: Real | float) -> Real:
return self.__mul__(other)

def __truediv__(self, other: Real | float) -> Real:
return Real(float(self) / float(other))

def __rtruediv__(self, other: Real | float) -> Real:
return Real(float(other) / float(self))

def __float__(self) -> float:
return self.__value * self.EPS

def __hash__(self) -> int:
Expand Down
16 changes: 10 additions & 6 deletions src/base/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import torch
from torch import nn

from src.base.mesh import Mesh


def nabla(f: torch.Tensor, x: torch.Tensor) -> torch.Tensor:
return torch.autograd.grad(f, x, grad_outputs=torch.ones_like(f), create_graph=True, retain_graph=True)[0]
Expand All @@ -16,13 +18,15 @@ def laplace(f: torch.Tensor, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tenso
return f_x, f_xx


class SequentialModel:
T = tp.TypeVar('T')


class SequentialModel(tp.Generic[T]):

def __init__(self, layers: tp.Sequence[int], device: str) -> None:
super().__init__()

if device == 'cpu':
assert torch.cpu.is_available()
assert torch.cpu.is_available()
if device == 'cuda':
assert torch.cuda.is_available()

Expand Down Expand Up @@ -50,7 +54,7 @@ def __init__(self, layers: tp.Sequence[int], device: str) -> None:

@staticmethod
def _detach(*tensor: torch.Tensor) -> tuple[list[list[float]], ...]:
return tuple([i.detach().cpu().tolist() for i in tensor])
return tuple(i.detach().cpu().tolist() for i in tensor)

def __str__(self) -> str:
return str(self._model)
Expand All @@ -60,11 +64,11 @@ def history(self) -> list[tuple[tp.Any, ...]]:
return self._losses

@abstractmethod
def train(self, callback: tp.Any) -> None:
def train(self, callback: tp.Callable[[tp.Any], None]) -> None:
...

@abstractmethod
def predict(self, sample: tp.Any) -> tuple[tp.Any, ...]:
def predict(self, mesh: Mesh) -> Mesh[T]:
...

def eval(self) -> None:
Expand Down
5 changes: 2 additions & 3 deletions src/base/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ def __init__(self, a: tuple[float, float] | Coordinate, b: tuple[float, float] |

def __contains__(self, coordinate: tuple | Coordinate) -> bool:
c = Coordinate(*coordinate)
return Real(self.__a.x) <= Real(c.x) <= Real(self.__b.x) \
and Real(self.__a.y) <= Real(c.y) <= Real(self.__b.y)
return Real(self.__a.x) <= c.x <= Real(self.__b.x) and Real(self.__a.y) <= c.y <= Real(self.__b.y)

def __add__(self, summand: float) -> Rectangle:
return Rectangle((self.__a.x - summand, self.__a.y - summand), (self.__b.x + summand, self.__b.y + summand))
Expand Down Expand Up @@ -227,7 +226,7 @@ def __f(self, x) -> tuple[Coordinate, Coordinate]:

def __contains__(self, coordinate: tuple[float, float] | Coordinate) -> bool:
c = (Coordinate(*coordinate) - self.__a) / self.__length
if Real(0) <= Real(c.x) <= Real(1):
if 0 <= Real(c.x) <= 1:
upper, lower = self.__f(c.x)
return Real(lower.y) <= Real(c.y + 5e-3) and Real(c.y - 5e-3) <= Real(upper.y)
return False
Expand Down
7 changes: 4 additions & 3 deletions src/nse/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from src.nse import DEFAULT_NU, DEFAULT_STEPS, DEFAULT_RHO, DEFAULT_INTAKE
from src.nse.experiments import EXPERIMENTS
from src.nse.experiments.experiment import Experiment
from src.nse.paper import plot_experiment
from src.nse.simulation import Simulation
from src.nse.visualize import plot_prediction, plot_losses
from src.utils.timer import Stopwatch
Expand Down Expand Up @@ -102,9 +103,9 @@ def callback(history):
logging.info(f'NU: {model.nu:.16f}')
logging.info(f'RHO: {model.rho:.16f}')

# if foam:
# logging.info('PLOT: OPENFOAM')
# plot_foam(experiment, identifier)
if foam:
logging.info('PLOT: OPENFOAM')
plot_experiment(experiment)

# if foam:
# logging.info('PLOT: DIFFERENCE')
Expand Down
5 changes: 1 addition & 4 deletions src/nse/paper.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ def plot_experiment(experiment: Experiment):
for k, v in experiment.inlet:
mesh.insert(k, v)

for k, v in experiment.outlet:
mesh.insert(k, v)

for k, v in experiment.knowledge:
mesh.insert(k, v)

Expand All @@ -71,7 +68,7 @@ def plot_experiment(experiment: Experiment):
mesh,
['u', 'v', 'p'],
marker=experiment.learning.keys(),
path=OUTPUT_DIR / 'paper' / 'training.pdf',
path=OUTPUT_DIR / 'paper' / f'{experiment.name.lower()}.pdf',
boundary=experiment.boundary,
figure=experiment.obstruction,
)
Expand Down
6 changes: 3 additions & 3 deletions src/nse/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from src.nse.record import Record


class Simulation(SequentialModel):
class Simulation(SequentialModel[Record]):

def __init__(self, experiment: Experiment, device: str, steps: int, layers: list[int]) -> None:

Expand Down Expand Up @@ -77,7 +77,7 @@ def nu(self) -> float:
def rho(self) -> float:
return self.__rho.detach().cpu()

def train(self, callback: tp.Any) -> None:
def train(self, callback: tp.Callable[[tp.Any], None]) -> None:

def closure():
callback(self.history)
Expand Down Expand Up @@ -144,7 +144,7 @@ def __forward(
def predict(self, mesh: Mesh) -> Mesh[Record]:
coordinates = [k for k, _ in mesh.detach()]

u, v, p, psi = self._detach(*self.__forward((
u, v, p, _ = self._detach(*self.__forward((
torch.tensor([[i.x] for i in coordinates], dtype=torch.float64, requires_grad=True, device=self._device),
torch.tensor([[i.y] for i in coordinates], dtype=torch.float64, requires_grad=True, device=self._device),
)))
Expand Down

0 comments on commit 79c4dfb

Please sign in to comment.