Skip to content

Commit eb53174

Browse files
Dario Cosciandem0
authored andcommitted
update examples
1 parent 37e9658 commit eb53174

11 files changed

+370
-255
lines changed

examples/problems/burgers.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,26 @@
55
from pina import Condition
66
from pina.span import Span
77

8+
# ===================================================== #
9+
# #
10+
# This script implements the one dimensional Burger #
11+
# problem. The Burgers1D class is defined inheriting #
12+
# from TimeDependentProblem, SpatialProblem and we #
13+
# denote: #
14+
# u --> field variable #
15+
# x --> spatial variable #
16+
# t --> temporal variable #
17+
# #
18+
# ===================================================== #
819

920
class Burgers1D(TimeDependentProblem, SpatialProblem):
1021

22+
# assign output/ spatial and temporal variables
1123
output_variables = ['u']
1224
spatial_domain = Span({'x': [-1, 1]})
1325
temporal_domain = Span({'t': [0, 1]})
1426

27+
# define the burger equation
1528
def burger_equation(input_, output_):
1629
du = grad(output_, input_)
1730
ddu = grad(du, input_, components=['dudx'])
@@ -21,17 +34,20 @@ def burger_equation(input_, output_):
2134
(0.01/torch.pi)*ddu.extract(['ddudxdx'])
2235
)
2336

37+
# define nill dirichlet boundary conditions
2438
def nil_dirichlet(input_, output_):
2539
u_expected = 0.0
2640
return output_.extract(['u']) - u_expected
2741

42+
# define initial condition
2843
def initial_condition(input_, output_):
2944
u_expected = -torch.sin(torch.pi*input_.extract(['x']))
3045
return output_.extract(['u']) - u_expected
3146

47+
# problem condition statement
3248
conditions = {
33-
'gamma1': Condition(Span({'x': -1, 't': [0, 1]}), nil_dirichlet),
34-
'gamma2': Condition(Span({'x': 1, 't': [0, 1]}), nil_dirichlet),
35-
't0': Condition(Span({'x': [-1, 1], 't': 0}), initial_condition),
36-
'D': Condition(Span({'x': [-1, 1], 't': [0, 1]}), burger_equation),
49+
'gamma1': Condition(location=Span({'x': -1, 't': [0, 1]}), function=nil_dirichlet),
50+
'gamma2': Condition(location=Span({'x': 1, 't': [0, 1]}), function=nil_dirichlet),
51+
't0': Condition(location=Span({'x': [-1, 1], 't': 0}), function=initial_condition),
52+
'D': Condition(location=Span({'x': [-1, 1], 't': [0, 1]}), function=burger_equation),
3753
}
Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
1-
import torch
2-
from pina.problem import Problem
3-
from pina.segment import Segment
4-
from pina.cube import Cube
5-
from pina.problem2d import Problem2D
6-
7-
xmin, xmax, ymin, ymax = -1, 1, -1, 1
8-
9-
class EllipticOptimalControl(Problem2D):
10-
11-
def __init__(self, alpha=1):
12-
13-
def term1(input_, output_):
14-
grad_p = self.grad(output_.extract(['p']), input_)
15-
gradgrad_p_x1 = self.grad(grad_p.extract(['x1']), input_)
16-
gradgrad_p_x2 = self.grad(grad_p.extract(['x2']), input_)
17-
yd = 2.0
18-
return output_.extract(['y']) - yd - (gradgrad_p_x1.extract(['x1']) + gradgrad_p_x2.extract(['x2']))
19-
20-
def term2(input_, output_):
21-
grad_y = self.grad(output_.extract(['y']), input_)
22-
gradgrad_y_x1 = self.grad(grad_y.extract(['x1']), input_)
23-
gradgrad_y_x2 = self.grad(grad_y.extract(['x2']), input_)
24-
return - (gradgrad_y_x1.extract(['x1']) + gradgrad_y_x2.extract(['x2'])) - output_.extract(['u'])
25-
26-
def term3(input_, output_):
27-
return output_.extract(['p']) - output_.extract(['u'])*alpha
28-
29-
30-
def nil_dirichlet(input_, output_):
31-
y_value = 0.0
32-
p_value = 0.0
33-
return torch.abs(output_.extract(['y']) - y_value) + torch.abs(output_.extract(['p']) - p_value)
34-
35-
self.conditions = {
36-
'gamma1': {'location': Segment((xmin, ymin), (xmax, ymin)), 'func': nil_dirichlet},
37-
'gamma2': {'location': Segment((xmax, ymin), (xmax, ymax)), 'func': nil_dirichlet},
38-
'gamma3': {'location': Segment((xmax, ymax), (xmin, ymax)), 'func': nil_dirichlet},
39-
'gamma4': {'location': Segment((xmin, ymax), (xmin, ymin)), 'func': nil_dirichlet},
40-
'D1': {'location': Cube([[xmin, xmax], [ymin, ymax]]), 'func': [term1, term2, term3]},
41-
}
42-
43-
self.input_variables = ['x1', 'x2']
44-
self.output_variables = ['u', 'p', 'y']
45-
self.spatial_domain = Cube([[xmin, xmax], [xmin, xmax]])
1+
# import torch
2+
# from pina.problem import Problem
3+
# from pina.segment import Segment
4+
# from pina.cube import Cube
5+
# from pina.problem2d import Problem2D
6+
7+
# xmin, xmax, ymin, ymax = -1, 1, -1, 1
8+
9+
# class EllipticOptimalControl(Problem2D):
10+
11+
# def __init__(self, alpha=1):
12+
13+
# def term1(input_, output_):
14+
# grad_p = self.grad(output_.extract(['p']), input_)
15+
# gradgrad_p_x1 = self.grad(grad_p.extract(['x1']), input_)
16+
# gradgrad_p_x2 = self.grad(grad_p.extract(['x2']), input_)
17+
# yd = 2.0
18+
# return output_.extract(['y']) - yd - (gradgrad_p_x1.extract(['x1']) + gradgrad_p_x2.extract(['x2']))
19+
20+
# def term2(input_, output_):
21+
# grad_y = self.grad(output_.extract(['y']), input_)
22+
# gradgrad_y_x1 = self.grad(grad_y.extract(['x1']), input_)
23+
# gradgrad_y_x2 = self.grad(grad_y.extract(['x2']), input_)
24+
# return - (gradgrad_y_x1.extract(['x1']) + gradgrad_y_x2.extract(['x2'])) - output_.extract(['u'])
25+
26+
# def term3(input_, output_):
27+
# return output_.extract(['p']) - output_.extract(['u'])*alpha
28+
29+
30+
# def nil_dirichlet(input_, output_):
31+
# y_value = 0.0
32+
# p_value = 0.0
33+
# return torch.abs(output_.extract(['y']) - y_value) + torch.abs(output_.extract(['p']) - p_value)
34+
35+
# self.conditions = {
36+
# 'gamma1': {'location': Segment((xmin, ymin), (xmax, ymin)), 'func': nil_dirichlet},
37+
# 'gamma2': {'location': Segment((xmax, ymin), (xmax, ymax)), 'func': nil_dirichlet},
38+
# 'gamma3': {'location': Segment((xmax, ymax), (xmin, ymax)), 'func': nil_dirichlet},
39+
# 'gamma4': {'location': Segment((xmin, ymax), (xmin, ymin)), 'func': nil_dirichlet},
40+
# 'D1': {'location': Cube([[xmin, xmax], [ymin, ymax]]), 'func': [term1, term2, term3]},
41+
# }
42+
43+
# self.input_variables = ['x1', 'x2']
44+
# self.output_variables = ['u', 'p', 'y']
45+
# self.spatial_domain = Cube([[xmin, xmax], [xmin, xmax]])
46+
47+
raise NotImplementedError('not available problem at the moment...')

