Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ name: CMake on multiple platforms

on:
push:
branches: [ "main" ]
branches:
- main
- adaptiveDt
pull_request:
branches: [ "main" ]

Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ endif()

# Includes and sources
include_directories(${CMAKE_SOURCE_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/src)
file(GLOB SRC_FILES ${CMAKE_SOURCE_DIR}/src/*.cpp)

# Build executable
add_executable(next ${SRC_FILES})

13 changes: 8 additions & 5 deletions src/begrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include<iostream>
#include<fstream>
#include"floatdef.h"
#include"dt/adaptive.h"

std::vector<Particle> LoadParticlesFromFile(const std::string& filename)
{
Expand All @@ -26,23 +27,25 @@ int main(int argc, char** argv) {
<< "NN NN EEEEEEE XX XX TTT \n"
<< "Newtonian EXact Trajectories\n";

if (argc != 2)
if (argc != 3)
{
std::cerr << "Usage: next <initial.txt>\n";
std::cerr << "Usage: next <initial.txt> <dt>\n";
return 1;
}

const char* filename = argv[1];
real dt = std::stod(argv[2]);

std::vector<Particle> particles = LoadParticlesFromFile(filename);

real dt = 0.01;
while (true)
{
Step(particles, dt);
real dtAdaptive = computeAdaptiveDt(particles, dt);
Step(particles, dtAdaptive);
}

return 0;


}
}

31 changes: 31 additions & 0 deletions src/dt/adaptive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
#include "struct/particle.h"
#include <vector>
#include <algorithm>
#include <cmath>
#include "floatdef.h"

real computeAdaptiveDt(const std::vector<Particle>& p, real base_dt)
{
real maxSpeed = 0;

for (const auto& a : p)
{
real speed = std::sqrt(a.vx*a.vx + a.vy*a.vy + a.vz*a.vz);
if (speed > maxSpeed)
maxSpeed = speed;
}

// If everything is basically stationary, use base dt
if (maxSpeed < real(1e-8))
return base_dt;

// Smaller dt when speeds are high
real dt = base_dt / (1 + maxSpeed);

// Clamp dt to a reasonable range
dt = std::max(dt, base_dt * real(0.01)); // never smaller than 1% of base
dt = std::min(dt, base_dt * real(1.0)); // never larger than base

return dt;
}