Skip to content

Commit eeb9bc3

Browse files
authored
Merge pull request #269 from KelsonMarine/line_perf_improvement
State and Line Performance Improvements
2 parents 4c98b79 + 198079b commit eeb9bc3

File tree

13 files changed

+437
-239
lines changed

13 files changed

+437
-239
lines changed

bench/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ endfunction()
2222

2323
add_bench(WaveBenchmark WaveBench.cpp)
2424
add_bench(MoordynBenchmark MDBench.cpp)
25+
add_bench(LinesBenchmark LinesBench.cpp)
2526

2627
add_custom_target(run_benchmarks
2728
COMMAND $<TARGET_FILE:WaveBenchmark>

bench/LinesBench.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "LinesBench.hpp"
2+
#include "Time.hpp"
3+
#include <benchmark/benchmark.h>
4+
#include <sstream>
5+
6+
/**
7+
* @brief Benchmarks stepping the model 0.1s
8+
* Loads the given model file and measures the duration
9+
* to complete an 0.1 second outer time step.
10+
* @param state Benchmark state
11+
* @param tScheme name of integrator to use
12+
*/
13+
static void
14+
MoorDynStepWithIntegrator(benchmark::State& state, const std::string& tScheme)
15+
{
16+
int num_lines = state.range(0);
17+
int num_segments = state.range(1);
18+
std::string input_file = "Mooring/cases/" + std::to_string(num_lines) +
19+
"_lines_" + std::to_string(num_segments) +
20+
"_segs/lines.txt";
21+
moordyn::MoorDyn system(input_file.c_str(), MOORDYN_NO_OUTPUT);
22+
system.SetDisableOutput(true);
23+
24+
// Don't have to free this pointer because it's done by the MoorDyn system.
25+
moordyn::TimeScheme* timeScheme = moordyn::create_time_scheme(
26+
tScheme, system.GetLogger(), system.GetWaves());
27+
system.SetTimeScheme(timeScheme);
28+
29+
// int err;
30+
unsigned int n_dof;
31+
n_dof = system.NCoupledDOF();
32+
double *x = NULL, *dx = NULL;
33+
if (n_dof) {
34+
x = new double[n_dof];
35+
std::fill(x, x + n_dof, 0.0);
36+
dx = new double[n_dof];
37+
std::fill(dx, dx + n_dof, 0.0);
38+
}
39+
system.Init(x, dx, true);
40+
41+
double t = 0.0, dt = 0.01;
42+
double f[3];
43+
44+
for (auto _ : state) {
45+
system.Step(x, dx, f, t, dt);
46+
}
47+
state.counters["OuterTimeStep"] = dt;
48+
}
49+
static void
50+
MoordynStepCaseRK4(benchmark::State& state)
51+
{
52+
MoorDynStepWithIntegrator(state, "RK4");
53+
}
54+
BENCHMARK(MoordynStepCaseRK4)
55+
->ArgsProduct({ { 1, 2, 4, 8, 16, 32, 64 }, { 1, 2, 4, 8, 16, 32, 64 } })
56+
->Unit(benchmark::kMicrosecond);
57+
58+
static void
59+
MoordynStepCaseRK2(benchmark::State& state)
60+
{
61+
MoorDynStepWithIntegrator(state, "RK2");
62+
}
63+
BENCHMARK(MoordynStepCaseRK2)
64+
->ArgsProduct({ { 1, 2, 4, 8, 16, 32, 64 }, { 1, 2, 4, 8, 16, 32, 64 } })
65+
->Unit(benchmark::kMicrosecond);
66+
67+
BENCHMARK_MAIN();

bench/LinesBench.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
#pragma once
3+
4+
#define _USE_MATH_DEFINES
5+
#include "MoorDyn.h"
6+
#include "MoorDyn2.hpp"
7+
#include "Waves.hpp"
8+
#include "Waves/SpectrumKin.hpp"
9+
10+
#include <cmath>
11+
#include <iostream>

bench/MDBench.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ LineGetStateDeriv(benchmark::State& state, std::string input_file)
2121

2222
auto line = system.GetLines().front();
2323

24+
std::vector<moordyn::vec> vel{};
25+
std::vector<moordyn::vec> acc{};
2426
for (auto _ : state) {
25-
line->getStateDeriv();
27+
line->getStateDeriv(vel, acc);
2628
}
2729
}
2830
BENCHMARK_CAPTURE(LineGetStateDeriv,

bench/Mooring/cases/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lines.txt
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from itertools import product
2+
import os
3+
from dataclasses import dataclass
4+
5+
"""Simple script for generating a bunch of model files for benchmarking purposes.
6+
7+
Just needs basic python (tested for version >=3.8).
8+
"""
9+
10+
11+
@dataclass
12+
class BenchmarkParams:
13+
num_lines: int
14+
num_segments: int
15+
16+
17+
def generate_benchmark(base_output_folder, params: BenchmarkParams):
18+
"""Generate a single benchmark model file.
19+
20+
The model has a user defined number of lines that each share a fixed top
21+
point, and have their own bottom point.
22+
They start off at a 10 degree angle from vertical.
23+
"""
24+
points = "\n".join(
25+
f"{i + 2} free 34.20201433256687 0.0 -93.96926207859084 0 0 0 0 -"
26+
for i in range(params.num_lines)
27+
)
28+
line_length = 100.0
29+
lines = "\n".join(
30+
f"{i+1} chain 1 {i + 2} {line_length} {params.num_segments} p,t"
31+
for i in range(params.num_lines)
32+
)
33+
34+
file_str = f"""--------------------- MoorDyn Input File -------------------------------------------------------
35+
Output from generate_cases.py
36+
----------------------- LINE TYPES --------------------------------------------------------------
37+
TypeName Diam Mass/m EA BA/-zeta EI Cd Ca CdAx CaAx
38+
(name) (m) (kg/m) (N) (N-s/-) (N-m^2) (-) (-) (-) (-)
39+
chain 0.252 390 16000000.0 -0.3 0.0 1.37 0.64 1.0 0.0
40+
----------------------- POINTS -------------------------------------------------------------------
41+
Node Type X Y Z M V CdA CA
42+
(-) (-) (m) (m) (m) (kg) (m^3) (m^2) (-)
43+
1 free 0.0 0.0 0.0 0 0 0 0 -
44+
{points}
45+
-------------------------- LINES -----------------------------------------------------------------
46+
Line LineType NodeA NodeB UnstrLen NumSegs Flags/Outputs
47+
(-) (-) (-) (-) (m) (-) (-)
48+
{lines}
49+
-------------------------- SOLVER OPTIONS---------------------------
50+
0 writeLog - Write a log file
51+
0.001 dtM - time step to use in mooring integration
52+
rk4 tScheme
53+
3000000.0 kb - bottom stiffness
54+
300000.0 cb - bottom damping
55+
200.0 WtrDpth - water depth
56+
4.0 ICDfac - factor to scale drag coeff during IC
57+
1.5e-06 threshIC - threshold for IC convergence
58+
0.0 TmaxIC - threshold for IC convergence
59+
1e-05 dtIC - Time lapse between convergence tests (s)
60+
9.81 g - gravitational force constant
61+
1025 rho - water density
62+
0 WaveKin - waves mode
63+
0 Currents - current mode
64+
------------------------- need this line --------------------------------------
65+
"""
66+
67+
folder_name = f"{params.num_lines}_lines_{params.num_segments}_segs"
68+
69+
output_folder = os.path.join(base_output_folder, folder_name)
70+
try:
71+
os.mkdir(output_folder)
72+
except FileExistsError:
73+
pass
74+
75+
out_path = os.path.join(output_folder, "lines.txt")
76+
77+
with open(out_path, "w") as f:
78+
f.write(file_str)
79+
80+
81+
def main():
82+
83+
number_of_lines = [2**n for n in range(7)]
84+
number_of_segments = [2**n for n in range(7)]
85+
86+
for vals in product(number_of_lines, number_of_segments):
87+
params = BenchmarkParams(*vals)
88+
generate_benchmark("./", params)
89+
90+
91+
if __name__ == "__main__":
92+
main()

docs/inputs.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@ The list of possible options is:
642642
If this is enabled initial conditions are calculated with scaled drag according to CdScaleIC.
643643
The new stationary solver in MoorDyn-C is more stable and more precise than the dynamic solver,
644644
but it can take longer to reach equilibrium.
645+
- disableOutput (0): Disables some console and file outputs to improve runtime.
646+
- disableOutTime (0): Disables the printing of the current timestep to the console, useful for the MATLAB wrapper
645647

646648
A note about time steps in MoorDyn-C: The internal time step is first taken from the dtM option. If
647649
no CFL factor is provided, then the user provided time step is used to calculate CFL and MoorDyn-C
@@ -676,6 +678,8 @@ The following MoorDyn-C options are not supported by MoorDyn-F:
676678
- FricDamp: Same as CV in MoorDyn-F.
677679
- StatDynFricScale: Same as MC in MoorDyn-F.
678680
- ICgenDynamic: MoorDyn-F does not have a stationary solver for initial conditions
681+
- disableOutput: MoorDyn-F output verbosity is controlled by OpenFAST
682+
- disableOutTime: MoorDyn-F output verbosity is controlled by OpenFAST
679683

680684
The following options from MoorDyn-F are not supported by MoorDyn-C:
681685

0 commit comments

Comments
 (0)