|
2 | 2 | // it under the terms of the GNU General Public License as published by |
3 | 3 | // the Free Software Foundation, either version 3 of the License, or |
4 | 4 | // (at your option) any later version. |
5 | | - |
6 | 5 | // This program is distributed in the hope that it will be useful, |
7 | 6 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
8 | 7 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
9 | 8 | // GNU General Public License for more details. |
10 | | - |
11 | 9 | // You should have received a copy of the GNU General Public License |
12 | 10 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
13 | 11 |
|
14 | 12 | #pragma once |
| 13 | +#include "floatdef.h" |
15 | 14 | #include "struct/particle.h" |
16 | | -#include <vector> |
17 | 15 | #include <algorithm> |
18 | 16 | #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; |
20 | 21 |
|
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 | + } |
24 | 27 |
|
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)); |
31 | 29 |
|
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; |
33 | 33 |
|
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); |
37 | 36 |
|
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 |
44 | 40 |
|
45 | | - return dt; |
| 41 | + return dt; |
46 | 42 | } |
0 commit comments