Documentation ¶
Overview ¶
Package odeint implements Ordinary Differential Equations integrators. for initial value problems to be solved by explicit methods. The package features methods for the standard library types,
float32 float64 complex64 complex128
Methods for complex128 ¶
The import to use complex128 is
import "github.com/Daniel-M/odeint/complex128"
The integrator methods implemented so far are,
* Euler
* Mid point
* Runge-Kutta 4
The package is easily extensible to provide other methods, you can follow the template files as reference,
templates/stepper_method.go.t templates/stepper_method_test.go.t
Example ¶
If we want to solve the system using complex128, we must import the adequate subpackage,
import "github.com/Daniel-M/odeint/complex128"
So we begin by defining the system of coupled differential equations, for instance,
func odesys(x []complex128, parameters []complex128) []complex128 { dxdt := make([]complex128, len(x)) dxdt[0] = -1i * x[0] * x[0] * x[1] dxdt[1] = 1i * x[1] return dxdt }
declare the state and parameters variables,
state := make([]complex128, 2) params := make([]complex128, 2)
Putting the inital conditions
state[0] = 1 / 2.1 state[1] = 0.8
And the parameters
params[0] = 2.0 params[1] = 0.0
We create an instance of the system,
system := odeint.NewSystem(state, params, odesys)
And an instance of the integrator with Midpoint method,
var integrator odeint.Midpoint
Set the system to the integrator before integrating the system
err := integrator.Set(2*math.Pi/500, *system) if err != nil { panic(err) }
And finally we integrate within a loop
for i := 0; i <= int(2*math.Pi/real(integrator.StepSize()))+1; i++ { fmt.Println(float64(i)*real(integrator.StepSize()), state) state, err = integrator.Step() if err != nil { panic(err) } }
The code above will print the data columns to the standard output. To write to a file you could create a file with
os.Create
and write to it with
fmt.Fprintf(w,...)
where w implements the interface
io.Writter
There are more examples at the examples path
FAQ ¶
A subpackage for each numeric type?
You might be thinking, why does this guy have a subpackage for each numeric type? Well, though it makes the package harder to maintain, having type specific integrators is a priority for me. I could have used interface-based integrators but it would be at the expense of the extensibility of the integrators to more custom numerical types, a feature which I find relevant too.
License ¶
This code is licensed under MIT license that can be found in the LICENSE file as,
MIT License Copyright (c) 2017-2018 Daniel Mejía Raigosa Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
And at the begining of the source code files as the fragment,
Copyright 2017-2018 Daniel Mejía Raigosa. All rights reserved. Use of this source code is governed by a MIT license that can be found in the LICENSE file.
Index ¶
- type Error
- type Euler
- func (euler *Euler) Set(stepSize complex128, system System) error
- func (euler *Euler) SetState(state []complex128) error
- func (euler *Euler) SetStep(step complex128) error
- func (euler *Euler) State() []complex128
- func (euler *Euler) Step() ([]complex128, error)
- func (euler *Euler) StepSize() complex128
- type Midpoint
- func (midpoint *Midpoint) Set(stepSize complex128, system System) error
- func (midpoint *Midpoint) SetState(state []complex128) error
- func (midpoint *Midpoint) SetStep(step complex128) error
- func (midpoint *Midpoint) State() []complex128
- func (midpoint *Midpoint) Step() ([]complex128, error)
- func (midpoint *Midpoint) StepSize() complex128
- type Rk4
- type Stepper
- type System
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error implements an error custom type.
type Euler ¶
type Euler struct {
// contains filtered or unexported fields
}
Euler implements the Euler stepper method. Euler is part of the Stepper interface.
func NewEuler ¶
func NewEuler(stepSize complex128, system System) (r *Euler)
NewEuler returns a reference to a new Euler stepper method.
func (*Euler) Set ¶
func (euler *Euler) Set(stepSize complex128, system System) error
Set sets the step size and System for the Euler stepper.
func (*Euler) SetState ¶
func (euler *Euler) SetState(state []complex128) error
SetState sets the state for the Euler stepper.
func (*Euler) SetStep ¶
func (euler *Euler) SetStep(step complex128) error
SetStep sets the state for the Euler stepper.
func (*Euler) State ¶
func (euler *Euler) State() []complex128
State returns the state of the Euler stepper.
func (*Euler) Step ¶
func (euler *Euler) Step() ([]complex128, error)
Step performs one step iteration call of the Midpoint stepper. It also updates the state of the Midpoint object.
The mid-point method for the system,
y = f(t, y)
Consists of building the sequence of numbers t_n, y_n, following the recurrence,
t_n+1 = t_n + dt ya_n = y_n + 0.5*dt*f(t_n, y_n) // First step y_n+1 = y_n + dt*f(t_n, ya_n) // Second step
func (*Euler) StepSize ¶
func (euler *Euler) StepSize() complex128
StepSize returns the step size of the method
type Midpoint ¶
type Midpoint struct {
// contains filtered or unexported fields
}
Midpoint implements the Midpoint stepper method. Midpoint is part of the Stepper interface.
func NewMidpoint ¶
func NewMidpoint(stepSize complex128, system System) (r *Midpoint)
NewMidpoint returns a reference to a new Midpoint stepper method.
func (*Midpoint) Set ¶
func (midpoint *Midpoint) Set(stepSize complex128, system System) error
Set sets the step size and System for the Midpoint stepper.
func (*Midpoint) SetState ¶
func (midpoint *Midpoint) SetState(state []complex128) error
SetState sets the state for the Midpoint stepper.
func (*Midpoint) SetStep ¶
func (midpoint *Midpoint) SetStep(step complex128) error
SetStep sets the state for the Midpoint stepper.
func (*Midpoint) State ¶
func (midpoint *Midpoint) State() []complex128
State returns the state of the Midpoint stepper.
func (*Midpoint) Step ¶
func (midpoint *Midpoint) Step() ([]complex128, error)
Step performs one step iteration call of the Midpoint stepper. It also updates the state of the Midpoint object.
The mid-point method for the system,
y = f(t, y)
Consists of building the sequence of numbers t_n, y_n, following the recurrence,
t_n+1 = t_n + dt ya_n = y_n + 0.5*dt*f(t_n, y_n) // First step y_n+1 = y_n + dt*f(t_n, ya_n) // Second step
func (*Midpoint) StepSize ¶
func (midpoint *Midpoint) StepSize() complex128
StepSize returns the step size of the method
type Rk4 ¶
type Rk4 struct {
// contains filtered or unexported fields
}
Rk4 implements the rk4 stepper method. rk4 is part of the Stepper interface.
func NewRk4 ¶
func NewRk4(stepSize complex128, system System) (r *Rk4)
NewRk4 returns a reference to a new rk4 stepper method.
func (*Rk4) Set ¶
func (rk4 *Rk4) Set(stepSize complex128, system System) error
Set sets the step size and System for the Rk4 stepper.
func (*Rk4) SetState ¶
func (rk4 *Rk4) SetState(state []complex128) error
SetState sets the state for the Rk4 stepper.
func (*Rk4) SetStep ¶
func (rk4 *Rk4) SetStep(step complex128) error
SetStep sets the state for the Rk4 stepper.
func (*Rk4) State ¶
func (rk4 *Rk4) State() []complex128
State returns the state of the Rk4 stepper.
func (*Rk4) Step ¶
func (rk4 *Rk4) Step() ([]complex128, error)
Step performs one step iteration call of the Rk4 stepper. It also updates the state of the Rk4 object.
The Runge-Kutta4 method for the system,
y = f(t,y)
Consists of building the sequence of numbers t_n, y_n, following the recurrence,
t_n+1 = t_n + dt k1 = dt*f(t_n, y_n) k2 = dt*f(t_n + 0.5*dt, y_n + 0.5*k1) k3 = dt*f(t_n + 0.5*dt, y_n + 0.5*k2) k4 = dt*f(t_n + dt, y_n + k3) y_n+1 = y_n + (1/6)*(k1 + 2*k2 + 2*k3 + k4) // First step
func (*Rk4) StepSize ¶
func (rk4 *Rk4) StepSize() complex128
StepSize returns the step size of the method
type Stepper ¶
type Stepper interface { // Setter methods SetStep(step complex128) error SetState(state []complex128) error Set(stepSize complex128, system System) error // Getter methods StepSize() complex128 State() []complex128 Step() ([]complex128, error) }
Stepper defines the functions that any Ordinary Differential Equation Integrator Stepper should implement.
type System ¶
type System struct {
// contains filtered or unexported fields
}
System wraps the function that represents the right-hand side of the ordinary differential equations system, its parameters t, and state x Consider the system,
x'(t) = f(x,t) (1)
System wraps around f(x,t) storing also t and x. stateVector is the present state of the system, i.e. the values stored at the components of x. parametersVector are the parameters of to the system (i.e. the t in f(x,t)) function describes the func(state []complex128, parameters []complex128) []complex128 that represents the right hand side of system (1)
func NewSystem ¶
func NewSystem(state []complex128, parameters []complex128, system func(state []complex128, parameters []complex128) []complex128) (s *System)
NewSystem returns a reference to a new System object with the properties given
func (*System) Evaluate ¶
func (s *System) Evaluate(state []complex128) []complex128
Evaluate returns the result of evaluating f(x,t) with x = state. if the size of state is zero, it returns f(x,t) using the internal state x
func (*System) Function ¶
func (s *System) Function() func(state []complex128, parameters []complex128) []complex128
State returns the internal function of the System
func (*System) Parameters ¶
func (s *System) Parameters() []complex128
Parameters returns the internal parameters vector of the System
func (*System) State ¶
func (s *System) State() []complex128
State returns the internal state vector of the System