Code Monkey home page Code Monkey logo

spring_mass_damper_pinn's Introduction

Physics Informed Neural Net (PINN) for a Spring-Mass-Damper system

This repo demonstrates how a physics informed neural net (PINN) can generalize to out of sample data using a simple 1-D spring-mass-damper system. This repo is based on an example by Ben Moseley So, what is a physics-informed neural network?

Results with PINN


Environment and Packages

This repo was built with python version 3.6.15. The only package outside the standard packages required for this repo are:

  • pytorch version 1.10.2
  • numpy version 1.16.4.
  • matplotlib version 3.3.2
  • sklearn version 0.23.2

The requirements.txt allows the user to create an environment with this package by running the command: python3 -m pip install -r requirements.txt


Spring-Mass-Damper

The spring-mass-damper system is implemented in SpringMassDamper.py as a time-stepped PDE solver. At each time step, the program solves for the spring and damper force and then finds the acceleration, velocity, and position using the following equations.

acceleration(t+1) = acceleration(t) - (spring_force + damper_force) / mass

velocity(t+1) = velocity(t) + acceleration(t+1) * dt

position(t+1) = position(t) + velocity(t+1) * dt

Using the system parameters:

  • Mass = 1 kg
  • Spring coefficient (k) = 2.5 N/m
  • Damper coefficient (c) = 0.3 N/m/s

and the initial conditions:

  • Position(0) = 1 m
  • Velocity(0) = 0 m/s
  • Acceleration(0) = 0 m/s^2

Results in the over damped oscillations below:

Spring-mass-system

Physics Informed Neural Net

The PINN is created by including the physics loss into the overall neural net's loss calculations. Normally, a neural net tries to minimize the difference between it's prediction (y_hat) and the true output (y) using mean squared error (MSE) or a similar metric.

MSE = (y_hat - y)^2

PINNs include additional terms into the loss function which can include the physics loss, boundary condition loss, and initial condition loss. In this example only the physics loss is explicitly calculated. The boundary loss is included in the MSE calculations. The spring-mass-damper is described by the partial differential equation:

acceleration(t) + DAMPING_COEFFICIENT * velocity(t) + SPRING_COEFFICIENT * position(t) = 0

The (auto differentiation) [https://pytorch.org/tutorials/beginner/basics/autogradqs_tutorial.html] function within most neural net packages allows the program to find the velocity (derivative of position) and accelerate (derivative of velocity) fairly easy. Thus, as the neural net is training, it can predict the position for a set of points during the forward pass. We then use auto differentiation to find the velocity and acceleration of those points.

This allows us to use the full loss function:

J = L_MSE + L_physics.

Training Data

To show the benefits of PINNs, the training data is sampled from only half of the full 1-D space. The training data consists of 20 equally-spaced points between 0 and 0.5 seconds.

TrainingData

During the calculation of the physics loss for the PINN, an additional set of points are also calculated using only the forward pass of the neural network. These 500 points span from 0 to 1 second.

Results

A neural net trained with only the MSE loss results in a good in-sample fit, but fails to generalize to new data as seen below.

training_results_noPINN

Using a PINN approach, the neural net is trained not only on the training data, but also on the physics of the problem. This results in a neural net that is able to extrapolate outside of the training data with a high degree of accuracy.

training_results_withPINN


References

Moseley, Ben. "So, what is a physics-informed neural network?" 28 August 2021, So, what is a physics-informed neural network?

spring_mass_damper_pinn's People

Contributors

brianwade1 avatar

Watchers

 avatar

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.