Skip to content

Commit 300d516

Browse files
committed
Create cprofile_tsm.py
1 parent 701a965 commit 300d516

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

examples/dev_sandbox/cprofile_tsm.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import clearwater_modules
2+
import numpy as np
3+
import xarray as xr
4+
import cProfile
5+
from pathlib import Path
6+
7+
8+
def create_data_array(
9+
gridsize: int,
10+
value: int | float
11+
):
12+
data = np.full(gridsize, value)
13+
data_array = xr.DataArray(data)
14+
return data_array
15+
16+
def run_performance_test(
17+
iters: int,
18+
gridsize: int
19+
):
20+
"""Log the performance for different numbers of iterations."""
21+
22+
if gridsize == 1:
23+
state_i = {
24+
'water_temp_c': 40.0,
25+
'surface_area': 1.0,
26+
'volume': 1.0,
27+
}
28+
meteo_parameters = {'wind_c': 1.0}
29+
30+
else:
31+
state_i = {
32+
'water_temp_c': create_data_array(gridsize, 40.0),
33+
'surface_area': create_data_array(gridsize, 1.0),
34+
'volume': create_data_array(gridsize, 1.0),
35+
}
36+
meteo_parameters = {
37+
'wind_c': 1.0
38+
}
39+
40+
# instantiate the TSM module
41+
tsm = clearwater_modules.tsm.EnergyBudget(
42+
time_steps=iters,
43+
initial_state_values=state_i,
44+
meteo_parameters=meteo_parameters,
45+
track_dynamic_variables=False,
46+
)
47+
48+
for _ in range(iters):
49+
tsm.increment_timestep()
50+
51+
52+
if __name__ == '__main__':
53+
iterations_list = [300000, 400000, 500000] # [1000, 10000, 100000, 150000, 200000]
54+
gridsize_list = [10000, 100000]
55+
cwd = Path.cwd()
56+
print(cwd)
57+
root = Path('./examples/dev_sandbox/profiling/')
58+
# iterations_list = [1000]
59+
# gridsize_list = [1]
60+
# detailed_profile = True
61+
for iteration in iterations_list:
62+
for gridsize in gridsize_list:
63+
cprofile_file = root / f'{iteration}_iters_{gridsize}_gridsize.prof'
64+
profiler = cProfile.Profile()
65+
profiler.enable()
66+
run_performance_test(iteration, gridsize)
67+
profiler.disable()
68+
profiler.dump_stats(cprofile_file)

0 commit comments

Comments
 (0)