Skip to content

Commit

Permalink
Update eval
Browse files Browse the repository at this point in the history
  • Loading branch information
s9latimm committed Oct 18, 2024
1 parent d5147b5 commit 7f64393
Show file tree
Hide file tree
Showing 20 changed files with 453 additions and 256 deletions.
46 changes: 36 additions & 10 deletions .eval.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
python -m src.nse -e step --id "eval_nn/02" -L 20:20:20:20 -i 1 -n .01 -d cuda -pFG -N 10000
python -m src.nse -e step --id "eval_nn/04" -L 40:40:40:40 -i 1 -n .01 -d cuda -pFG -N 10000
python -m src.nse -e step --id "eval_nn/06" -L 60:60:60:60 -i 1 -n .01 -d cuda -pFG -N 10000
python -m src.nse -e step --id "eval_nn/08" -L 80:80:80:80 -i 1 -n .01 -d cuda -pFG -N 10000
python -m src.nse -e step --id "eval_nn/10" -L 100:100:100:100 -i 1 -n .01 -d cuda -pFG -N 10000
python -m src.nse -e step --id "eval_nn/12" -L 120:120:120:120 -i 1 -n .01 -d cuda -pFG -N 10000
python -m src.nse -e step --id "eval_nn/14" -L 140:140:140:140 -i 1 -n .01 -d cuda -pFG -N 10000
python -m src.nse -e step --id "eval_nn/16" -L 160:160:160:160 -i 1 -n .01 -d cuda -pFG -N 10000
python -m src.nse -e step --id "eval_nn/18" -L 180:180:180:180 -i 1 -n .01 -d cuda -pFG -N 10000
python -m src.nse -e step --id "eval_nn/20" -L 200:200:200:200 -i 1 -n .01 -d cuda -pFG -N 10000
python -m src.nse -e step --id "eval/025/1" -i 1 -n .01 -d cuda -pFG -N 20000 -L 25:25
python -m src.nse -e step --id "eval/025/2" -i 1 -n .01 -d cuda -pFG -N 20000 -L 25:25:25
python -m src.nse -e step --id "eval/025/3" -i 1 -n .01 -d cuda -pFG -N 20000 -L 25:25:25:25
python -m src.nse -e step --id "eval/025/4" -i 1 -n .01 -d cuda -pFG -N 20000 -L 25:25:25:25:25
python -m src.nse -e step --id "eval/025/5" -i 1 -n .01 -d cuda -pFG -N 20000 -L 25:25:25:25:25:25
python -m src.nse -e step --id "eval/025/6" -i 1 -n .01 -d cuda -pFG -N 20000 -L 25:25:25:25:25:25:25
python -m src.nse -e step --id "eval/025/7" -i 1 -n .01 -d cuda -pFG -N 20000 -L 25:25:25:25:25:25:25:25

python -m src.nse -e step --id "eval/050/1" -i 1 -n .01 -d cuda -pFG -N 20000 -L 50:50
python -m src.nse -e step --id "eval/050/2" -i 1 -n .01 -d cuda -pFG -N 20000 -L 50:50:50
python -m src.nse -e step --id "eval/050/3" -i 1 -n .01 -d cuda -pFG -N 20000 -L 50:50:50:50
python -m src.nse -e step --id "eval/050/4" -i 1 -n .01 -d cuda -pFG -N 20000 -L 50:50:50:50:50

python -m src.nse -e step --id "eval/075/1" -i 1 -n .01 -d cuda -pFG -N 20000 -L 75:75
python -m src.nse -e step --id "eval/075/2" -i 1 -n .01 -d cuda -pFG -N 20000 -L 75:75:75
python -m src.nse -e step --id "eval/075/3" -i 1 -n .01 -d cuda -pFG -N 20000 -L 75:75:75:75

python -m src.nse -e step --id "eval/100/1" -i 1 -n .01 -d cuda -pFG -N 20000 -L 100:100
python -m src.nse -e step --id "eval/100/2" -i 1 -n .01 -d cuda -pFG -N 20000 -L 100:100:100
python -m src.nse -e step --id "eval/100/3" -i 1 -n .01 -d cuda -pFG -N 20000 -L 100:100:100:100

