PINA icon indicating copy to clipboard operation
PINA copied to clipboard

Input points help needed

Open Asseni opened this issue 1 year ago • 1 comments

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()

Asseni avatar Jul 17 '24 07:07 Asseni

Hi @Asseni 👋🏻

The code was almost fine but there were a few issues.

  1. The Trainer is not finding the point because you are passing the PINN solver an instance of the problem, not the problem variable. The code should be solver = PINN(problem=problem, model=model) instead of solver = PINN(problem=SimpleODE(), model=model). This is because when you call SimpleODE() python creates an object (inherited from AbstractProblem) different than the problem you defined before for which you discretized the domain.
  2. The equations in the SimpleODE class 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()

dario-coscia avatar Jul 18 '24 09:07 dario-coscia

Hi @Asseni! Do you still have problem with this? Otherwise I will close the issue😄

dario-coscia avatar Aug 07 '24 06:08 dario-coscia