tmp commit - toward 0.0.1

This commit is contained in:
Your Name
2021-11-29 15:29:00 +01:00
parent beae301a58
commit fb16fc7f3a
38 changed files with 2790 additions and 1 deletions

78
problems/burgers.py Normal file
View File

@@ -0,0 +1,78 @@
import numpy as np
import scipy.io
import torch
from pina.problem import Problem
from pina.segment import Segment
from pina.cube import Cube
from pina.tdproblem1d import TimeDepProblem1D
def tmp_grad(output_, input_):
return torch.autograd.grad(
output_,
input_.tensor,
grad_outputs=torch.ones(output_.size()).to(
dtype=input_.tensor.dtype,
device=input_.tensor.device),
create_graph=True, retain_graph=True, allow_unused=True)[0]
class Burgers1D(TimeDepProblem1D):
def __init__(self):
def burger_equation(input_, output_):
grad_u = self.grad(output_['u'], input_)
grad_x, grad_t = tmp_grad(output_['u'], input_).T
gradgrad_u_x = self.grad(grad_u['x'], input_)
grad_xx = tmp_grad(grad_x, input_)[:, 0]
#print(grad_t, grad_u['t'])
#rrrr
return grad_u['t'] + output_['u']*grad_u['x'] - (0.01/torch.pi)*gradgrad_u_x['x']
def nil_dirichlet(input_, output_):
u_expected = 0.0
return output_['u'] - u_expected
def initial_condition(input_, output_):
u_expected = -torch.sin(torch.pi*input_['x'])
return output_['u'] - u_expected
self.conditions = {
'gamma1': {'location': Segment((-1, 0), (-1, 1)), 'func': nil_dirichlet},
'gamma2': {'location': Segment(( 1, 0), ( 1, 1)), 'func': nil_dirichlet},
'initia': {'location': Segment((-1, 0), ( 1, 0)), 'func': initial_condition},
'D': {'location': Cube([[-1, 1],[0,1]]), 'func': burger_equation}
}
self.input_variables = ['x', 't']
self.output_variables = ['u']
self.spatial_domain = Cube([[0, 1]])
self.temporal_domain = Cube([[0, 1]])
bc = (
(-1, lambda x: torch.zeros(x.shape[0], 1)),
( 1, lambda x: torch.zeros(x.shape[0], 1))
)
initial = lambda x: -np.sin(np.pi*x[:,0]).reshape(-1, 1)
def equation(x, fx):
grad_x, grad_t = Problem.grad(fx, x).T
grad_xx = Problem.grad(grad_x, x)[:, 0]
a = grad_t + fx.flatten()*grad_x - (0.01/torch.pi)*grad_xx
return a
burgers = TimeDepProblem1D(bc=bc, initial=initial, tend=1, domain_bound=[-1, 1])
burgers.equation = equation
# read data for errors and plots
data = scipy.io.loadmat('Data/burgers_shock.mat')
data_solution = {'grid': np.meshgrid(data['x'], data['t']), 'grid_solution': data['usol'].T}
burgers.data_solution = data_solution

View File

@@ -0,0 +1,49 @@
import numpy as np
import torch
from pina.problem import Problem
from pina.segment import Segment
from pina.cube import Cube
from pina.problem2d import Problem2D
xmin, xmax, ymin, ymax = -1, 1, -1, 1
class EllipticOptimalControl(Problem2D):
def __init__(self, alpha=1):
def term1(input_, output_):
grad_p = self.grad(output_['p'], input_)
gradgrad_p_x1 = self.grad(grad_p['x1'], input_)
gradgrad_p_x2 = self.grad(grad_p['x2'], input_)
yd = 2.0
return output_['y'] - yd - (gradgrad_p_x1['x1'] + gradgrad_p_x2['x2'])
def term2(input_, output_):
grad_y = self.grad(output_['y'], input_)
gradgrad_y_x1 = self.grad(grad_y['x1'], input_)
gradgrad_y_x2 = self.grad(grad_y['x2'], input_)
return - (gradgrad_y_x1['x1'] + gradgrad_y_x2['x2']) - output_['u']
def term3(input_, output_):
return output_['p'] - output_['u']*alpha
def nil_dirichlet(input_, output_):
y_value = 0.0
p_value = 0.0
return torch.abs(output_['y'] - y_value) + torch.abs(output_['p'] - p_value)
self.conditions = {
'gamma1': {'location': Segment((xmin, ymin), (xmax, ymin)), 'func': nil_dirichlet},
'gamma2': {'location': Segment((xmax, ymin), (xmax, ymax)), 'func': nil_dirichlet},
'gamma3': {'location': Segment((xmax, ymax), (xmin, ymax)), 'func': nil_dirichlet},
'gamma4': {'location': Segment((xmin, ymax), (xmin, ymin)), 'func': nil_dirichlet},
'D1': {'location': Cube([[xmin, xmax], [ymin, ymax]]), 'func': [term1, term2, term3]},
#'D2': {'location': Cube([[0, 1], [0, 1]]), 'func': term2},
#'D3': {'location': Cube([[0, 1], [0, 1]]), 'func': term3}
}
self.input_variables = ['x1', 'x2']
self.output_variables = ['u', 'p', 'y']
self.spatial_domain = Cube([[xmin, xmax], [xmin, xmax]])

