MRaster examples 22.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
lorenz_mM.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file lorenz_mM.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief For each pixel, simulate a double pendulum system over 2sec and color the pixel according to the pendulum end state.@EOL
7 @std C++20
8 @copyright
9 @parblock
10 Copyright (c) 2025, Mitchell Jay Richling <https://www.mitchr.me> All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
13
14 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
15
16 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18
19 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software
20 without specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27 DAMAGE.
28 @endparblock
29 @filedetails
30
31 This code makes a movie of time steps of the solution the Lorenz system. Each pixel is a complete simulation of the Lorenz system with the pixel's color
32 encoding the system state (red for x, green for y, and blue for z). The initial values for x range from -20 to 20, for z they range from 20 to 50, and y is
33 always zero.
34
35 For reference the Lorenz system is defined by:
36 @f[ \begin{array}{lcl}
37 \frac{dx}{dt} & = & a(y-x) \\
38 \frac{dy}{dt} & = & x(b-z)-y \\
39 \frac{dz}{dt} & = & xy-cz
40 \end{array} @f]
41
42 Traditional parameter values are:
43
44 @f[ \begin{array}{lcc}
45 a & = & 10 \\
46 b & = & 28 \\
47 c & = & \frac{8}{3}
48 \end{array} @f]
49
50 And the traditional initial conditions are:
51 @f[ \begin{array}{lcc}
52 x & = & \frac{1}{10} \\
53 y & = & 0 \\
54 z & = & 0
55 \end{array} @f]
56
57 This program produces an image sequence which may be rendered into a movie with ffmpeg.
58
59 ffmpeg -y -framerate 15 -i lorenz_mM_%4d.tiff -vf "scale=trunc(iw/4)*2:trunc(ih/4)*2" -c:v libx264 -crf 30 -b:v 0 -preset veryslow lorenz_mM_100_crf30.mp4;
60 ffmpeg -y -framerate 15 -i lorenz_mM_%4d.tiff -vf "scale=trunc(iw/4)*2:trunc(ih/4)*2" -c:v libx264 -crf 20 -b:v 0 -preset veryslow lorenz_mM_100_crf20.mp4;
61 ffmpeg -y -framerate 15 -i lorenz_mM_%4d.tiff -vf "scale=trunc(iw/4)*2:trunc(ih/4)*2" -c:v libx264 -crf 10 -b:v 0 -preset veryslow lorenz_mM_100_crf10.mp4;
62 ffmpeg -y -framerate 15 -i lorenz_mM_%4d.tiff -vf "scale=trunc(iw/4)*2:trunc(ih/4)*2" -c:v libx264 -crf 3 -b:v 0 -preset veryslow lorenz_mM_100_crf03.mp4;
63 ffmpeg -y -framerate 15 -i lorenz_mM_%4d.tiff -vf "scale=trunc(iw/4)*2:trunc(ih/4)*2" -c:v libx264 -crf 0 -b:v 0 -preset veryslow lorenz_mM_100_crf00.mp4;
64*/
65/*******************************************************************************************************************************************************.H.E.**/
66/** @cond exj */
67
68//--------------------------------------------------------------------------------------------------------------------------------------------------------------
69#include "ramCanvas.hpp"
70#include "MRMathSTR.hpp"
71#include "MRMathIVL.hpp"
72
73//--------------------------------------------------------------------------------------------------------------------------------------------------------------
74int main(void) {
75 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
76 const int NUMFRM = 1000;
77 const int STPPF = 1000/1000;
78 const int IMXSIZ = 7680/4;
79 const int IMYSIZ = 7680/4;
80 const double tDelta = 0.00001*1000;
81 const double a = 10.0;
82 const double b = 28.0;
83 const double c = 8.0 / 3;
84
85 mjr::ramCanvas1c64F xcanvas(IMXSIZ, IMYSIZ);
86 mjr::ramCanvas1c64F ycanvas(IMXSIZ, IMYSIZ);
87 mjr::ramCanvas1c64F zcanvas(IMXSIZ, IMYSIZ);
88
89 mjr::ramCanvas3c8b pcolorCanvas(IMXSIZ, IMYSIZ, -20, 20, 20, 50);
90 pcolorCanvas.clrCanvasToBlack();
91
92 for(int frame=0; frame<NUMFRM; frame++) {
93# pragma omp parallel for schedule(static,1)
94 for(int yi=0;yi<pcolorCanvas.getNumPixY();yi++) {
95 for(int xi=0;xi<pcolorCanvas.getNumPixX();xi++) {
96 double x, y, z;
97 for(int step=0; step<STPPF; step++) {
98 if (frame == 0) {
99 x = pcolorCanvas.int2realX(xi);
100 y = 0.0;
101 z = pcolorCanvas.int2realY(yi);
102 } else {
103 x = xcanvas.getPxColorRefNC(xi, yi).getC0();
104 y = ycanvas.getPxColorRefNC(xi, yi).getC0();
105 z = zcanvas.getPxColorRefNC(xi, yi).getC0();
106 double xNew = x + a * (y - x) * tDelta;
107 double yNew = y + (x * (b - z) - y) * tDelta;
108 double zNew = z + (x * y - c * z) * tDelta;
109 x = xNew;
110 y = yNew;
111 z = zNew;
112 }
113 xcanvas.drawPoint(xi, yi, x);
114 ycanvas.drawPoint(xi, yi, y);
115 zcanvas.drawPoint(xi, yi, z);
116 }
117 double r = mjr::math::ivl::wrapCO((x+20)/40, 1.0);
118 double g = mjr::math::ivl::wrapCO((y+20)/40, 1.0);
119 double b = mjr::math::ivl::wrapCO((z-20)/30, 1.0);
120 pcolorCanvas.getPxColorRefNC(xi, yi).setChansRGB_dbl(r, g, b);
121 }
122 }
123 pcolorCanvas.writeTIFFfile("lorenz_mM_" + mjr::math::str::fmt_int(frame, 4, '0') + ".tiff");
124# pragma omp critical
125 std::cout << "FRAME(" << frame << "): " << "DONE" << std::endl;
126 }
127 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
128 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
129 return 0;
130}
131/** @endcond */
132
int main(int argc, char *argv[])