MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
doublePendulum.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file doublePendulum.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 The equations of motion for the double pendulum may be written as a system of first degree differential equations:
32
33 @f[\begin{eqnarray}
34 \frac{\mathrm{d}\theta_1}{\mathrm{d}t} & = & \omega_1 \\
35 \frac{\mathrm{d}\theta_2}{\mathrm{d}t} & = & \omega_2 \\
36 \frac{\mathrm{d}\omega_1}{\mathrm{d}t} & = & \frac{-g (2 m_1+m_2) \sin(\theta_1)-m_2 g \sin(\theta_1-2 \theta_2)-2 m_2 (\omega_2^2 L_2+\omega_1^2 L_1 \cos(\theta_1-\theta_2))\sin(\theta_1-\theta_2)}{L_1 (2 m_1+m_2-m_2 \cos(2 \theta_1-2 \theta_2))} \\
37 \frac{\mathrm{d}\omega_2}{\mathrm{d}t} & = & \frac{2 \sin(\theta_1-\theta_2) (\omega_1^2 L_1 (m_1+m_2)+g (m_1+m_2) \cos(\theta_1)+\omega_2^2 L_2 m_2 \cos(\theta_1-\theta_2))}{L_2 (2 m_1+m_2-m_2 \cos(2 \theta_1-2 \theta_2))}
38 \end{eqnarray}@f]
39
40 Where
41
42 @f[\begin{array}{lll}
43 L_1 & = & \text{ Length of top bar} \\
44 L_2 & = & \text{ Length of bottom bar} \\
45 m_1 & = & \text{ Mass of top weight} \\
46 m_2 & = & \text{ Mass of bottom weight} \\
47 g & = & \text{ Acceleration of gravity} \\
48 \theta_1 & = & \text{ Vertical angle of top bar} \\
49 \theta_2 & = & \text{ Vertical angle of bottom bar} \\
50 \omega_1 & = & \text{ Angular velocity of top bar} \\
51 \omega_2 & = & \text{ Angular velocity of bottom bar}
52 \end{array}@f]
53
54 The idea is to run a double pendulum simulation for each pixel. Each pixel is mapped to a pair of angles -- the x coordinate is mapped to @f$ \theta_1 @f$,
55 and the y coordinate is mapped to @f$ \theta_2 @f$. These angles are used for the initial conditions for the double pendulum equations. We then solve
56 these equations over a time span of 2 seconds using 200 steps of Euler's method. We dump an image of the final state of the simulation at the end.
57
58 Two ways are provided to map pixels to angles controlled by the boolean `CENTER`. If it's `true`, then the angles are mapped left to right & top to bottom
59 from @f$0@f$ to @f$2\pi@f$. If it's `false`, we map from @f$-\pi@f$ to @f$\pi@f$. The effect is that when `CENTER` is true the larger angles are at the
60 center of the image, and they are at the corners otherwise.
61
62 Inspired by a reddit post:
63 https://www.reddit.com/r/FractalPorn/comments/1i4gdy8/visualization_of_a_double_pendulum_pixel_x_and_y/
64
65*/
66/*******************************************************************************************************************************************************.H.E.**/
67/** @cond exj */
68
69//--------------------------------------------------------------------------------------------------------------------------------------------------------------
70#include "ramCanvas.hpp"
71#include "MRMathCPP.hpp"
72
73//--------------------------------------------------------------------------------------------------------------------------------------------------------------
74int main(void) {
75 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
76 const bool CENTER = true;
77 const int IMXSIZ = 7680/2;
78 const int IMYSIZ = 7680/2;
79 const double m1 = 1.0;
80 const double m2 = 1.0;
81 const double L1 = 1.0;
82 const double L2 = 1.0;
83 const int steps = 200;
84 const double h = 0.01;
85 const double g = 9.80665;
86 const double p = std::numbers::pi;
87
88 mjr::ramCanvas3c8b theRamCanvas(IMXSIZ, IMYSIZ);
89 if (CENTER)
90 theRamCanvas.newRealCoords(0, 2*p, 0, 2*p);
91 else
92 theRamCanvas.newRealCoords(-p, p, -p, p);
93 theRamCanvas.clrCanvasToBlack();
94
95# pragma omp parallel for schedule(static,1)
96 for(int y=0;y<theRamCanvas.getNumPixY();y++) {
97 if ((y%100)==0)
98 std::cout << y << std::endl;
99 for(int x=0;x<theRamCanvas.getNumPixX();x++) {
100 double theta1 = theRamCanvas.int2realX(x);
101 double omega1 = 0.0;
102 double theta2 = theRamCanvas.int2realY(y);
103 double omega2 = 0.0;
104 for(int i=0; i<steps; i++) {
105 double cd = cos(theta1-theta2);
106 double sd = sin(theta1-theta2);
107 double o12 = omega1*omega1;
108 double o22 = omega2*omega2;
109 double denom = 2*m1+m2-m2*cos(2*theta1-2*theta2);
110 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);
111 double new_omega2 = (2*sd*(o12*L1*(m1+m2)+g*(m1+m2)*cos(theta1)+o22*L2*m2*cd))/(L2*denom);
112 theta1 += h*omega1;
113 omega1 += h*new_omega1;
114 theta2 += h*omega2;
115 omega2 += h*new_omega2;
116 }
117 double r = mjr::math::ivl::wrapCO(theta2, 2*p)/(2*p);
118 double b = mjr::math::ivl::wrapCO(theta1, 2*p)/(2*p);
119 double g = mjr::math::ivl::wrapCC((omega1+omega2)/(4*p), 1.0);
120 theRamCanvas.getPxColorRefNC(x, y).setChansRGB_dbl(r, g, b);
121 }
122 }
123 theRamCanvas.writeTIFFfile((CENTER ? "doublePendulum_center.tiff" : "doublePendulum_corner.tiff"));
124 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
125 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
126 return 0;
127}
128/** @endcond */
129
int main(int argc, char *argv[])