Comments (2)
Yes, it should be sufficient to create a new problem by inheriting by those classes. Like
class MyProblem(TimeDependentProblem, SpatialProblem, ParametricProblem):
...
Just remember to specify also the variable domains!
from pina.
""" Burgers' problem. """
# ===================================================== #
# #
# This script implements the one dimensional Burger #
# problem. The Burgers1D class is defined inheriting #
# from TimeDependentProblem, SpatialProblem and we #
# denote: #
# u --> field variable #
# x --> spatial variable #
# t --> temporal variable #
# #
# ===================================================== #
import torch
from pina.geometry import CartesianDomain
from pina import Condition
from pina.problem import TimeDependentProblem, SpatialProblem, ParametricProblem
from pina.operators import grad
from pina.equation import FixedValue, Equation
class Burgers1D(TimeDependentProblem, SpatialProblem, ParametricProblem):
# define the burger equation
def burger_equation(input_, output_):
du = grad(output_, input_)
ddu = grad(du, input_, components=['dudx'])
mu1 = input_.extract(['mu1'])
return (
du.extract(['dudt']) +
output_.extract(['u']) * du.extract(['dudx']) -
(mu1 / torch.pi) * ddu.extract(['ddudxdx'])
)
# define initial condition
def initial_condition(input_, output_):
u_expected = -torch.sin(torch.pi * input_.extract(['x']))
return output_.extract(['u']) - u_expected
# assign output/ spatial and temporal variables
output_variables = ['u']
spatial_domain = CartesianDomain({'x': [-1, 1]})
temporal_domain = CartesianDomain({'t': [0, 1]})
mu1 = CartesianDomain({'mu1': [0.01, 0.02]})
# problem condition statement
conditions = {
'gamma1': Condition(location=CartesianDomain({'x': -1, 't': [0, 1],'mu1':[0.01,0.02]}), equation=FixedValue(0.)),
'gamma2': Condition(location=CartesianDomain({'x': 1, 't': [0, 1],'mu1':[0.01,0.02]}), equation=FixedValue(0.)),
't0': Condition(location=CartesianDomain({'x': [-1, 1], 't': 0,'mu1':[0.01,0.02]}), equation=Equation(initial_condition)),
'D': Condition(location=CartesianDomain({'x': [-1, 1], 't': [0, 1],'mu1':[0.01,0.02]}), equation=Equation(burger_equation)),
}
""" Run PINA on Burgers equation. """
import argparse
import torch
from torch.nn import Softplus
from pina import LabelTensor
from pina.model import FeedForward
from pina.solvers import PINN
from pina.plotter import Plotter
from pina.trainer import Trainer
class myFeature(torch.nn.Module):
"""
Feature: sin(pi*x)
"""
def __init__(self):
super(myFeature, self).__init__()
def forward(self, x):
return LabelTensor(torch.sin(torch.pi * x.extract(['x'])), ['sin(x)'])
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run PINA")
parser.add_argument("--load", help="directory to save or load file", type=str)
parser.add_argument("--features", help="extra features", type=int)
parser.add_argument("--epochs", help="extra features", type=int, default=1000)
args = parser.parse_args()
if args.features is None:
args.features = 0
# extra features
feat = [myFeature()] if args.features else []
# create problem and discretise domain
burgers_problem = Burgers1D()
burgers_problem.discretise_domain(n=200, mode='grid', variables = 't', locations=['D'])
burgers_problem.discretise_domain(n=20, mode='grid', variables = 'x', locations=['D'])
burgers_problem.discretise_domain(n=20, mode='grid', variables = 'mu1', locations=['D'])
burgers_problem.discretise_domain(n=150, mode='random', locations=['gamma1', 'gamma2', 't0'])
# create model
model = FeedForward(
layers=[30, 20, 10, 5],
output_dimensions=len(burgers_problem.output_variables),
input_dimensions=len(burgers_problem.input_variables) + len(feat),
func=Softplus
)
# create solver
pinn = PINN(
problem=burgers_problem,
model=model,
extra_features=feat,
optimizer_kwargs={'lr' : 0.006}
)
# create trainer
directory = 'pina.burger_extrafeats_{}'.format(bool(args.features))
trainer = Trainer(solver=pinn, accelerator='cpu', max_epochs=args.epochs, default_root_dir=directory)
if args.load:
pinn = PINN.load_from_checkpoint(checkpoint_path=args.load, problem=burgers_problem, model=model)
plotter = Plotter()
plotter.plot(pinn)
else:
trainer.train()
from pina.
Related Issues (20)
- Not able to solve simple PDE with flux condition HOT 4
- Tutorial for Equations Class HOT 2
- None
- Using custom arguments in boundary condition HOT 2
- Adaptive Activations Functions
- Tried using spatial, time and parametric domain HOT 3
- Solver Compilation HOT 1
- Tests and Monthly Automated Tag HOT 1
- Unkown parameters in InverseProblem can't go on device HOT 3
- Large-scale experiment HOT 2
- Benchmarks HOT 1
- KS mat generation HOT 3
- Residual-based attention PINN
- `add_points` assigns wrong labels to new data HOT 3
- Input points help needed HOT 2
- Problem in the example of parametric elliptic control problem HOT 1
- Cannot track mean_loss HOT 1
- .cpu() method for LabelTensor HOT 1
- Add tests for MetricTracker
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pina.