From 411c20b97ab25aa6537edc99904a3e139f1c9421 Mon Sep 17 00:00:00 2001 From: Rafael M Mudafort Date: Wed, 30 Aug 2023 09:50:01 -0500 Subject: [PATCH] Add scaling notebook # Conflicts: # profiling/timing.py --- profiling/profiling_utils.py | 56 ++++++++++++++++++++++++++++++++++++ profiling/timing.py | 8 ++++-- tests/conftest.py | 10 +++---- 3 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 profiling/profiling_utils.py diff --git a/profiling/profiling_utils.py b/profiling/profiling_utils.py new file mode 100644 index 000000000..674ff9b92 --- /dev/null +++ b/profiling/profiling_utils.py @@ -0,0 +1,56 @@ + +import copy +import time +import numpy as np +import memory_profiler +from floris.simulation import Floris + + +def run_floris(input_dict: dict) -> float: + + # Deep copy the input dictionary to ensure no side effects + input_dict = copy.deepcopy(input_dict) + + # Start timing + start = time.perf_counter() + + # Run floris calculation + floris = Floris.from_dict(input_dict) + floris.initialize_domain() + floris.steady_state_atmospheric_condition() + + # Stop timing + end = time.perf_counter() + + return end - start + + +def time_profile(input_dict, N_ITERATIONS=30) -> float: + """ + Runs the wake calculation for N_ITERATIONS and returns + the average of the timing results. + An initial iteration of the calculation is run and not included in the + timing results in order to allow Python to compile what it needs to and + initialize the computer's memory. + """ + + # Run once to initialize Python and memory + run_floris(input_dict) + + # Get timing for each iteration + times = np.zeros(N_ITERATIONS) + for i in range(N_ITERATIONS): + times[i] = run_floris(input_dict) + + # Return the average + return np.mean(times) + + +def memory_profile(input_dict): + floris = Floris.from_dict(copy.deepcopy(input_dict["floris"])) + floris.initialize_domain() # TODO: include this in memory_usage()? + mem_usage = memory_profiler.memory_usage( + (floris.steady_state_atmospheric_condition, (), {}), + max_usage=True + ) + return mem_usage diff --git a/profiling/timing.py b/profiling/timing.py index e93f6523d..a7118474c 100644 --- a/profiling/timing.py +++ b/profiling/timing.py @@ -1,9 +1,7 @@ import copy -import time import matplotlib.pyplot as plt -import memory_profiler import numpy as np from conftest import SampleInputs @@ -11,6 +9,12 @@ from runners import time_profile, memory_profile +def internal_probe(input_dict): + floris = Floris(input_dict=input_dict.floris) + internal_quantity = floris.steady_state_atmospheric_condition() + return internal_quantity + + if __name__=="__main__": sample_inputs = SampleInputs() TURBINE_DIAMETER = sample_inputs.turbine["rotor_diameter"] diff --git a/tests/conftest.py b/tests/conftest.py index 27c65ef50..c408e64d6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -400,11 +400,11 @@ def __init__(self): "kd": 0.05, }, "empirical_gauss": { - "horizontal_deflection_gain_D": 3.0, - "vertical_deflection_gain_D": -1, - "deflection_rate": 15, - "mixing_gain_deflection": 0.0, - "yaw_added_mixing_gain": 0.0 + "horizontal_deflection_gain_D": 3.0, + "vertical_deflection_gain_D": -1, + "deflection_rate": 15, + "mixing_gain_deflection": 0.0, + "yaw_added_mixing_gain": 0.0 }, }, "wake_velocity_parameters": {