examples/problems/first_order_ode.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from pina.problem import SpatialProblem
2+
from pina import Condition, Span
3+
from pina.operators import grad
4+
import torch
5+
6+
# ===================================================== #
7+
# #
8+
# This script implements a simple first order ode. #
9+
# The FirstOrderODE class is defined inheriting from #
10+
# SpatialProblem. We denote: #
11+
# y --> field variable #
12+
# x --> spatial variable #
13+
# #
14+
# ===================================================== #
15+
16+
class FirstOrderODE(SpatialProblem):
17+
18+
# variable domain range
19+
x_rng = [0, 5]
20+
# field variable
21+
output_variables = ['y']
22+
# create domain
23+
spatial_domain = Span({'x': x_rng})
24+
25+
# define the ode
26+
def ode(input_, output_):
27+
y = output_
28+
x = input_
29+
return grad(y, x) + y - x
30+
31+
# define initial conditions
32+
def fixed(input_, output_):
33+
exp_value = 1.
34+
return output_ - exp_value
35+
36+
# define real solution
37+
def solution(self, input_):
38+
x = input_
39+
return x - 1.0 + 2*torch.exp(-x)
40+
41+
# define problem conditions
42+
conditions = {
43+
'bc': Condition(location=Span({'x': x_rng[0]}), function=fixed),
44+
'dd': Condition(location=Span({'x': x_rng}), function=ode),
45+
}
46+
truth_solution = solution
Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
import numpy as np
2-
import torch
3-
from pina.problem import Problem
4-
from pina.segment import Segment
5-
from pina.cube import Cube
6-
from pina.problem2d import Problem2D
7-
8-
xmin, xmax, ymin, ymax = -1, 1, -1, 1
9-
10-
class ParametricEllipticOptimalControl(Problem2D):
11-
12-
def __init__(self, alpha=1):
13-
14-
def term1(input_, param_, output_):
15-
grad_p = self.grad(output_['p'], input_)
16-
gradgrad_p_x1 = self.grad(grad_p['x1'], input_)
17-
gradgrad_p_x2 = self.grad(grad_p['x2'], input_)
18-
return output_['y'] - param_ - (gradgrad_p_x1['x1'] + gradgrad_p_x2['x2'])
19-
20-
def term2(input_, param_, output_):
21-
grad_y = self.grad(output_['y'], input_)
22-
gradgrad_y_x1 = self.grad(grad_y['x1'], input_)
23-
gradgrad_y_x2 = self.grad(grad_y['x2'], input_)
24-
return - (gradgrad_y_x1['x1'] + gradgrad_y_x2['x2']) - output_['u_param']
25-
26-
def term3(input_, param_, output_):
27-
return output_['p'] - output_['u_param']*alpha
28-
29-
30-
def term(input_, param_, output_):
31-
return term1( input_, param_, output_) +term2( input_, param_, output_) + term3( input_, param_, output_)
32-
33-
def nil_dirichlet(input_, param_, output_):
34-
y_value = 0.0
35-
p_value = 0.0
36-
return torch.abs(output_['y'] - y_value) + torch.abs(output_['p'] - p_value)
37-
38-
self.conditions = {
39-
'gamma1': {'location': Segment((xmin, ymin), (xmax, ymin)), 'func': nil_dirichlet},
40-
'gamma2': {'location': Segment((xmax, ymin), (xmax, ymax)), 'func': nil_dirichlet},
41-
'gamma3': {'location': Segment((xmax, ymax), (xmin, ymax)), 'func': nil_dirichlet},
42-
'gamma4': {'location': Segment((xmin, ymax), (xmin, ymin)), 'func': nil_dirichlet},
43-
'D1': {'location': Cube([[xmin, xmax], [ymin, ymax]]), 'func': term},
44-
#'D2': {'location': Cube([[0, 1], [0, 1]]), 'func': term2},
45-
#'D3': {'location': Cube([[0, 1], [0, 1]]), 'func': term3}
46-
}
47-
48-
self.input_variables = ['x1', 'x2']
49-
self.output_variables = ['u', 'p', 'y']
50-
self.parameters = ['mu']
51-
self.spatial_domain = Cube([[xmin, xmax], [xmin, xmax]])
52-
self.parameter_domain = np.array([[0.5, 3]])
53-
1+
# import numpy as np
2+
# import torch
3+
# from pina.problem import Problem
4+
# from pina.segment import Segment
5+
# from pina.cube import Cube
6+
# from pina.problem2d import Problem2D
7+
8+
# xmin, xmax, ymin, ymax = -1, 1, -1, 1
9+
10+
# class ParametricEllipticOptimalControl(Problem2D):
11+
12+
# def __init__(self, alpha=1):
13+
14+
# def term1(input_, param_, output_):
15+
# grad_p = self.grad(output_['p'], input_)
16+
# gradgrad_p_x1 = self.grad(grad_p['x1'], input_)
17+
# gradgrad_p_x2 = self.grad(grad_p['x2'], input_)
18+
# return output_['y'] - param_ - (gradgrad_p_x1['x1'] + gradgrad_p_x2['x2'])
19+
20+
# def term2(input_, param_, output_):
21+
# grad_y = self.grad(output_['y'], input_)
22+
# gradgrad_y_x1 = self.grad(grad_y['x1'], input_)
23+
# gradgrad_y_x2 = self.grad(grad_y['x2'], input_)
24+
# return - (gradgrad_y_x1['x1'] + gradgrad_y_x2['x2']) - output_['u_param']
25+
26+
# def term3(input_, param_, output_):
27+
# return output_['p'] - output_['u_param']*alpha
28+
29+
30+
# def term(input_, param_, output_):
31+
# return term1( input_, param_, output_) +term2( input_, param_, output_) + term3( input_, param_, output_)
32+
33+
# def nil_dirichlet(input_, param_, output_):
34+
# y_value = 0.0
35+
# p_value = 0.0
36+
# return torch.abs(output_['y'] - y_value) + torch.abs(output_['p'] - p_value)
37+
38+
# self.conditions = {
39+
# 'gamma1': {'location': Segment((xmin, ymin), (xmax, ymin)), 'func': nil_dirichlet},
40+
# 'gamma2': {'location': Segment((xmax, ymin), (xmax, ymax)), 'func': nil_dirichlet},
41+
# 'gamma3': {'location': Segment((xmax, ymax), (xmin, ymax)), 'func': nil_dirichlet},
42+
# 'gamma4': {'location': Segment((xmin, ymax), (xmin, ymin)), 'func': nil_dirichlet},
43+
# 'D1': {'location': Cube([[xmin, xmax], [ymin, ymax]]), 'func': term},
44+
# #'D2': {'location': Cube([[0, 1], [0, 1]]), 'func': term2},
45+
# #'D3': {'location': Cube([[0, 1], [0, 1]]), 'func': term3}
46+
# }
47+
48+
# self.input_variables = ['x1', 'x2']
49+
# self.output_variables = ['u', 'p', 'y']
50+
# self.parameters = ['mu']
51+
# self.spatial_domain = Cube([[xmin, xmax], [xmin, xmax]])
52+
# self.parameter_domain = np.array([[0.5, 3]])
53+
raise NotImplementedError('not available problem at the moment...')

