Skip to content

Commit 92c7845

Browse files
Update adaptive.h
1 parent 821b195 commit 92c7845

1 file changed

Lines changed: 20 additions & 24 deletions

File tree

src/dt/adaptive.h

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,41 @@
22
// it under the terms of the GNU General Public License as published by
33
// the Free Software Foundation, either version 3 of the License, or
44
// (at your option) any later version.
5-
65
// This program is distributed in the hope that it will be useful,
76
// but WITHOUT ANY WARRANTY; without even the implied warranty of
87
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
98
// GNU General Public License for more details.
10-
119
// You should have received a copy of the GNU General Public License
1210
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1311

1412
#pragma once
13+
#include "floatdef.h"
1514
#include "struct/particle.h"
16-
#include <vector>
1715
#include <algorithm>
1816
#include <cmath>
19-
#include "floatdef.h"
17+
#include <vector>
18+
19+
real computeAdaptiveDt(const std::vector<Particle> &p, real base_dt) {
20+
real maxSpeed = 0;
2021

21-
real computeAdaptiveDt(const std::vector<Particle>& p, real base_dt)
22-
{
23-
real maxSpeed = 0;
22+
for (const auto &a : p) {
23+
real speed = std::sqrt(a.vx * a.vx + a.vy * a.vy + a.vz * a.vz);
24+
if (speed > maxSpeed)
25+
maxSpeed = speed;
26+
}
2427

25-
for (const auto& a : p)
26-
{
27-
real speed = std::sqrt(a.vx*a.vx + a.vy*a.vy + a.vz*a.vz);
28-
if (speed > maxSpeed)
29-
maxSpeed = speed;
30-
}
28+
maxSpeed = std::min(maxSpeed, real(1e4));
3129

32-
maxSpeed = std::min(maxSpeed, real(1e4));
30+
// If everything is basically stationary, use base dt
31+
if (maxSpeed < real(1e-8))
32+
return base_dt;
3333

34-
// If everything is basically stationary, use base dt
35-
if (maxSpeed < real(1e-8))
36-
return base_dt;
34+
// Smaller dt when speeds are high
35+
real dt = base_dt / (1 + maxSpeed);
3736

38-
// Smaller dt when speeds are high
39-
real dt = base_dt / (1 + maxSpeed);
40-
41-
// Clamp dt to a reasonable range
42-
dt = std::max(dt, base_dt * real(0.01)); // never smaller than 1% of base
43-
dt = std::min(dt, base_dt * real(1.0)); // never larger than base
37+
// Clamp dt to a reasonable range
38+
dt = std::max(dt, base_dt * real(0.01)); // never smaller than 1% of base
39+
dt = std::min(dt, base_dt * real(1.0)); // never larger than base
4440

45-
return dt;
41+
return dt;
4642
}

0 commit comments

Comments
 (0)