python -m src.nse -e step --id "eval/125/1" -i 1 -n .01 -d cuda -pFG -N 20000 -L 125:125
python -m src.nse -e step --id "eval/125/2" -i 1 -n .01 -d cuda -pFG -N 20000 -L 125:125:125
python -m src.nse -e step --id "eval/125/3" -i 1 -n .01 -d cuda -pFG -N 20000 -L 125:125:125:125

python -m src.nse -e step --id "eval/150/1" -i 1 -n .01 -d cuda -pFG -N 20000 -L 150:150
python -m src.nse -e step --id "eval/150/2" -i 1 -n .01 -d cuda -pFG -N 20000 -L 150:150:150
python -m src.nse -e step --id "eval/150/3" -i 1 -n .01 -d cuda -pFG -N 20000 -L 150:150:150:150

# python -m src.nse -e step --id "eval/175/1" -i 1 -n .01 -d cuda -pFG -N 20000 -L 175:175
# python -m src.nse -e step --id "eval/175/2" -i 1 -n .01 -d cuda -pFG -N 20000 -L 175:175:175
# python -m src.nse -e step --id "eval/175/3" -i 1 -n .01 -d cuda -pFG -N 20000 -L 175:175:175:175
#
# python -m src.nse -e step --id "eval/200/1" -i 1 -n .01 -d cuda -pFG -N 20000 -L 200:200
# python -m src.nse -e step --id "eval/200/2" -i 1 -n .01 -d cuda -pFG -N 20000 -L 200:200:200
# python -m src.nse -e step --id "eval/200/3" -i 1 -n .01 -d cuda -pFG -N 20000 -L 200:200:200:200
2 changes: 1 addition & 1 deletion evaluation.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pinNSE:

| experiment | inlet | nu | FOAM | pinNSE 3x100 | pinNSE 4x80 |
|:-----------|-------|-----|:--------------------:|:-----------------------:|-------------|
| empty | 1 | .01 | convergence (21 min) | convergence (6 s, += 3) | |
| step | 1 | .01 | convergence (21 min) | convergence (6 s, += 3) | |
| | 4 | .04 | | | |
| | | | | | |
| | | | | | |
Expand Down
2 changes: 1 addition & 1 deletion resources
Submodule resources updated 110 files
3 changes: 0 additions & 3 deletions src/base/controller/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,3 @@ def save(self, path: Path):
for i, loss in enumerate(self._losses):
s = ",".join(f'{j:.16f}' for j in loss)
f.write(f'{i:d},{s}\n')

def load(self, path: Path) -> None:
self._model.load_state_dict(torch.load(path))
75 changes: 46 additions & 29 deletions src/base/model/algebra.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
from __future__ import annotations

import logging
from pathlib import Path


class RealNumber:
class Real:
EPS: float = 1e-3
DELTA: float = 1e-5

def __init__(self, value: float) -> None:
def __init__(self, value: float = 0.) -> None:
if value < 0:
self.__value: int = int((value - self.DELTA) / self.EPS)
self.__value = int((value - self.DELTA) / self.EPS)
else:
self.__value: int = int((value + self.DELTA) / self.EPS)
self.__value = int((value + self.DELTA) / self.EPS)

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

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

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

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

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

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

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

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

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

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

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

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

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

def __float__(self) -> float:
return self.__value * self.EPS
Expand All @@ -75,13 +76,29 @@ def save(self, path: Path) -> None:
with path.open("w", encoding="utf-8") as f:
f.write(f'{str(self)}\n')

@staticmethod
def load(path: Path) -> Real:
if path.exists():
value = path.read_text(encoding='utf-8').strip()
return Real(float(value))
else:
logging.error(f'{path} does not exist')


class Integer:

def __init__(self, value: int) -> None:
def __init__(self, value: int = 0) -> None:
self.__value: int = value

def save(self, path: Path) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", encoding="utf-8") as f:
f.write(f'{self.__value:d}\n')

@staticmethod
def load(path: Path) -> Integer:
if path.exists():
value = path.read_text(encoding='utf-8').strip()
return Integer(int(value))
else:
logging.error(f'{path} does not exist')
25 changes: 19 additions & 6 deletions src/base/model/function.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from abc import abstractmethod

