MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
doublePendulumM.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file doublePendulumM.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 double pendulum equations. For the equations, see doublePendulum.cpp.
32
33 In this program we solve solve the equations over a time span of 10 seconds using 1000 steps of Euler's method. We dump out an image for each step.
34
35 Make Movies:
36
37 ffmpeg -y -framerate 15 -i doublePendulumM_center_%4d.tiff -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -c:v libx264 -crf 30 -b:v 0 -pix_fmt yuv420p doublePendulumM_center_100_crf30.mp4;
38 ffmpeg -y -framerate 15 -i doublePendulumM_center_%4d.tiff -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -c:v libx264 -crf 3 -b:v 0 -pix_fmt yuv420p doublePendulumM_center_100_crf01.mp4;
39 ffmpeg -y -framerate 15 -i doublePendulumM_corner_%4d.tiff -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -c:v libx264 -crf 30 -b:v 0 -pix_fmt yuv420p doublePendulumM_corner_100_crf30.mp4;
40 ffmpeg -y -framerate 15 -i doublePendulumM_corner_%4d.tiff -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -c:v libx264 -crf 3 -b:v 0 -pix_fmt yuv420p doublePendulumM_corner_100_crf01.mp4;
41
42*/
43/*******************************************************************************************************************************************************.H.E.**/
44/** @cond exj */
45
46//--------------------------------------------------------------------------------------------------------------------------------------------------------------
47#include "ramCanvas.hpp"
48#include "MRMathCPP.hpp"
49
50//--------------------------------------------------------------------------------------------------------------------------------------------------------------
51int main(void) {
52 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
53 const bool CENTER = false;
54 const int NUMFRM = 1080;
55 const int IMXSIZ = 7680/8;
56 const int IMYSIZ = 7680/8;
57 const double m1 = 1.0;
58 const double m2 = 1.0;
59 const double L1 = 1.0;
60 const double L2 = 1.0;
61 const double h = 0.01;
62 const double g = 9.80665;
63 const double p = std::numbers::pi;
64
65 mjr::ramCanvas1c64F theta1canvas(IMXSIZ, IMYSIZ);
66 mjr::ramCanvas1c64F omega1canvas(IMXSIZ, IMYSIZ);
67 mjr::ramCanvas1c64F theta2canvas(IMXSIZ, IMYSIZ);
68 mjr::ramCanvas1c64F omega2canvas(IMXSIZ, IMYSIZ);
69
70 mjr::ramCanvas3c8b pcolorCanvas(IMXSIZ, IMYSIZ);
71 if (CENTER)
72 pcolorCanvas.newRealCoords(0, 2*p, 0, 2*p);
73 else
74 pcolorCanvas.newRealCoords(-p, p, -p, p);
75 pcolorCanvas.clrCanvasToBlack();
76
77 for(int frame=0; frame<NUMFRM; frame++) {
78# pragma omp parallel for schedule(static,1)
79 for(int y=0;y<pcolorCanvas.getNumPixY();y++) {
80 for(int x=0;x<pcolorCanvas.getNumPixX();x++) {
81 double theta1, omega1, theta2, omega2;
82 if (frame == 0) {
83 theta1 = pcolorCanvas.int2realX(x);
84 omega1 = 0.0;
85 theta2 = pcolorCanvas.int2realY(y);
86 omega2 = 0.0;
87 } else {
88 theta1 = theta1canvas.getPxColorRefNC(x, y).getC0();
89 omega1 = omega1canvas.getPxColorRefNC(x, y).getC0();
90 theta2 = theta2canvas.getPxColorRefNC(x, y).getC0();
91 omega2 = omega2canvas.getPxColorRefNC(x, y).getC0();
92 double cd = cos(theta1-theta2);
93 double sd = sin(theta1-theta2);
94 double o12 = omega1*omega1;
95 double o22 = omega2*omega2;
96 double denom = 2*m1+m2-m2*cos(2*theta1-2*theta2);
97 double new_omega1 = (-g*(2*m1+m2)*sin(theta1)-m2*g*sin(theta1-2*theta2)-2*sd*m2*(o22*L2+o12*L1*cd))/(L1*denom);
98 double new_omega2 = (2*sd*(o12*L1*(m1+m2)+g*(m1+m2)*cos(theta1)+o22*L2*m2*cd))/(L2*denom);
99 theta1 += h*omega1;
100 omega1 += h*new_omega1;
101 theta2 += h*omega2;
102 omega2 += h*new_omega2;
103 }
104 theta1canvas.drawPoint(x, y, theta1);
105 omega1canvas.drawPoint(x, y, omega1);
106 theta2canvas.drawPoint(x, y, theta2);
107 omega2canvas.drawPoint(x, y, omega2);
108 double r = mjr::math::ivl::wrapCO(theta2, 2*p)/(2*p);
109 double b = mjr::math::ivl::wrapCO(theta1, 2*p)/(2*p);
110 double g = mjr::math::ivl::wrapCC((omega1+omega2)/(4*p), 1.0);
111 pcolorCanvas.getPxColorRefNC(x, y).setChansRGB_dbl(r, g, b);
112 }
113 }
114 pcolorCanvas.writeTIFFfile((CENTER ? "doublePendulumM_center_" : "doublePendulumM_corner_") + mjr::math::str::fmt_int(frame, 4, '0') + ".tiff");
115# pragma omp critical
116 std::cout << "FRAME(" << frame << "): " << "DONE" << std::endl;
117 }
118 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
119 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
120 return 0;
121}
122/** @endcond */
123
int main(int argc, char *argv[])