View File

@@ -0,0 +1,55 @@
import numpy as np
import torch
from pina.problem import Problem
from pina.segment import Segment
from pina.parametricproblem2d import ParametricProblem2D
bc = {
'y': (
(Segment((0, 0), (4, 0)), lambda x: torch.ones(x.shape[0], 1)),
(Segment((4, 0), (4, 1)), lambda x: torch.ones(x.shape[0], 1)),
(Segment((4, 1), (0, 1)), lambda x: torch.ones(x.shape[0], 1)),
(Segment((0, 1), (0, 0)), lambda x: torch.ones(x.shape[0], 1)),
),
'p': (
(Segment((0, 0), (4, 0)), lambda x: torch.zeros(x.shape[0], 1)),
(Segment((4, 0), (4, 1)), lambda x: torch.zeros(x.shape[0], 1)),
(Segment((4, 1), (0, 1)), lambda x: torch.zeros(x.shape[0], 1)),
(Segment((0, 1), (0, 0)), lambda x: torch.zeros(x.shape[0], 1)),
)
}
# optimal control parameters and data
alpha = 1e-5
# yd = 10*x[:, 0]*(1-x[:, 0])*x[:, 1]*(1-x[:, 1])
# three variables
# state y = f[0]
# control u = f[1]
# adjoint p = f[2]
# the three variables
def adjoint_eq(x, f):
grad_x, grad_y = Problem.grad(f['p'], x)[:, :2].T
grad_xx = Problem.grad(grad_x, x)[:, 0]
grad_yy = Problem.grad(grad_y, x)[:, 1]
return - grad_xx - grad_yy - f['y'] + 1*(x[:, 0] <= 1) + x[:, 2]*(x[:, 0] > 1)
def control_eq(x, f):
return alpha*f['u'] - f['p']
def state_eq(x, f):
grad_x, grad_y = Problem.grad(f['y'], x)[:, :2].T
grad_xx = Problem.grad(grad_x, x)[:, 0]
grad_yy = Problem.grad(grad_y, x)[:, 1]
return - grad_xx - grad_yy - f['u']
def equation(x, f):
return state_eq(x, f) + control_eq(x, f) + adjoint_eq(x, f)
laplace = ParametricProblem2D(
variables=['y', 'u', 'p'],
bc=bc,
domain_bound=np.array([[0, 4],[0, 1]]),
params_bound=np.array([[0.5, 2.5]]))
laplace.equation = equation

View File

@@ -0,0 +1,29 @@
import numpy as np
import torch
from pina.problem import Problem
from pina.segment import Segment
from pina.parametricproblem2d import ParametricProblem2D
bc = (
(Segment((-1, -1), ( 1, -1)), lambda x: torch.zeros(x.shape[0], 1)),
(Segment(( 1, -1), ( 1, 1)), lambda x: torch.zeros(x.shape[0], 1)),
(Segment(( 1, 1), (-1, 1)), lambda x: torch.zeros(x.shape[0], 1)),
(Segment((-1, 1), (-1, -1)), lambda x: torch.zeros(x.shape[0], 1)),
)
params_domain = np.array([
[-1.0, 1.0],
[-1.0, 1.0]])
def equation(x, fx):
grad_x, grad_y = Problem.grad(fx, x)[:, :2].T
grad_xx = Problem.grad(grad_x, x)[:, 0]
grad_yy = Problem.grad(grad_y, x)[:, 1]
a = grad_xx + grad_yy - torch.exp(- 2*(x[:, 0] - x[:, 2])**2 - 2*(x[:, 1] - x[:, 3])**2)
return a
laplace = ParametricProblem2D(bc=bc, domain_bound=params_domain, params_bound=params_domain)
laplace.equation = equation

View File