import numpy as np


class Function:

Expand Down Expand Up @@ -33,18 +35,29 @@ def __init__(self) -> None:
super().__init__(0)


class Sinus(Function):

def __init__(self, start: float, end: float, scale: float = 1.) -> None:
self.__scale = scale
self.__a = 20 / (end - start) / np.pi

def __call__(self, x: float) -> float:
return self.__scale * np.sin(self.__a * x)

def __str__(self) -> str:
return f'{self.__scale:.1f}*sin({self.__a:.1f}x)'


class Parabola(Function):

def __init__(self, start: float, end: float, scale: float = 1.) -> None:
self.__scale = scale
self.__width = end - start
mid = end - self.__width / 2.

self.__b = 2. / self.__width
self.__a = mid * self.__b
self.__a = 2. / self.__width
self.__b = (end - self.__width / 2.) * self.__a

def __call__(self, x: float) -> float:
return self.__scale * self.__width * (1. - (self.__a - self.__b * x)**2)
return self.__scale * self.__width * (1. - (self.__b - self.__a * x)**2)

def __str__(self) -> str:
return f'{self.__scale:.0f}*{self.__width:.0f}*(1-({self.__a:.0f}-{self.__b:.0f}x)^2)'
return f'{self.__scale:.1f}*{self.__width:.1f}*(1-({self.__b:.1f}-{self.__a:.1f}x)^2)'
22 changes: 12 additions & 10 deletions src/base/model/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import numpy as np

from src.base.model.algebra import RealNumber
from src.base.model.algebra import Real


def merge(*lists: tp.Sequence[tp.Any]) -> list[tp.Any]:
Expand All @@ -19,7 +19,7 @@ def merge(*lists: tp.Sequence[tp.Any]) -> list[tp.Any]:


def arrange(start: float, stop: float, step: float, center=False) -> list[float]:
start, stop, step = RealNumber(start), RealNumber(stop), RealNumber(step)
start, stop, step = Real(start), Real(stop), Real(step)

if center:
start = start + step / 2
Expand All @@ -45,9 +45,9 @@ def arrange(start: float, stop: float, step: float, center=False) -> list[float]

class Coordinate:

def __init__(self, x: float | RealNumber, y: float | RealNumber) -> None:
self.__x = RealNumber(float(x))
self.__y = RealNumber(float(y))
def __init__(self, x: float | Real, y: float | Real) -> None:
self.__x = Real(float(x))
self.__y = Real(float(y))

def __eq__(self, coordinate: tuple[float, float] | Coordinate) -> bool:
c = Coordinate(*coordinate)
Expand Down Expand Up @@ -87,7 +87,7 @@ def __rmul__(self, factor: float) -> Coordinate:
return self.__mul__(factor)

def __truediv__(self, factor: float) -> Coordinate:
if RealNumber(factor) == 0:
if Real(factor) == 0:
return Coordinate(np.infty, np.infty)
return Coordinate(self.__x / factor, self.__y / factor)

Expand Down Expand Up @@ -291,9 +291,11 @@ def save(self, path: Path):
r = ",".join(f'{i:.16f}' for i in v)
f.write(f'{c},{r}\n')

def load(self, path: Path) -> None:
def load(self, path: Path) -> Mesh[T]:
mesh = Mesh(self._value_type)
if path.exists():
lines = path.read_text(encoding='utf-8').strip().split('\n')
lines = path.read_text(encoding='utf-8').strip().splitlines()
for line in lines:
token = line.split(',')
self.insert((float(token[0]), float(token[1])), self._value_type(*[float(i) for i in token[2:]]))
token = line.strip().split(',')
mesh.insert((float(token[0]), float(token[1])), self._value_type(*[float(i) for i in token[2:]]))
return mesh
15 changes: 7 additions & 8 deletions src/base/model/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import numpy as np

from src.base.model.algebra import RealNumber
from src.base.model.algebra import Real
from src.base.model.mesh import Coordinate, arrange, merge


Expand Down Expand Up @@ -123,7 +123,7 @@ def __add__(self, summand: float) -> Circle:

