Skip to content

Commit

Permalink
init. basic examples good.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisWu1997 committed Oct 8, 2022
0 parents commit 0619ed4
Show file tree
Hide file tree
Showing 14 changed files with 629 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__
outputs
12 changes: 12 additions & 0 deletions configs/inflow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"tag": "inflow",
"example": "inflow",
"N": 256,
"dt": 1,
"T": 200,
"diff": 0,
"visc": 0,
"draw": "mix",
"fps": 20,
"save_grids": 0
}
12 changes: 12 additions & 0 deletions configs/taylorgreen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"tag": "taylorgreen",
"example": "taylorgreen",
"N": 32,
"dt": 0.05,
"T": 100,
"diff": 0,
"visc": 0,
"draw": "velocity",
"fps": 20,
"save_grids": 0
}
12 changes: 12 additions & 0 deletions configs/vortexsheet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"tag": "vortexsheet",
"example": "vortexsheet",
"N": 128,
"dt": 0.025,
"T": 100,
"diff": 0,
"visc": 0,
"draw": "density",
"fps": 20,
"save_grids": 0
}
11 changes: 11 additions & 0 deletions examples/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import importlib


def get_example_setup(name):
try:
module = importlib.import_module("examples." + name)
setup = getattr(module, "setup")
return setup
except Exception as e:
print(e)
raise RuntimeError(f"Cannot find example setup. Example name: {name}.")
38 changes: 38 additions & 0 deletions examples/inflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# from https://github.com/GregTJ/stable-fluids/blob/main/example.py
import numpy as np


def create_circular_points(point_dist: float, point_count: int):
points = np.linspace(-np.pi, np.pi, point_count, endpoint=False)
points = tuple(np.array((np.cos(p), np.sin(p))) for p in points)
normals = tuple(-p for p in points)
points = tuple(point_dist * p for p in points)
return points, normals


def inflow_velocity(coords: np.ndarray, vel: float=1. / 250, point_radius: float=0.032, point_dist: float=0.8, point_count: int=4):
points, normals = create_circular_points(point_dist, point_count)

inflow_velocity = np.zeros_like(coords)
for p, n in zip(points, normals):
mask = np.linalg.norm(coords - p[None, None], 2, axis=-1) <= point_radius
inflow_velocity[mask] += n[None] * vel
return inflow_velocity


def inflow_density(coords: np.ndarray, point_radius: float=0.032, point_dist: float=0.8, point_count: int=4):
points, normals = create_circular_points(point_dist, point_count)

inflow_density = np.zeros(coords.shape[:-1])
for p in points:
mask = np.linalg.norm(coords - p[None, None], 2, axis=-1) <= point_radius
inflow_density[mask] = 1
return inflow_density


setup = {
"domain": [[-1, 1], [-1, 1]],
"vsource": inflow_velocity,
"dsource": inflow_density,
"src_duration": 60
}
30 changes: 30 additions & 0 deletions examples/taylorgreen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import numpy as np


def taylorgreen_velocity(coords: np.ndarray):
"""taylorgreen velocity. domain: [0, 2pi] x [0, 2pi].
Args:
coords (np.ndarray): coordinates of shape (..., 2)
Returns:
velocity: velocity at coordinates
"""
A = 1
a = 1
B = -1
b = 1
x = coords[..., 0]
y = coords[..., 1]
u = A * np.sin(a * x) * np.cos(b * y)
v = B * np.cos(a * x) * np.sin(b * y)
vel = np.stack([u, v], axis=-1)
return vel


setup = {
"domain": [[0, 2 * np.pi], [0, 2 * np.pi]],
"vsource": taylorgreen_velocity,
"dsource": None,
"src_duration": 1
}
30 changes: 30 additions & 0 deletions examples/vortexsheet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import numpy as np


def vortexsheet_velocity(coords: np.ndarray, rigidR=0.5, rate=0.1):
"""domain: [-1, 1] x [-1, 1]"""
w = 1 * 1.0 / rate
R = np.linalg.norm(coords, 2, axis=-1)
mask = R < rigidR
weight = 1
u = w * coords[..., 1] * weight
v = -w * coords[..., 0] * weight
u[~mask] = 0
v[~mask] = 0
vel = np.stack([u, v], axis=-1)
return vel


def vortexsheet_density(coords: np.ndarray, rigidR=0.5):
R = np.linalg.norm(coords, 2, axis=-1)
den = np.zeros(coords.shape[:-1])
den[R < rigidR] = 1.0
return den


setup = {
"domain": [[-1, 1], [-1, 1]],
"vsource": vortexsheet_velocity,
"dsource": vortexsheet_density,
"src_duration": 1
}
Loading

0 comments on commit 0619ed4

Please sign in to comment.