preliminary modifications for N-S
This commit is contained in:
@@ -6,7 +6,12 @@ from .location import Location
|
||||
class Condition:
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
||||
if len(args) == 2 and not kwargs:
|
||||
if 'data_weight' in kwargs:
|
||||
self.data_weight = kwargs['data_weight']
|
||||
if not 'data_weight' in kwargs:
|
||||
self.data_weight = 1.
|
||||
|
||||
if len(args) == 2:
|
||||
|
||||
if (isinstance(args[0], torch.Tensor) and
|
||||
isinstance(args[1], torch.Tensor)):
|
||||
@@ -21,7 +26,7 @@ class Condition:
|
||||
else:
|
||||
raise ValueError
|
||||
|
||||
elif not args and len(kwargs) == 2:
|
||||
elif not args and len(kwargs) >= 2:
|
||||
|
||||
if 'input_points' in kwargs and 'output_points' in kwargs:
|
||||
self.input_points = kwargs['input_points']
|
||||
@@ -33,3 +38,4 @@ class Condition:
|
||||
raise ValueError
|
||||
else:
|
||||
raise ValueError
|
||||
|
||||
|
||||
@@ -3,7 +3,8 @@ import torch
|
||||
import torch.nn as nn
|
||||
|
||||
from pina.label_tensor import LabelTensor
|
||||
|
||||
import warnings
|
||||
import copy
|
||||
|
||||
class DeepONet(torch.nn.Module):
|
||||
"""
|
||||
@@ -18,7 +19,7 @@ class DeepONet(torch.nn.Module):
|
||||
<https://doi.org/10.1038/s42256-021-00302-5>`_
|
||||
|
||||
"""
|
||||
def __init__(self, branch_net, trunk_net, output_variables):
|
||||
def __init__(self, branch_net, trunk_net, output_variables, inner_size=10):
|
||||
"""
|
||||
:param torch.nn.Module branch_net: the neural network to use as branch
|
||||
model. It has to take as input a :class:`LabelTensor`. The number
|
||||
@@ -43,7 +44,7 @@ class DeepONet(torch.nn.Module):
|
||||
(1): Tanh()
|
||||
(2): Linear(in_features=20, out_features=20, bias=True)
|
||||
(3): Tanh()
|
||||
(4): Linear(in_features=20, out_features=10, bias=True)
|
||||
(4): Linear(in_features=20, out_features=20, bias=True)
|
||||
)
|
||||
)
|
||||
(branch_net): FeedForward(
|
||||
@@ -53,20 +54,27 @@ class DeepONet(torch.nn.Module):
|
||||
(1): Tanh()
|
||||
(2): Linear(in_features=20, out_features=20, bias=True)
|
||||
(3): Tanh()
|
||||
(4): Linear(in_features=20, out_features=10, bias=True)
|
||||
(4): Linear(in_features=20, out_features=20, bias=True)
|
||||
)
|
||||
)
|
||||
)
|
||||
"""
|
||||
super().__init__()
|
||||
|
||||
self.output_variables = output_variables
|
||||
self.output_dimension = len(output_variables)
|
||||
|
||||
self.trunk_net = trunk_net
|
||||
self.branch_net = branch_net
|
||||
|
||||
self.output_variables = output_variables
|
||||
self.output_dimension = len(output_variables)
|
||||
if self.output_dimension > 1:
|
||||
raise NotImplementedError('Vectorial DeepONet to be implemented')
|
||||
if isinstance(self.branch_net.output_variables, int) and isinstance(self.branch_net.output_variables, int):
|
||||
if self.branch_net.output_dimension == self.trunk_net.output_dimension:
|
||||
self.inner_size = self.branch_net.output_dimension
|
||||
else:
|
||||
raise ValueError('Branch and trunk networks have not the same output dimension.')
|
||||
else:
|
||||
warnings.warn("The output dimension of the branch and trunk networks has been imposed by default as 10 for each output variable. To set it change the output_variable of networks to an integer.")
|
||||
self.inner_size = self.output_dimension*inner_size
|
||||
|
||||
@property
|
||||
def input_variables(self):
|
||||
@@ -82,10 +90,16 @@ class DeepONet(torch.nn.Module):
|
||||
:rtype: LabelTensor
|
||||
"""
|
||||
branch_output = self.branch_net(
|
||||
x.extract(self.branch_net.input_variables))
|
||||
x.extract(self.branch_net.input_variables))
|
||||
trunk_output = self.trunk_net(
|
||||
x.extract(self.trunk_net.input_variables))
|
||||
|
||||
output_ = torch.sum(branch_output * trunk_output, dim=1).reshape(-1, 1)
|
||||
|
||||
return LabelTensor(output_, self.output_variables)
|
||||
x.extract(self.trunk_net.input_variables))
|
||||
local_size = int(self.inner_size/self.output_dimension)
|
||||
for i, var in enumerate(self.output_variables):
|
||||
start = i*local_size
|
||||
stop = (i+1)*local_size
|
||||
local_output = LabelTensor(torch.sum(branch_output[:, start:stop] * trunk_output[:, start:stop], dim=1).reshape(-1, 1), var)
|
||||
if i==0:
|
||||
output_ = local_output
|
||||
else:
|
||||
output_ = output_.append(local_output)
|
||||
return output_
|
||||
|
||||
@@ -30,7 +30,7 @@ def div(output_, input_):
|
||||
else: # really to improve
|
||||
a = []
|
||||
for o in output_.T:
|
||||
a.append(grad(o, input_))
|
||||
a.append(grad(o, input_).extract(['x', 'y']))
|
||||
div = torch.zeros(output_.shape[0], 1)
|
||||
for i in range(output_.shape[1]):
|
||||
div += a[i][:, i].reshape(-1, 1)
|
||||
@@ -42,4 +42,25 @@ def nabla(output_, input_):
|
||||
"""
|
||||
TODO
|
||||
"""
|
||||
return div(grad(output_, input_), input_)
|
||||
return div(grad(output_, input_).extract(['x', 'y']), input_)
|
||||
|
||||
|
||||
def advection_term(output_, input_):
|
||||
"""
|
||||
TODO
|
||||
"""
|
||||
dimension = len(output_.labels)
|
||||
for i, label in enumerate(output_.labels):
|
||||
# compute u dot gradient in each direction
|
||||
gradient_loc = grad(output_.extract([label]), input_).extract(input_.labels[:dimension])
|
||||
dim_0 = gradient_loc.shape[0]
|
||||
dim_1 = gradient_loc.shape[1]
|
||||
u_dot_grad_loc = torch.bmm(output_.view(dim_0, 1, dim_1),
|
||||
gradient_loc.view(dim_0, dim_1, 1))
|
||||
u_dot_grad_loc = LabelTensor(torch.reshape(u_dot_grad_loc,
|
||||
(u_dot_grad_loc.shape[0], u_dot_grad_loc.shape[1])), [input_.labels[i]])
|
||||
if i==0:
|
||||
adv_term = u_dot_grad_loc
|
||||
else:
|
||||
adv_term = adv_term.append(u_dot_grad_loc)
|
||||
return adv_term
|
||||
|
||||
59
pina/pinn.py
59
pina/pinn.py
@@ -5,6 +5,7 @@ import numpy as np
|
||||
from pina.label_tensor import LabelTensor
|
||||
torch.pi = torch.acos(torch.zeros(1)).item() * 2 # which is 3.1415927410125732
|
||||
|
||||
|
||||
class PINN(object):
|
||||
|
||||
def __init__(self,
|
||||
@@ -13,7 +14,6 @@ class PINN(object):
|
||||
optimizer=torch.optim.Adam,
|
||||
lr=0.001,
|
||||
regularizer=0.00001,
|
||||
data_weight=1.,
|
||||
dtype=torch.float32,
|
||||
device='cpu',
|
||||
error_norm='mse'):
|
||||
@@ -53,13 +53,10 @@ class PINN(object):
|
||||
self.truth_values = {}
|
||||
self.input_pts = {}
|
||||
|
||||
|
||||
self.trained_epoch = 0
|
||||
self.optimizer = optimizer(
|
||||
self.model.parameters(), lr=lr, weight_decay=regularizer)
|
||||
|
||||
self.data_weight = data_weight
|
||||
|
||||
@property
|
||||
def problem(self):
|
||||
return self._problem
|
||||
@@ -96,6 +93,7 @@ class PINN(object):
|
||||
'optimizer_state' : self.optimizer.state_dict(),
|
||||
'optimizer_class' : self.optimizer.__class__,
|
||||
'history' : self.history,
|
||||
'input_points_dict' : self.input_pts,
|
||||
}
|
||||
|
||||
# TODO save also architecture param?
|
||||
@@ -117,22 +115,27 @@ class PINN(object):
|
||||
self.trained_epoch = checkpoint['epoch']
|
||||
self.history = checkpoint['history']
|
||||
|
||||
self.input_pts = checkpoint['input_points_dict']
|
||||
|
||||
return self
|
||||
|
||||
|
||||
def span_pts(self, n, mode='grid', locations='all'):
|
||||
def span_pts(self, n_spatial, n_params=0, mode_spatial='grid', mode_param='random', locations='all'):
|
||||
if locations == 'all':
|
||||
locations = [condition for condition in self.problem.conditions]
|
||||
|
||||
for location in locations:
|
||||
condition = self.problem.conditions[location]
|
||||
|
||||
try:
|
||||
pts = condition.location.sample(n, mode)
|
||||
pts = condition.location.sample(n_spatial, mode_spatial, variables=self.problem.spatial_variables)
|
||||
if n_params != 0:
|
||||
pts_params = condition.location.sample(n_params, mode_param, variables=self.problem.parameters)
|
||||
pts = LabelTensor(pts.repeat(n_params, 1), pts.labels)
|
||||
pts_params = LabelTensor(pts_params.repeat_interleave(n_spatial).reshape((n_spatial*n_params, len(self.problem.parameters))), pts_params.labels)
|
||||
pts = pts.append(pts_params)
|
||||
except:
|
||||
pts = condition.input_points
|
||||
|
||||
self.input_pts[location] = pts#.double() # TODO
|
||||
self.input_pts[location] = pts #.double() # TODO
|
||||
self.input_pts[location] = (
|
||||
self.input_pts[location].to(dtype=self.dtype,
|
||||
device=self.device))
|
||||
@@ -140,19 +143,16 @@ class PINN(object):
|
||||
self.input_pts[location].retain_grad()
|
||||
|
||||
|
||||
|
||||
def plot_pts(self, locations='all'):
|
||||
import matplotlib
|
||||
matplotlib.use('GTK3Agg')
|
||||
# matplotlib.use('GTK3Agg')
|
||||
if locations == 'all':
|
||||
locations = [condition for condition in self.problem.conditions]
|
||||
|
||||
for location in locations:
|
||||
x, y = self.input_pts[location].tensor.T
|
||||
#plt.plot(x.detach(), y.detach(), 'o', label=location)
|
||||
np.savetxt('burgers_{}_pts.txt'.format(location), self.input_pts[location].tensor.detach(), header='x y', delimiter=' ')
|
||||
|
||||
|
||||
x = self.input_pts[location].extract(['x'])
|
||||
y = self.input_pts[location].extract(['y'])
|
||||
plt.plot(x.detach(), y.detach(), '.', label=location)
|
||||
# np.savetxt('burgers_{}_pts.txt'.format(location), self.input_pts[location].tensor.detach(), header='x y', delimiter=' ')
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
@@ -169,18 +169,23 @@ class PINN(object):
|
||||
for condition_name in self.problem.conditions:
|
||||
condition = self.problem.conditions[condition_name]
|
||||
pts = self.input_pts[condition_name]
|
||||
|
||||
predicted = self.model(pts)
|
||||
|
||||
if isinstance(condition.function, list):
|
||||
for function in condition.function:
|
||||
residuals = function(pts, predicted)
|
||||
losses.append(self._compute_norm(residuals))
|
||||
else:
|
||||
residuals = condition.function(pts, predicted)
|
||||
losses.append(self._compute_norm(residuals))
|
||||
|
||||
if hasattr(condition, 'function'):
|
||||
if isinstance(condition.function, list):
|
||||
for function in condition.function:
|
||||
residuals = function(pts, predicted)
|
||||
local_loss = condition.data_weight*self._compute_norm(residuals)
|
||||
losses.append(local_loss)
|
||||
else:
|
||||
residuals = condition.function(pts, predicted)
|
||||
local_loss = condition.data_weight*self._compute_norm(residuals)
|
||||
losses.append(local_loss)
|
||||
elif hasattr(condition, 'output_points'):
|
||||
residuals = predicted - condition.output_points
|
||||
local_loss = condition.data_weight*self._compute_norm(residuals)
|
||||
losses.append(local_loss)
|
||||
self.optimizer.zero_grad()
|
||||
|
||||
sum(losses).backward()
|
||||
self.optimizer.step()
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
""" Module for plotting. """
|
||||
import matplotlib
|
||||
matplotlib.use('Qt5Agg')
|
||||
#matplotlib.use('Qt5Agg')
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import torch
|
||||
@@ -32,15 +32,15 @@ class Plotter:
|
||||
truth_output = obj.problem.truth_solution(*pts.tensor.T).float()
|
||||
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(16, 6))
|
||||
|
||||
cb = getattr(axes[0], method)(*grids_container, predicted_output.tensor.reshape(res, res).detach())
|
||||
cb = getattr(axes[0], method)(*grids_container, predicted_output.reshape(res, res).detach())
|
||||
fig.colorbar(cb, ax=axes[0])
|
||||
cb = getattr(axes[1], method)(*grids_container, truth_output.reshape(res, res).detach())
|
||||
fig.colorbar(cb, ax=axes[1])
|
||||
cb = getattr(axes[2], method)(*grids_container, (truth_output-predicted_output.tensor.float().flatten()).detach().reshape(res, res))
|
||||
cb = getattr(axes[2], method)(*grids_container, (truth_output-predicted_output.float().flatten()).detach().reshape(res, res))
|
||||
fig.colorbar(cb, ax=axes[2])
|
||||
else:
|
||||
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(8, 6))
|
||||
cb = getattr(axes, method)(*grids_container, predicted_output.tensor.reshape(res, res).detach())
|
||||
cb = getattr(axes, method)(*grids_container, predicted_output.reshape(res, res).detach())
|
||||
fig.colorbar(cb, ax=axes)
|
||||
|
||||
|
||||
@@ -66,66 +66,50 @@ class Plotter:
|
||||
truth_output = obj.problem.truth_solution(*pts.tensor.T).float()
|
||||
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(16, 6))
|
||||
|
||||
cb = getattr(axes[0], method)(*grids_container, predicted_output.tensor.reshape(res, res).detach())
|
||||
cb = getattr(axes[0], method)(*grids_container, predicted_output.reshape(res, res).detach())
|
||||
fig.colorbar(cb, ax=axes[0])
|
||||
cb = getattr(axes[1], method)(*grids_container, truth_output.reshape(res, res).detach())
|
||||
fig.colorbar(cb, ax=axes[1])
|
||||
cb = getattr(axes[2], method)(*grids_container, (truth_output-predicted_output.tensor.float().flatten()).detach().reshape(res, res))
|
||||
cb = getattr(axes[2], method)(*grids_container, (truth_output-predicted_output.float().flatten()).detach().reshape(res, res))
|
||||
fig.colorbar(cb, ax=axes[2])
|
||||
else:
|
||||
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(8, 6))
|
||||
cb = getattr(axes, method)(*grids_container, predicted_output.tensor.reshape(res, res).detach())
|
||||
fig.colorbar(cb, ax=axes)
|
||||
|
||||
|
||||
|
||||
def plot(self, obj, method='contourf', filename=None):
|
||||
"""
|
||||
"""
|
||||
res = 256
|
||||
pts = obj.problem.domain.sample(res, 'grid')
|
||||
print(pts)
|
||||
grids_container = [
|
||||
pts.tensor[:, 0].reshape(res, res),
|
||||
pts.tensor[:, 1].reshape(res, res),
|
||||
]
|
||||
predicted_output = obj.model(pts)
|
||||
predicted_output = predicted_output['p']
|
||||
|
||||
if hasattr(obj.problem, 'truth_solution'):
|
||||
truth_output = obj.problem.truth_solution(*pts.tensor.T).float()
|
||||
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(16, 6))
|
||||
|
||||
cb = getattr(axes[0], method)(*grids_container, predicted_output.tensor.reshape(res, res).detach())
|
||||
fig.colorbar(cb, ax=axes[0])
|
||||
cb = getattr(axes[1], method)(*grids_container, truth_output.reshape(res, res).detach())
|
||||
fig.colorbar(cb, ax=axes[1])
|
||||
cb = getattr(axes[2], method)(*grids_container, (truth_output-predicted_output.tensor.float().flatten()).detach().reshape(res, res))
|
||||
fig.colorbar(cb, ax=axes[2])
|
||||
else:
|
||||
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(8, 6))
|
||||
# cb = getattr(axes, method)(*grids_container, predicted_output.tensor.reshape(res, res).detach())
|
||||
cb = getattr(axes, method)(*grids_container, predicted_output.reshape(res, res).detach())
|
||||
fig.colorbar(cb, ax=axes)
|
||||
|
||||
if filename:
|
||||
plt.savefig(filename)
|
||||
else:
|
||||
plt.show()
|
||||
|
||||
|
||||
def plot(self, obj, method='contourf', filename=None):
|
||||
def plot(self, obj, method='contourf', component='u', parametric=False, params_value=1, filename=None):
|
||||
"""
|
||||
"""
|
||||
res = 256
|
||||
pts = obj.problem.domain.sample(res, 'grid')
|
||||
if parametric:
|
||||
pts_params = torch.ones(pts.shape[0], len(obj.problem.parameters), dtype=pts.dtype)*params_value
|
||||
pts_params = LabelTensor(pts_params, obj.problem.parameters)
|
||||
pts = pts.append(pts_params)
|
||||
grids_container = [
|
||||
pts[:, 0].reshape(res, res),
|
||||
pts[:, 1].reshape(res, res),
|
||||
]
|
||||
ind_dict = {}
|
||||
all_locations = [condition for condition in obj.problem.conditions]
|
||||
for location in all_locations:
|
||||
if hasattr(obj.problem.conditions[location], 'location'):
|
||||
keys_range_ = obj.problem.conditions[location].location.range_.keys()
|
||||
if ('x' in keys_range_) and ('y' in keys_range_):
|
||||
range_x = obj.problem.conditions[location].location.range_['x']
|
||||
range_y = obj.problem.conditions[location].location.range_['y']
|
||||
ind_x = np.where(np.logical_or(pts[:, 0]<range_x[0], pts[:, 0]>range_x[1]))
|
||||
ind_y = np.where(np.logical_or(pts[:, 1]<range_y[0], pts[:, 1]>range_y[1]))
|
||||
ind_to_exclude = np.union1d(ind_x, ind_y)
|
||||
ind_dict[location] = ind_to_exclude
|
||||
import functools
|
||||
from functools import reduce
|
||||
final_inds = reduce(np.intersect1d, ind_dict.values())
|
||||
predicted_output = obj.model(pts)
|
||||
predicted_output = predicted_output.extract(['u'])
|
||||
|
||||
predicted_output = predicted_output.extract([component])
|
||||
predicted_output[final_inds] = np.nan
|
||||
if hasattr(obj.problem, 'truth_solution'):
|
||||
truth_output = obj.problem.truth_solution(*pts.T).float()
|
||||
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(16, 6))
|
||||
@@ -142,16 +126,16 @@ class Plotter:
|
||||
fig.colorbar(cb, ax=axes)
|
||||
|
||||
if filename:
|
||||
plt.title('Output {} with parameter {}'.format(component, params_value))
|
||||
plt.savefig(filename)
|
||||
else:
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
def plot_samples(self, obj):
|
||||
|
||||
for location in obj.input_pts:
|
||||
plt.plot(*obj.input_pts[location].T.detach(), '.', label=location)
|
||||
pts_x = obj.input_pts[location].extract(['x'])
|
||||
pts_y = obj.input_pts[location].extract(['y'])
|
||||
plt.plot(pts_x.detach(), pts_y.detach(), '.', label=location)
|
||||
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
43
pina/span.py
43
pina/span.py
@@ -20,9 +20,27 @@ class Span(Location):
|
||||
else:
|
||||
raise TypeError
|
||||
|
||||
def sample(self, n, mode='random'):
|
||||
def sample(self, n, mode='random', variables='all'):
|
||||
|
||||
bounds = np.array(list(self.range_.values()))
|
||||
if variables=='all':
|
||||
spatial_range_ = list(self.range_.keys())
|
||||
spatial_fixed_ = list(self.fixed_.keys())
|
||||
bounds = np.array(list(self.range_.values()))
|
||||
fixed = np.array(list(self.fixed_.values()))
|
||||
else:
|
||||
bounds = []
|
||||
spatial_range_ = []
|
||||
spatial_fixed_ = []
|
||||
fixed = []
|
||||
for variable in variables:
|
||||
if variable in self.range_.keys():
|
||||
spatial_range_.append(variable)
|
||||
bounds.append(list(self.range_[variable]))
|
||||
elif variable in self.fixed_.keys():
|
||||
spatial_fixed_.append(variable)
|
||||
fixed.append(int(self.fixed_[variable]))
|
||||
fixed = torch.Tensor(fixed)
|
||||
bounds = np.array(bounds)
|
||||
if mode == 'random':
|
||||
pts = np.random.uniform(size=(n, bounds.shape[0]))
|
||||
elif mode == 'chebyshev':
|
||||
@@ -41,23 +59,24 @@ class Span(Location):
|
||||
from scipy.stats import qmc
|
||||
sampler = qmc.LatinHypercube(d=bounds.shape[0])
|
||||
pts = sampler.random(n)
|
||||
|
||||
# Scale pts
|
||||
pts *= bounds[:, 1] - bounds[:, 0]
|
||||
pts += bounds[:, 0]
|
||||
|
||||
pts = pts.astype(np.float32)
|
||||
pts = torch.from_numpy(pts)
|
||||
|
||||
fixed = torch.Tensor(list(self.fixed_.values()))
|
||||
pts_fixed_ = torch.ones(pts.shape[0], len(self.fixed_),
|
||||
dtype=pts.dtype) * fixed
|
||||
pts_range_ = LabelTensor(pts, list(self.range_.keys()))
|
||||
pts_fixed_ = LabelTensor(pts_fixed_, list(self.fixed_.keys()))
|
||||
pts_range_ = LabelTensor(pts, spatial_range_)
|
||||
|
||||
if not len(spatial_fixed_)==0:
|
||||
pts_fixed_ = torch.ones(pts.shape[0], len(spatial_fixed_),
|
||||
dtype=pts.dtype) * fixed
|
||||
|
||||
pts_fixed_ = LabelTensor(pts_fixed_, spatial_fixed_)
|
||||
pts_range_ = pts_range_.append(pts_fixed_)
|
||||
|
||||
return pts_range_
|
||||
|
||||
if self.fixed_:
|
||||
return pts_range_.append(pts_fixed_)
|
||||
else:
|
||||
return pts_range_
|
||||
|
||||
def meshgrid(self, n):
|
||||
pts = np.array([
|
||||
|
||||
Reference in New Issue
Block a user