def __contains__(self, coordinate: tuple | Coordinate) -> bool:
c = Coordinate(*coordinate) - self.__center
return RealNumber(np.sqrt(c.x**2 + c.y**2)) <= RealNumber(self.__radius)
return Real(np.sqrt(c.x**2 + c.y**2)) <= Real(self.__radius)

def __getitem__(self, s: slice) -> _Polygon:
n = -((2 * np.pi * self.__radius) // -s.step)
Expand All @@ -148,7 +148,7 @@ def __add__(self, summand: float) -> Line:
return Line((self.__a.x - summand, self.__a.y - summand), (self.__b.x + summand, self.__b.y + summand))

def __getitem__(self, s: slice) -> _Polygon:
if RealNumber(self.__a.x) == RealNumber(self.__b.x):
if Real(self.__a.x) == Real(self.__b.x):
return _Polygon(*[Coordinate(self.__a.x, y) for y in arrange(self.__a.y, self.__b.y, s.step)])
m = (self.__b.y - self.__a.y) / (self.__b.x - self.__a.x)
coordinates = [Coordinate(x, self.__a.y + m * x) for x in arrange(self.__a.x, self.__b.x, s.step)]
Expand All @@ -160,7 +160,7 @@ def __getitem__(self, s: slice) -> _Polygon:

def __contains__(self, coordinate: tuple | Coordinate) -> bool:
c = Coordinate(*coordinate)
return RealNumber(self.__a.distance(c) + self.__b.distance(c)) == RealNumber(self.__a.distance(self.__b))
return Real(self.__a.distance(c) + self.__b.distance(c)) == Real(self.__a.distance(self.__b))


class Rectangle(Shape):
Expand All @@ -179,8 +179,7 @@ def shape(self):

def __contains__(self, coordinate: tuple | Coordinate) -> bool:
c = Coordinate(*coordinate)
return RealNumber(self.__a.x) <= c.x <= RealNumber(self.__b.x) and RealNumber(self.__a.y) <= c.y <= RealNumber(
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 @@ -232,9 +231,9 @@ def __f(self, x) -> tuple[Coordinate, Coordinate]:

def __contains__(self, coordinate: tuple[float, float] | Coordinate) -> bool:
c = (Coordinate(*coordinate) - self.__a) / self.__length
if 0 <= RealNumber(c.x) <= 1:
if 0 <= Real(c.x) <= 1:
upper, lower = self.__f(c.x)
return RealNumber(lower.y) <= RealNumber(c.y + 5e-3) and RealNumber(c.y - 5e-3) <= RealNumber(upper.y)
return Real(lower.y) <= Real(c.y + 5e-3) and Real(c.y - 5e-3) <= Real(upper.y)
return False

def __getitem__(self, s: slice) -> _Polygon:
Expand Down
31 changes: 31 additions & 0 deletions src/base/view/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import colors

COLORS: list[str] = ['black'] + list(colors.TABLEAU_COLORS.keys())[1:]
DEFAULT_COLOR: str = COLORS[1]

DPI: int = 1000
SCALE: float = 2.5

SEISMIC: plt.Colormap = colors.LinearSegmentedColormap.from_list('seismic',
plt.get_cmap('seismic')(np.linspace(0, 1., 100)))
SEISMIC_NEGATIVE: plt.Colormap = colors.LinearSegmentedColormap.from_list(
'seismic_neg',
plt.get_cmap('seismic')(np.linspace(0., .5, 50)))
SEISMIC_POSITIVE: plt.Colormap = colors.LinearSegmentedColormap.from_list(
'seismic_pos',
plt.get_cmap('seismic')(np.linspace(.5, 1., 50)))

PHI = (1. + np.sqrt(5.)) / 2.

# print(sorted(mpl.font_manager.get_font_names()))
plt.rcParams['font.family'] = 'cmr10'
plt.rcParams['mathtext.fontset'] = 'cm'
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['axes.formatter.use_mathtext'] = True
plt.rcParams['axes.linewidth'] = 1
plt.rcParams['xtick.major.width'] = 1
plt.rcParams['xtick.minor.width'] = .5
plt.rcParams['ytick.major.width'] = 1
plt.rcParams['ytick.minor.width'] = .5
Loading

0 comments on commit 7f64393

Please sign in to comment.