Giter Site home page Giter Site logo

Input points help needed about pina HOT 2 OPEN

Asseni avatar Asseni commented on August 17, 2024
Input points help needed

from pina.

Comments (2)

dario-coscia avatar dario-coscia commented on August 17, 2024

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

from pina.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.