Skip to content

Commit

Permalink
Merge pull request #36 from praksharma/development
Browse files Browse the repository at this point in the history
basic domain created
  • Loading branch information
praksharma committed Jul 16, 2023
2 parents 45b98ad + 76eea11 commit 5c1d635
Show file tree
Hide file tree
Showing 11 changed files with 751 additions and 303 deletions.
1 change: 1 addition & 0 deletions DeepINN/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
from . import utils
from . import constraint

from . import domain

14 changes: 12 additions & 2 deletions DeepINN/constraint/templates.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
from .gradients import Jacobian
import torch

def burgers_pde(X, y):
# TODO
def test_function(X, y):
"""
The burgers equation.
x: array_like, N x D_in
y: array_like, N x D_out
"""
return x * np.sin(5 * x)
def Laplace(X, y):
"""
The laplace equation.
u_xx + u_yy
"""
dy_x = dde.grad.jacobian(y, x, i=0, j=0)
dy_t = dde.grad.jacobian(y, x, i=0, j=1)
Expand Down
1 change: 1 addition & 0 deletions DeepINN/domain/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .standard import Generic
23 changes: 23 additions & 0 deletions DeepINN/domain/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import abc

class Dataset(abc.ABC):
"""
Dataset base class
"""
def sample_collocation_points(self):
# returns the collocation points as tensor
raise NotImplementedError(f"{self.__class__.__name__} must implement the sample_collocation_points() method.")

def sample_collocation_labels(self):
# returns the collocation points using the above function.
# also returns the collocation points labels i.e. the residual of the PDE as tensor.
raise NotImplementedError(f"{self.__class__.__name__} must implement the sample_collocation_labels() method.")

def sample_boundary_points(self):
# returns a list of boundary points on each boundary as tensor
raise NotImplementedError(f"{self.__class__.__name__} must implement the sample_boundary_points method.")

def sample_boundary_labels(self):
# returns the list of boundary points on each boundary using the above function.
# returns a list of tensor boundary conditions on each boundary.
raise NotImplementedError(f"{self.__class__.__name__} must implement the sample_boundary_labels() method.")
50 changes: 50 additions & 0 deletions DeepINN/domain/standard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from .base import Dataset

class Generic(Dataset):
"""
Very generic dataset loader. It loads the entire dataset in one go, which can lead to overfitting in stiff-PDEs.
Parameters
----------
pde_equation: a method which returns the PDE equation as tensor object (not numpy)
collocation_object: A PDE sampler.
Instance of DeepINN.constraint.PDE()
bc_object: A BC sampler.
Instance of dp.constraint.DirichletBC() or Neumann BC (to be implemented).
"""
def __init__(self, pde_equation, collocation_object, bc_object) -> None:
super().__init__()

self.pde = pde_equation
self.pde_sampler = collocation_object
# if the bcs is not list, then make it a list
self.bc_sampler = bc_object if isinstance(bc_object, (list)) else [bc_object]

self.bc_list_len = len(self.bc_sampler)

def sample_collocation_points(self):
# returns the collocation points as tensor.
# the DeepINN.constraint.PDE() takes care of the missing column in 1D tensor. See tutorials/4. dataset/ 1.basic.ipynb
return self.pde_sampler.sampler_object().sample_points().as_tensor

def sample_collocation_labels(self):
# returns the collocation points using the above function.
# also returns the collocation points labels i.e. the residual of the PDE as tensor.
self.collocation_points = self.sample_collocation_points() # returns a tensor of sampled collocation points
return self.collocation_points, self.pde_sampler.sample_labels(self.collocation_points).unsqueeze(1)

def sample_boundary_points(self):
# returns a list of boundary points on each boundary as tensor
self.bc_points_list =[]
for bc in self.bc_sampler:
self.bc_points_list.append(bc.sampler_object().sample_points().as_tensor)
return self.bc_points_list

def sample_boundary_labels(self):
# returns the list of boundary points on each boundary using the above function.
# returns a list of tensor boundary conditions on each boundary.
self.bc_labels_list = []
self.bc_points = self.sample_boundary_points() # returns a list of boundary points on each boundary as tensor
for bc_number in range(0, self.bc_list_len):
self.bc_labels_list.append(self.bc_sampler[bc_number].sample_labels(self.bc_points[bc_number]).unsqueeze(1))
return self.bc_points, self.bc_labels_list
8 changes: 4 additions & 4 deletions DeepINN/utils/plotting/scatter_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import matplotlib.pyplot as plt


def scatter(subspace, *samplers, dpi=False, save=False):
def scatter(subspace, *samplers, dpi=100, save=False):
"""Shows (one batch) of used points in the training. If the sampler is
static, the shown points will be the points for the training. If not
the points may vary, depending of the sampler.
Expand Down Expand Up @@ -67,7 +67,7 @@ def _choose_scatter_function(space_dim):


def _scatter_1D(points, labels, dpi, save):
fig = plt.figure()
fig = plt.figure(dpi=dpi)
ax = fig.add_subplot()
ax.grid()
ax.scatter(points, np.zeros_like(points))
Expand All @@ -77,7 +77,7 @@ def _scatter_1D(points, labels, dpi, save):


def _scatter_2D(points, labels, dpi, save):
fig = plt.figure()
fig = plt.figure(dpi=dpi)
ax = fig.add_subplot()
ax.grid()
ax.scatter(points[:, 0], points[:, 1])
Expand All @@ -88,7 +88,7 @@ def _scatter_2D(points, labels, dpi, save):


def _scatter_3D(points, labels, dpi, save):
fig = plt.figure()
fig = plt.figure(dpi=dpi)
ax = fig.add_subplot(projection='3d')
ax.grid()
ax.scatter(points[:, 0], points[:, 1], points[:, 2])
Expand Down
14 changes: 7 additions & 7 deletions Tutorials/2. BC/1. dirichlet.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit 5c1d635

Please sign in to comment.