SIMLIB/C++ 3.09
Loading...
Searching...
No Matches
examples/lorenz.cc

Lorenz equation.

Lorenz equation

////////////////////////////////////////////////////////////////////////////
// Model lorenz SIMLIB/C++
//
// Lorenz equation:
//
// dx1/dt = sigma * (x2 - x1)
// dx2/dt = (1 + lambda - x3) * x1 - x2
// dx3/dt = x1 * x2 - b * x3
//
// Parameters: sigma = 10
// lambda = 24
// b = 2
// Initial conditions: xi(0) = 1.0
//
// SOURCE: SimPack
//
// Creates output for GNU-Plot
//
// Results strongly depend on required accuracy
//
#include "simlib.h"
#include <cstdlib> // atof()
const double DEFAULT_T1 = 30.0;
// Model
struct Lorenz {
Integrator x1, x2, x3;
Lorenz(double sigma, double lambda, double b) :
// blocks:
x1(sigma*(x2 - x1), 1), // dx1/dt = sigma * (x2 - x1)
x2((1 + lambda - x3)*x1 - x2, 1), // dx2/dt = (1 + lambda - x3) * x1 - x2
x3(x1*x2 - b*x3, 1) {} // dx3/dt = x1 * x2 - b * x3
};
Lorenz L(10, 24, 2); // Create instance of model
// Output sampling
void Sample() {
Print("%6.2f %.5g %.5g %.5g\n", T.Value(), L.x1.Value(), L.x2.Value(), L.x3.Value());
}
Sampler S(Sample, 0.01); // Output step
// Experiment control
int main(int argc, char *argv[]) {
double maxtime = DEFAULT_T1;
if (argc > 1)
maxtime = std::atof(argv[1]); // Parameter of simulation
if (maxtime < 1.0) {
_Print("\nUsage: %s [maxtime>=1,default=%g] \n\n", argv[0], DEFAULT_T1);
return 1;
}
SetOutput("lorenz.dat"); // Redirect output to file
Print("# Lorenz equation output (maxtime=%g) \n", maxtime);
Print("# Time x1 x2 \n");
Init(0, maxtime); // Initialize simulator
SetAccuracy(1e-8); // Required accuracy
Run(); // Simulate
SIMLIB_statistics.Output(); // Print simulation run statistics
}
//
Sampler S(Sample, 1)
void Sample()
Definition: _test_.cc:38
int main()
Definition: _test_.cc:44
virtual double Value()=0
get block output value this method should be defined in classes derived from aContiBlock
void SetAccuracy(double _abserr, double _relerr)
set max.
Definition: intg.cc:106
void SetOutput(const char *name)
redirects Output(), Print() to file
Definition: print.cc:50
int _Print(const char *fmt,...)
output of messages to stdout, too
Definition: print.cc:68
void Run()
run simulation experiment
Definition: run.cc:228
int Print(const char *fmt,...)
for Output methods, can be redirected
Definition: print.cc:92
void Init(double t0, double t1=SIMLIB_MAXTIME)
Initialize simulator and model time.
Definition: simlib.h:181
const SIMLIB_statistics_t & SIMLIB_statistics
interface to internal run-time statistics structure
Definition: run.cc:78
Main SIMLIB/C++ interface.
void Output() const
print run-time statistics to output
Definition: output1.cc:63