examples/problems/parametric_elliptic_optimal_control_alpha_variable.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,42 @@
55
from pina.problem import SpatialProblem, ParametricProblem
66
from pina.operators import grad, nabla
77

8+
# ===================================================== #
9+
# #
10+
# This script implements the two dimensional #
11+
# Parametric Elliptic Optimal Control problem. #
12+
# The ParametricEllipticOptimalControl class is #
13+
# inherited from TimeDependentProblem, SpatialProblem #
14+
# and we denote: #
15+
# u --> field variable #
16+
# p --> field variable #
17+
# y --> field variable #
18+
# x1, x2 --> spatial variables #
19+
# mu, alpha --> problem parameters #
20+
# #
21+
# More info in https://arxiv.org/pdf/2110.13530.pdf #
22+
# Section 4.2 of the article #
23+
# ===================================================== #
24+
825

926
class ParametricEllipticOptimalControl(SpatialProblem, ParametricProblem):
1027

28+
# setting spatial variables ranges
1129
xmin, xmax, ymin, ymax = -1, 1, -1, 1
30+
x_range = [xmin, xmax]
31+
y_range = [ymin, ymax]
32+
# setting parameters range
1233
amin, amax = 0.0001, 1
1334
mumin, mumax = 0.5, 3
1435
mu_range = [mumin, mumax]
1536
a_range = [amin, amax]
16-
x_range = [xmin, xmax]
17-
y_range = [ymin, ymax]
18-
37+
# setting field variables
1938
output_variables = ['u', 'p', 'y']
39+
# setting spatial and parameter domain
2040
spatial_domain = Span({'x1': x_range, 'x2': y_range})
2141
parameter_domain = Span({'mu': mu_range, 'alpha': a_range})
2242

