Input points help needed
Hello, I tried to follow the tutorial with a very simple function just to try it out. I got stopped because of this error message when I run the trainer.
Input points in ['initial_u', 'ode'] training are None. Please sample points in your problem by calling discretise_domain function before train in the provided locations.
I think i have done the discretise_domain and I also do not see any other way in the tutorial. What am I doing wrong?
import torch from pina.problem import TimeDependentProblem from pina.operators import grad from pina.equation import Equation, FixedValue from pina import Condition from pina.geometry import CartesianDomain
class SimpleODE(TimeDependentProblem): output_variables = ['u'] temporal_domain = CartesianDomain({'t': [0, 10]}) # Define the temporal domain
def equations(self, input_, output_):
t = input_['t']
u = output_['u']
# Define the differential equation du/dt = -u
du_dt = grad(u, t)
eq = du_dt + u
return {'ode': eq}
conditions = {
'initial_u': Condition(location=CartesianDomain({'t': [0, 0]}), equation=FixedValue(1.0)), # Initial condition u(0) = 1
'ode': Condition(location=CartesianDomain({'t': [0, 10]}), equation=Equation(equations))
}
problem = SimpleODE()
problem.discretise_domain(n=1, mode='random', locations=['initial_u'])
problem.discretise_domain(n=10, mode='grid', locations=['ode'])
from pina import Plotter
pl = Plotter() pl.plot_samples(problem=problem)
print('Input points:', problem.input_pts) print('Initial condition points:', problem.input_pts['initial_u'].labels) print('Domain points:', problem.input_pts['ode'].labels)
from pina.model import FeedForward from pina.solvers import PINN from pina.trainer import Trainer
model = FeedForward( layers=[20, 20, 20], # Hidden layers configuration func=torch.nn.Tanh, output_dimensions=len(problem.output_variables), input_dimensions=len(problem.input_variables) )
solver = PINN(problem=SimpleODE(), model=model)
trainer = Trainer(solver=solver, max_epochs=50)
trainer.train()
Hi @Asseni 👋🏻
The code was almost fine but there were a few issues.
- The
Traineris not finding the point because you are passing thePINNsolver an instance of the problem, not theproblemvariable. The code should besolver = PINN(problem=problem, model=model)instead ofsolver = PINN(problem=SimpleODE(), model=model). This is because when you callSimpleODE()python creates an object (inherited fromAbstractProblem) different than theproblemyou defined before for which you discretized the domain. - The
equationsin theSimpleODEclass was a bit off, here is how it should be written (I put some comments for the understanding):
def equations(input_, output_): # COMMENT: do not put self here, PINA uses static method for equations
t = input_.extract('t')
u = output_.extract('u')
# Define the differential equation du/dt = -u
du_dt = grad(output_, input_, components=['u'], d=['t']) # COMMENT: grad(u, t) this brakes the grad computation, use components and d for specifying the derivative
eq = du_dt + u
return eq # {'ode': eq} COMMENT: in PINA you need to resturn the residual
conditions = {
'initial_u': Condition(location=CartesianDomain({'t': [0, 0]}), equation=FixedValue(1.0)), # Initial condition u(0) = 1
'ode': Condition(location=CartesianDomain({'t': [0, 10]}), equation=Equation(equations))
}
The rest of the code was very good! Here the complete updated code😄 Let me know if you have further questions!
import torch
from pina.problem import TimeDependentProblem
from pina.operators import grad
from pina.equation import Equation, FixedValue
from pina import Condition
from pina.geometry import CartesianDomain
class SimpleODE(TimeDependentProblem):
output_variables = ['u']
temporal_domain = CartesianDomain({'t': [0, 10]}) # Define the temporal domain
def equations(input_, output_): # COMMENT: do not put self here, we use static method for equations
t = input_.extract('t')
u = output_.extract('u')
# Define the differential equation du/dt = -u
du_dt = grad(output_, input_, components=['u'], d=['t']) # COMMENT: grad(u, t) this brakes the grad computation, use components and d for specifying the derivative
eq = du_dt + u
return eq # {'ode': eq} COMMENT: in PINA you need to resturn the residual
conditions = {
'initial_u': Condition(location=CartesianDomain({'t': [0, 0]}), equation=FixedValue(1.0)), # Initial condition u(0) = 1
'ode': Condition(location=CartesianDomain({'t': [0, 10]}), equation=Equation(equations))
}
problem = SimpleODE()
problem.discretise_domain(n=1, mode='random', locations=['initial_u'])
problem.discretise_domain(n=10, mode='grid', locations=['ode'])
from pina import Plotter
pl = Plotter()
pl.plot_samples(problem=problem)
print('Input points:', problem.input_pts)
print('Initial condition points:', problem.input_pts['initial_u'].labels)
print('Domain points:', problem.input_pts['ode'].labels)
from pina.model import FeedForward
from pina.solvers import PINN
from pina.trainer import Trainer
model = FeedForward(
layers=[20, 20, 20], # Hidden layers configuration
func=torch.nn.Tanh,
output_dimensions=len(problem.output_variables),
input_dimensions=len(problem.input_variables)
)
solver = PINN(problem=problem, model=model)
trainer = Trainer(solver=solver, max_epochs=50)
trainer.train()
Hi @Asseni! Do you still have problem with this? Otherwise I will close the issue😄