@@ -0,0 +1,53 @@
import numpy as np
import torch
from pina.problem import Problem
from pina.segment import Segment
from pina.cube import Cube
from pina.problem2d import Problem2D
xmin, xmax, ymin, ymax = -1, 1, -1, 1
class ParametricEllipticOptimalControl(Problem2D):
def __init__(self, alpha=1):
def term1(input_, param_, output_):
grad_p = self.grad(output_['p'], input_)
gradgrad_p_x1 = self.grad(grad_p['x1'], input_)
gradgrad_p_x2 = self.grad(grad_p['x2'], input_)
return output_['y'] - param_ - (gradgrad_p_x1['x1'] + gradgrad_p_x2['x2'])
def term2(input_, param_, output_):
grad_y = self.grad(output_['y'], input_)
gradgrad_y_x1 = self.grad(grad_y['x1'], input_)
gradgrad_y_x2 = self.grad(grad_y['x2'], input_)
return - (gradgrad_y_x1['x1'] + gradgrad_y_x2['x2']) - output_['u_param']
def term3(input_, param_, output_):
return output_['p'] - output_['u_param']*alpha
def term(input_, param_, output_):
return term1( input_, param_, output_) +term2( input_, param_, output_) + term3( input_, param_, output_)
def nil_dirichlet(input_, param_, output_):
y_value = 0.0
p_value = 0.0
return torch.abs(output_['y'] - y_value) + torch.abs(output_['p'] - p_value)
self.conditions = {
'gamma1': {'location': Segment((xmin, ymin), (xmax, ymin)), 'func': nil_dirichlet},
'gamma2': {'location': Segment((xmax, ymin), (xmax, ymax)), 'func': nil_dirichlet},
'gamma3': {'location': Segment((xmax, ymax), (xmin, ymax)), 'func': nil_dirichlet},
'gamma4': {'location': Segment((xmin, ymax), (xmin, ymin)), 'func': nil_dirichlet},
'D1': {'location': Cube([[xmin, xmax], [ymin, ymax]]), 'func': term},
#'D2': {'location': Cube([[0, 1], [0, 1]]), 'func': term2},
#'D3': {'location': Cube([[0, 1], [0, 1]]), 'func': term3}
}
self.input_variables = ['x1', 'x2']
self.output_variables = ['u', 'p', 'y']
self.parameters = ['mu']
self.spatial_domain = Cube([[xmin, xmax], [xmin, xmax]])
self.parameter_domain = np.array([[0.5, 3]])

View File

@@ -0,0 +1,52 @@
import numpy as np
import torch
from pina.problem import Problem
from pina.segment import Segment
from pina.cube import Cube
from pina.problem2d import Problem2D
xmin, xmax, ymin, ymax = -1, 1, -1, 1
class ParametricEllipticOptimalControl(Problem2D):
def __init__(self, alpha=1):
def term1(input_, param_, output_):
grad_p = self.grad(output_['p'], input_)
gradgrad_p_x1 = self.grad(grad_p['x1'], input_)
gradgrad_p_x2 = self.grad(grad_p['x2'], input_)
#print('mu', input_['mu'])
return output_['y'] - input_['mu'] - (gradgrad_p_x1['x1'] + gradgrad_p_x2['x2'])
def term2(input_, param_, output_):
grad_y = self.grad(output_['y'], input_)
gradgrad_y_x1 = self.grad(grad_y['x1'], input_)
gradgrad_y_x2 = self.grad(grad_y['x2'], input_)
return - (gradgrad_y_x1['x1'] + gradgrad_y_x2['x2']) - output_['u_param']
def term3(input_, param_, output_):
#print('a', input_['alpha'], output_['p'], output_['u_param'])
return output_['p'] - output_['u_param']*input_['alpha']
def nil_dirichlet(input_, param_, output_):
y_value = 0.0
p_value = 0.0
return torch.abs(output_['y'] - y_value) + torch.abs(output_['p'] - p_value)
self.conditions = {
'gamma1': {'location': Segment((xmin, ymin), (xmax, ymin)), 'func': nil_dirichlet},
'gamma2': {'location': Segment((xmax, ymin), (xmax, ymax)), 'func': nil_dirichlet},
'gamma3': {'location': Segment((xmax, ymax), (xmin, ymax)), 'func': nil_dirichlet},
'gamma4': {'location': Segment((xmin, ymax), (xmin, ymin)), 'func': nil_dirichlet},
'D1': {'location': Cube([[xmin, xmax], [ymin, ymax]]), 'func': [term1, term2]},
#'D2': {'location': Cube([[0, 1], [0, 1]]), 'func': term2},
#'D3': {'location': Cube([[0, 1], [0, 1]]), 'func': term3}
}
self.input_variables = ['x1', 'x2']
self.output_variables = ['u', 'p', 'y']
self.parameters = ['mu', 'alpha']
self.spatial_domain = Cube([[xmin, xmax], [xmin, xmax]])
self.parameter_domain = np.array([[0.5, 3], [0.0001, 1]])

View File

