| 1 |
# (C) Copyright 2007-2025 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
# System library imports. |
| 12 |
import numpy as np |
| 13 |
|
| 14 |
# Local imports |
| 15 |
from attractors.model.i_model_3d import IModel3d, IModel3dIPlottable2dMixin |
| 16 |
from attractors.model.i_plottable_2d import IPlottable2d |
| 17 |
from scipy import arange, array |
| 18 |
from scipy.integrate import odeint |
| 19 |
|
| 20 |
# Enthought libary imports. |
| 21 |
from traits.api import ( |
| 22 |
Adapter, |
| 23 |
Array, |
| 24 |
cached_property, |
| 25 |
Float, |
| 26 |
HasTraits, |
| 27 |
Instance, |
| 28 |
Property, |
| 29 |
provides, |
| 30 |
register_factory, |
| 31 |
Str, |
| 32 |
) |
| 33 |
from traitsui.api import Item, View |
| 34 |
|
| 35 |
|
| 36 |
@provides(IModel3d) |
| 37 |
class Lorenz(HasTraits): |
| 38 |
"""The model object for the Lorenz attractor.""" |
| 39 |
|
| 40 |
#### 'IModel3d' interface ################################################# |
| 41 |
|
| 42 |
name = Str("Lorenz Attractor") |
| 43 |
points = Property( |
| 44 |
Array, observe="prandtl, rayleigh, beta, initial_point, times" |
| 45 |
) |
| 46 |
|
| 47 |
#### 'Lorenz' interface ################################################### |
| 48 |
|
| 49 |
# Equation parameters. |
| 50 |
prandtl = Float(10.0, auto_set=False, enter_set=True) |
| 51 |
rayleigh = Float(28.0, auto_set=False, enter_set=True) |
| 52 |
beta = Float(8.0 / 3.0, auto_set=False, enter_set=True) |
| 53 |
|
| 54 |
# Integration parameters. |
| 55 |
initial_point = Array(dtype=np.float64, value=[0.0, 1.0, 0.0]) |
| 56 |
time_start = Float(0.0) |
| 57 |
time_stop = Float(100.0) |
| 58 |
time_step = Float(0.01) |
| 59 |
times = Property(Array, observe="time_start, time_stop, time_step") |
| 60 |
|
| 61 |
# Configuration view. |
| 62 |
view = View( |
| 63 |
Item("prandtl"), |
| 64 |
Item("rayleigh"), |
| 65 |
Item("beta"), |
| 66 |
Item("initial_point"), |
| 67 |
Item("time_start"), |
| 68 |
Item("time_stop"), |
| 69 |
Item("time_step"), |
| 70 |
resizable=True, |
| 71 |
) |
| 72 |
|
| 73 |
########################################################################### |
| 74 |
# 'Lorenz' interface. |
| 75 |
########################################################################### |
| 76 |
|
| 77 |
def compute_step(self, point, time): |
| 78 |
x, y, z = point |
| 79 |
return array( |
| 80 |
[ |
| 81 |
self.prandtl * (y - x), |
| 82 |
x * (self.rayleigh - z) - y, |
| 83 |
x * y - self.beta * z, |
| 84 |
] |
| 85 |
) |
| 86 |
|
| 87 |
########################################################################### |
| 88 |
# Protected interface. |
| 89 |
########################################################################### |
| 90 |
|
| 91 |
@cached_property |
| 92 |
def _get_points(self): |
| 93 |
return odeint(self.compute_step, self.initial_point, self.times) |
| 94 |
|
| 95 |
@cached_property |
| 96 |
def _get_times(self): |
| 97 |
return arange(self.time_start, self.time_stop, self.time_step) |
| 98 |
|
| 99 |
|
| 100 |
@provides(IPlottable2d) |
| 101 |
class LorenzIPlottable2dAdapter(Adapter, IModel3dIPlottable2dMixin): |
| 102 |
adaptee = Instance(Lorenz) |
| 103 |
|
| 104 |
plot_type = Str("line") |
| 105 |
|
| 106 |
|
| 107 |
register_factory(LorenzIPlottable2dAdapter, Lorenz, IPlottable2d) |