cpp/functions.cpp

71 lines
2.1 KiB
C++
Raw Normal View History

2024-08-16 17:55:27 +00:00
//
// Created by dominik on 8/14/24.
//#
#include <vector>
2024-08-20 15:51:49 +00:00
#include <array>
2024-08-16 17:55:27 +00:00
#include <chrono>
#include <iostream>
2024-08-20 15:51:49 +00:00
#include <cmath>
#include <utility>
2024-08-16 17:55:27 +00:00
int nearest_index(const std::vector<double> &x_ref, const double x, int start=0) {
while (x > x_ref[start+1]) {
start++;
}
return start;
}
double lerp(const std::vector<double>& x_ref, const std::vector<double>& y_ref, const double x, const int i) {
2024-08-18 11:21:27 +00:00
/*
* Linear interpolation between two
*/
2024-08-16 17:55:27 +00:00
const double x_left = x_ref[i];
const double y_left = y_ref[i];
const double x_right = x_ref[i+1];
const double y_right = y_ref[i+1];
const double dydx = (y_right - y_left) / ( x_right - x_left );
return y_left + dydx * (x - x_left);
}
2024-08-20 15:51:49 +00:00
std::array<double, 3> spherical_to_xyz(const double cos_theta, const double phi) {
const double sin_theta = std::sin(std::acos(cos_theta));
const std::array<double, 3> xyz{sin_theta * std::cos(phi), sin_theta * std::sin(phi), cos_theta};
return xyz;
}
std::pair<double, double> xyz_to_spherical(const std::array<double, 3>& xyz) {
// std::cout << "length " <<xyz[0]*xyz[0] + xyz[1]*xyz[1] + xyz[2]*xyz[2] << std::endl;
double cos_theta = xyz[2];
double phi = std::atan2(xyz[1], xyz[0]);
return std::make_pair(cos_theta, phi);
}
2024-08-16 17:55:27 +00:00
std::chrono::time_point<std::chrono::system_clock> printSteps(
2024-08-18 11:21:27 +00:00
/*
* Prints roughly every 10 seconds how many runs were done and gives a time estimation
*/
2024-08-16 17:55:27 +00:00
const std::chrono::time_point<std::chrono::system_clock> last_print_out,
const std::chrono::time_point<std::chrono::system_clock> start,
const int total,
const int steps
) {
const auto now = std::chrono::high_resolution_clock::now();
if (const std::chrono::duration<float> duration = now - last_print_out; duration.count() < 10.) {
return last_print_out;
}
2024-08-18 11:21:27 +00:00
const std::chrono::duration<float> duration = now - start;
2024-08-16 17:55:27 +00:00
const auto passed = duration.count();
std::cout << steps << " of " << total << " steps: " << passed << "s passed; ~" << passed * static_cast<float>(total-steps) / static_cast<float>(steps) << "s remaining\n";
return now;
}