@@ -0,0 +1,43 @@
import numpy as np
import torch
from pina.segment import Segment
from pina.cube import Cube
from pina.problem2d import Problem2D
from pina.problem import Problem
class ParametricPoisson2DProblem(Problem2D):
def __init__(self):
def laplace_equation(input_, param_, output_):
grad_u = self.grad(output_['u'], input_)
gradgrad_u_x = self.grad(grad_u['x'], input_)
gradgrad_u_y = self.grad(grad_u['y'], input_)
force_term = torch.exp(
- 2*(input_['x'] - input_['mu1'])**2 -
2*(input_['y'] - input_['mu2'])**2
)
return gradgrad_u_x['x'] + gradgrad_u_y['y'] - force_term
def nil_dirichlet(input_, param_, output_):
value = 0.0
return output_['u'] - value
self.conditions = {
'gamma1': {'location': Segment((-1, -1), ( 1, -1)),'func': nil_dirichlet},
'gamma2': {'location': Segment(( 1, -1), ( 1, 1)),'func': nil_dirichlet},
'gamma3': {'location': Segment(( 1, 1), (-1, 1)),'func': nil_dirichlet},
'gamma4': {'location': Segment((-1, 1), (-1, -1)),'func': nil_dirichlet},
'D': {'location': Cube([[-1, 1], [-1, 1]]), 'func': laplace_equation}
}
self.input_variables = ['x', 'y']
self.output_variables = ['u']
self.parameters = ['mu1', 'mu2']
#self.truth_solution = poisson_sol
self.spatial_domain = Cube([[0, 1], [0, 1]])
self.parameter_domain = np.array([[-1, 1], [-1, 1]])
#self.check() # Check the problem is correctly set

41
problems/poisson2D.py Normal file
View File

@@ -0,0 +1,41 @@
import numpy as np
import torch
from pina.segment import Segment
from pina.cube import Cube
from pina.problem2d import Problem2D
from pina.problem import Problem
class Poisson2DProblem(Problem2D):
def __init__(self):
def laplace_equation(input_, output_):
grad_u = self.grad(output_['u'], input_)
gradgrad_u_x = self.grad(grad_u['x'], input_)
gradgrad_u_y = self.grad(grad_u['y'], input_)
force_term = (torch.sin(input_['x']*torch.pi)
* torch.sin(input_['y']*torch.pi))
return gradgrad_u_x['x'] + gradgrad_u_y['y'] - force_term
def nil_dirichlet(input_, output_):
value = 0.0
return output_['u'] - value
self.conditions = {
'gamma1': {'location': Segment((0, 0), (1, 0)), 'func': nil_dirichlet},
'gamma2': {'location': Segment((1, 0), (1, 1)), 'func': nil_dirichlet},
'gamma3': {'location': Segment((1, 1), (0, 1)), 'func': nil_dirichlet},
'gamma4': {'location': Segment((0, 1), (0, 0)), 'func': nil_dirichlet},
'D': {'location': Cube([[0, 1], [0, 1]]), 'func': laplace_equation}
}
def poisson_sol(x, y):
return -(np.sin(x*np.pi)*np.sin(y*np.pi))/(2*np.pi**2)
self.input_variables = ['x', 'y']
self.output_variables = ['u']
self.truth_solution = poisson_sol
self.spatial_domain = Cube([[0, 1], [0, 1]])
#self.check() # Check the problem is correctly set

43
problems/poisson_2.py Normal file
View File

@@ -0,0 +1,43 @@
import numpy as np
import torch
from pina.segment import Segment
from pina.cube import Cube
from pina.problem2d import Problem2D
from pina.problem import Problem
class Poisson2DProblem(Problem2D):
def __init__(self):
def laplace_equation(input_, output_):
grad_u = self.grad(output_['u'], input_)
gradgrad_u_x = self.grad(grad_u['x'], input_)
gradgrad_u_y = self.grad(grad_u['y'], input_)
#force_term = (torch.sin(input_['x']*torch.pi)
# * torch.sin(input_['y']*torch.pi))
force_term = -2*(input_['y']*(1-input_['y']) +
input_['x']*(1-input_['x']))
return gradgrad_u_x['x'] + gradgrad_u_y['y'] - force_term
def nil_dirichlet(input_, output_):
value = 0.0
return output_['u'] - value
self.conditions = {
'gamma1': {'location': Segment((0, 0), (1, 0)), 'func': nil_dirichlet},
'gamma2': {'location': Segment((1, 0), (1, 1)), 'func': nil_dirichlet},
'gamma3': {'location': Segment((1, 1), (0, 1)), 'func': nil_dirichlet},
'gamma4': {'location': Segment((0, 1), (0, 0)), 'func': nil_dirichlet},
'D': {'location': Cube([[0, 1], [0, 1]]), 'func': laplace_equation}
}
def poisson_sol(x, y):
return x*(1-x)*y*(1-y)
self.input_variables = ['x', 'y']
self.output_variables = ['u']
self.truth_solution = poisson_sol
self.spatial_domain = Cube([[0, 1], [0, 1]])
#self.check() # Check the problem is correctly set