Skip to content

Instantly share code, notes, and snippets.

@nna774
Last active Oct 25, 2020
Embed
What would you like to do?
gyogun
#include <iostream>
#include <array>
#include <numbers>
#include <cmath>
int const N = 10;
int prev(int n) {
return (N + n - 1) % N;
}
double const dt = 0.0001;
double const to = 100;
int const max_step = to / dt + 1;
double const K = 1;
int const by = max_step / 5000;
int main(int, char**) {
std::array<std::pair<double, double>, N> xs, vs;
for (int i{}; i < size(xs); ++i) {
using std::numbers::pi;
auto arg = 2 * pi / N * i;
xs[i] = std::make_pair(std::cos(arg), std::sin(arg));
}
for (int i{}; i < max_step; ++i) {
for (int j{}; j < size(xs); ++j) {
// symplectic
xs[j].first += vs[j].first * dt;
xs[j].second += vs[j].second * dt;
vs[j].first = K * (xs[prev(j)].first - xs[j].first);
vs[j].second = K * (xs[prev(j)].second - xs[j].second);
}
if(i / by * by == i) {
for(int j{}; j < size(xs); ++j) {
std::cout << dt * i << ' ' << j << ' ' << xs[j].first << ' ' << xs[j].second << ' ' << vs[j].first << ' ' << vs[j].second << std::endl;
}
std::cout << std::endl;
}
}
return 0;
}
#! /bin/bash
cat << EOS | gnuplot
set terminal png
set out 'orbit.png'
p 'out.dat' u 3:4 w p
EOS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment