MRaster examples 22.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 -preset veryslow 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 -preset veryslow doublePendulumM_center_100_crf03.mp4;
39 ffmpeg -y -framerate 15 -i doublePendulumM_center_%4d.tiff -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -c:v libx264 -crf 0 -b:v 0 -preset veryslow doublePendulumM_center_100_crf00.mp4;
40
41 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 -preset veryslow doublePendulumM_corner_100_crf30.mp4;
42 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 -preset veryslow doublePendulumM_corner_100_crf03.mp4;
43 ffmpeg -y -framerate 15 -i doublePendulumM_corner_%4d.tiff -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -c:v libx264 -crf 0 -b:v 0 -preset veryslow doublePendulumM_corner_100_crf00.mp4;
44*/
45/*******************************************************************************************************************************************************.H.E.**/
46/** @cond exj */
47
48//--------------------------------------------------------------------------------------------------------------------------------------------------------------
49#include "ramCanvas.hpp"
50#include "MRMathSTR.hpp"
51#include "MRMathIVL.hpp"
52
53//--------------------------------------------------------------------------------------------------------------------------------------------------------------
54int main(void) {
55 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
56 const bool CENTER = false;
57 const int NUMFRM = 1080;
58 const int IMXSIZ = 7680/8;
59 const int IMYSIZ = 7680/8;
60 const double m1 = 1.0;
61 const double m2 = 1.0;
62 const double L1 = 1.0;
63 const double L2 = 1.0;
64 const double h = 0.01;
65 const double g = 9.80665;
66 const double p = std::numbers::pi;
67
68 mjr::ramCanvas1c64F theta1canvas(IMXSIZ, IMYSIZ);
69 mjr::ramCanvas1c64F omega1canvas(IMXSIZ, IMYSIZ);
70 mjr::ramCanvas1c64F theta2canvas(IMXSIZ, IMYSIZ);
71 mjr::ramCanvas1c64F omega2canvas(IMXSIZ, IMYSIZ);
72
73 mjr::ramCanvas3c8b pcolorCanvas(IMXSIZ, IMYSIZ);
74 if (CENTER)
75 pcolorCanvas.newRealCoords(0, 2*p, 0, 2*p);
76 else
77 pcolorCanvas.newRealCoords(-p, p, -p, p);
78 pcolorCanvas.clrCanvasToBlack();
79
80 for(int frame=0; frame<NUMFRM; frame++) {
81# pragma omp parallel for schedule(static,1)
82 for(int y=0;y<pcolorCanvas.getNumPixY();y++) {
83 for(int x=0;x<pcolorCanvas.getNumPixX();x++) {
84 double theta1, omega1, theta2, omega2;
85 if (frame == 0) {
86 theta1 = pcolorCanvas.int2realX(x);
87 omega1 = 0.0;
88 theta2 = pcolorCanvas.int2realY(y);
89 omega2 = 0.0;
90 } else {
91 theta1 = theta1canvas.getPxColorRefNC(x, y).getC0();
92 omega1 = omega1canvas.getPxColorRefNC(x, y).getC0();
93 theta2 = theta2canvas.getPxColorRefNC(x, y).getC0();
94 omega2 = omega2canvas.getPxColorRefNC(x, y).getC0();
95 double cd = cos(theta1-theta2);
96 double sd = sin(theta1-theta2);
97 double o12 = omega1*omega1;
98 double o22 = omega2*omega2;
99 double denom = 2*m1+m2-m2*cos(2*theta1-2*theta2);
100 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);
101 double new_omega2 = (2*sd*(o12*L1*(m1+m2)+g*(m1+m2)*cos(theta1)+o22*L2*m2*cd))/(L2*denom);
102 theta1 += h*omega1;
103 omega1 += h*new_omega1;
104 theta2 += h*omega2;
105 omega2 += h*new_omega2;
106 }
107 theta1canvas.drawPoint(x, y, theta1);
108 omega1canvas.drawPoint(x, y, omega1);
109 theta2canvas.drawPoint(x, y, theta2);
110 omega2canvas.drawPoint(x, y, omega2);
111 double r = mjr::math::ivl::wrapCO(theta2, 2*p)/(2*p);
112 double b = mjr::math::ivl::wrapCO(theta1, 2*p)/(2*p);
113 double g = mjr::math::ivl::wrapCC((omega1+omega2)/(4*p), 1.0);
114 pcolorCanvas.getPxColorRefNC(x, y).setChansRGB_dbl(r, g, b);
115 }
116 }
117 pcolorCanvas.writeTIFFfile((CENTER ? "doublePendulumM_center_" : "doublePendulumM_corner_") + mjr::math::str::fmt_int(frame, 4, '0') + ".tiff");
118# pragma omp critical
119 std::cout << "FRAME(" << frame << "): " << "DONE" << std::endl;
120 }
121 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
122 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
123 return 0;
124}
125/** @endcond */
126
int main(int argc, char *argv[])