23-
43+
# equation terms as in https://arxiv.org/pdf/2110.13530.pdf
2444
def term1(input_, output_):
2545
laplace_p = nabla(output_, input_, components=['p'], d=['x1', 'x2'])
2646
return output_.extract(['y']) - input_.extract(['mu']) - laplace_p
@@ -37,21 +57,22 @@ def adj_dirichlet(input_, output_):
3757
p_exp = 0.0
3858
return output_.extract(['p']) - p_exp
3959

60+
# setting problem condition formulation
4061
conditions = {
4162
'gamma1': Condition(
42-
Span({'x1': x_range, 'x2': 1, 'mu': mu_range, 'alpha': a_range}),
43-
[state_dirichlet, adj_dirichlet]),
63+
location=Span({'x1': x_range, 'x2': 1, 'mu': mu_range, 'alpha': a_range}),
64+
function=[state_dirichlet, adj_dirichlet]),
4465
'gamma2': Condition(
45-
Span({'x1': x_range, 'x2': -1, 'mu': mu_range, 'alpha': a_range}),
46-
[state_dirichlet, adj_dirichlet]),
66+
location=Span({'x1': x_range, 'x2': -1, 'mu': mu_range, 'alpha': a_range}),
67+
function=[state_dirichlet, adj_dirichlet]),
4768
'gamma3': Condition(
48-
Span({'x1': 1, 'x2': y_range, 'mu': mu_range, 'alpha': a_range}),
49-
[state_dirichlet, adj_dirichlet]),
69+
location=Span({'x1': 1, 'x2': y_range, 'mu': mu_range, 'alpha': a_range}),
70+
function=[state_dirichlet, adj_dirichlet]),
5071
'gamma4': Condition(
51-
Span({'x1': -1, 'x2': y_range, 'mu': mu_range, 'alpha': a_range}),
52-
[state_dirichlet, adj_dirichlet]),
72+
location=Span({'x1': -1, 'x2': y_range, 'mu': mu_range, 'alpha': a_range}),
73+
function=[state_dirichlet, adj_dirichlet]),
5374
'D': Condition(
54-
Span({'x1': x_range, 'x2': y_range,
75+
location=Span({'x1': x_range, 'x2': y_range,
5576
'mu': mu_range, 'alpha': a_range}),
56-
[term1, term2]),
57-
}
77+
function=[term1, term2]),
78+
}

0 commit comments

Comments
 (0)