Skip to content

Commit

Permalink
feat(mfsimlist): add functionality to parse memory_print_options (#2009)
Browse files Browse the repository at this point in the history
* add function to return memory summary information as a dictionary
* add function to return full memory table information as a dictionary
  • Loading branch information
jdhughes-usgs committed Nov 22, 2023
1 parent be104c4 commit 121ca78
Show file tree
Hide file tree
Showing 2 changed files with 343 additions and 31 deletions.
61 changes: 59 additions & 2 deletions autotest/test_mfsimlist.py
@@ -1,16 +1,28 @@
import numpy as np
import pandas as pd
import pytest
from autotest.conftest import get_example_data_path
from modflow_devtools.markers import requires_exe

import flopy
from flopy.mf6 import MFSimulation

MEMORY_UNITS = ("gigabytes", "megabytes", "kilobytes", "bytes")


def base_model(sim_path, memory_print_option=None):
MEMORY_PRINT_OPTIONS = ("summary", "all")
if memory_print_option is not None:
if memory_print_option.lower() not in MEMORY_PRINT_OPTIONS:
raise ValueError(
f"invalid memory_print option ({memory_print_option.lower()})"
)

def base_model(sim_path):
load_path = get_example_data_path() / "mf6-freyberg"

sim = MFSimulation.load(sim_ws=load_path)
if memory_print_option is not None:
sim.memory_print_option = memory_print_option
sim.set_sim_path(sim_path)
sim.write_simulation()
sim.run_simulation()
Expand All @@ -27,7 +39,7 @@ def test_mfsimlist_nofile(function_tmpdir):
def test_mfsimlist_normal(function_tmpdir):
sim = base_model(function_tmpdir)
mfsimlst = flopy.mf6.utils.MfSimulationList(function_tmpdir / "mfsim.lst")
assert mfsimlst.is_normal_termination, "model did not terminate normally"
assert mfsimlst.normal_termination, "model did not terminate normally"


@pytest.mark.xfail
Expand Down Expand Up @@ -95,6 +107,13 @@ def test_mfsimlist_memory(function_tmpdir):
f"total memory is not greater than 0.0 " + f"({total_memory})"
)

total_memory_kb = mfsimlst.get_memory_usage(units="kilobytes")
assert np.allclose(total_memory_kb, total_memory * 1e6), (
f"total memory in kilobytes ({total_memory_kb}) is not equal to "
+ f"the total memory converted to kilobytes "
+ f"({total_memory * 1e6})"
)

virtual_memory = mfsimlst.get_memory_usage(virtual=True)
if not np.isnan(virtual_memory):
assert virtual_memory == virtual_answer, (
Expand All @@ -107,3 +126,41 @@ def test_mfsimlist_memory(function_tmpdir):
f"total memory ({total_memory}) "
+ f"does not equal non-virtual memory ({non_virtual_memory})"
)


@requires_exe("mf6")
@pytest.mark.parametrize("mem_option", (None, "summary"))
def test_mfsimlist_memory_summary(mem_option, function_tmpdir):
KEYS = ("TDIS", "FREYBERG", "SLN_1")
sim = base_model(function_tmpdir, memory_print_option=mem_option)
mfsimlst = flopy.mf6.utils.MfSimulationList(function_tmpdir / "mfsim.lst")

if mem_option is None:
mem_dict = mfsimlst.get_memory_summary()
assert mem_dict is None, "Expected None to be returned"
else:
for units in MEMORY_UNITS:
mem_dict = mfsimlst.get_memory_summary(units=units)
for key in KEYS:
assert key in KEYS, f"memory summary key ({key}) not in KEYS"


@requires_exe("mf6")
@pytest.mark.parametrize("mem_option", (None, "all"))
def test_mfsimlist_memory_all(mem_option, function_tmpdir):
sim = base_model(function_tmpdir, memory_print_option=mem_option)
mfsimlst = flopy.mf6.utils.MfSimulationList(function_tmpdir / "mfsim.lst")

if mem_option is None:
mem_dict = mfsimlst.get_memory_all()
assert mem_dict is None, "Expected None to be returned"
else:
for units in MEMORY_UNITS:
mem_dict = mfsimlst.get_memory_all(units=units)
total = 0.0
for key, value in mem_dict.items():
total += value["MEMORYSIZE"]
# total_ = mfsimlst.get_memory_usage(units=units)
# diff = total_ - total
# percent_diff = 100.0 * diff / total_
assert total > 0.0, "memory is not greater than zero"

0 comments on commit 121ca78

Please sign in to comment.