Skip to content

[SimulationEngine] Refactor main daily simulation method#2772

Open
ew3361zh wants to merge 66 commits intodevfrom
refactor-daily-sim-2
Open

[SimulationEngine] Refactor main daily simulation method#2772
ew3361zh wants to merge 66 commits intodevfrom
refactor-daily-sim-2

Conversation

@ew3361zh
Copy link
Collaborator

@ew3361zh ew3361zh commented Feb 23, 2026

Refactors SimulationEngine._daily_simulation() to be a higher-level managerial function for a day in the life on a RuFaS farm. Also starts the process of more clearly accommodating multiple simulation types (i.e. isolating modules to run on their own or in smaller groups than as a whole farm) some of which are discussed in #2671.

Context

Issue(s) closed by this pull request: closes #2735

What

  1. Creates a series of high-level functions that more distinctly represent the daily operations executed in a simulation.
  2. Creates 2 versions of _daily_simulation() one of which executes running a full farm (_execute_full_farm_daily_simulation()) and the other of which executes a farm with no animals simulated (_execute_no_animals_daily_simulation()).
  3. Incorporates a new config input called simualtion_type which would determine which of these daily simulation functions would be used. Note, this is meant to overtake the simulate_animals config input so that we don't get conflicting inputs.

Why

The daily simulation function is overly complicated and busy. To try and figure out the order of operations and data flowing on a farm, it takes way too much effort. This simplifies that top level daily simulation function for clarity and readability. It also will making testing (which I didn't do yet for reasons to be discussed below) much simpler.
Additionally, we want to maintain this clarity of high level farm operations and exchange of data between them while allowing for multiple types of simulations where different modules or groups of modules are isolated. It also allows the model to have its daily operations set at startup and avoids checking boolean flags each simulation day to know whether or not to run a particular module.

How

  1. Grouped daily simulation operations by their respective modules to create high-level functions that more clearly summarize what is being done and in what order.
  2. Refactored _daily_simulation() to now have 2 main options:
  • _execute_full_farm_daily_simulation() which runs a full farm simulation with all modules and
  • _execute_no_animals_daily_simulation()where it runs everything from a full farm simulation except for animal operations and manure operations.
  • More to come in future PRs if this approach is approved.

Notes For Discussion

  1. As noted above, I think this PR brought up implications for future implementations in Add example scenario(s) to help maintain Module independence #2671. Namely, I think the primary approach we have in place right now to account for some modules running and others not running is a flag. While this works, it is also highly repetitive - the same flag with the same value checked thousands of times over a single simulation. It also leads to making this top-level function look overly busy again.
  2. I think the way we think about isolating different modules to run either alone or in groups will and should impact the structure of this _execute_daily_simulation() (pka _daily_simulation()) function in SimulationEngine.
  3. What's the best way to balance omitting certain modules from a simulation with avoiding repetitive daily flag checking?
  4. Testing will be updated once we've decided as a team that this is useful and in the final form we'd like for this stage.

Test plan

Try running a freestall simulation and a no-animals simulation. Both should run as before.
Unit testing will be expanded to cover all the new functions in SimulationEngine once this refactor approach is approved.

Input Changes

Output Changes

  • N/A

Filter

@github-actions
Copy link
Contributor

Current Coverage: %

Mypy errors on refactor-daily-sim-2 branch: 1690
Mypy errors on dev branch: 1684
6 more errors on refactor-daily-sim-2 branch

@github-actions
Copy link
Contributor

🚨 Please update the changelog. This PR cannot be merged until changelog.md is updated.
🚨 Some tests have failed.

@ew3361zh ew3361zh requested a review from morrowcj February 23, 2026 22:24
Copy link
Collaborator

@morrowcj morrowcj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a solid refactor with good docstrings added. As you mention in your Notes for Discussion, I wonder how skipping certain methods should work when only some of the modules are being simulated. If it were me, I would add flags for each module to the main simulation method - as I describe in a separate comment - but maybe that is not ideal, as you indicate.

@github-actions
Copy link
Contributor

Current Coverage: %

Mypy errors on refactor-daily-sim-2 branch: 1690
Mypy errors on dev branch: 1684
6 more errors on refactor-daily-sim-2 branch

@github-actions
Copy link
Contributor

🚨 Please update the changelog. This PR cannot be merged until changelog.md is updated.
🚨 Some tests have failed.

@KFosterReed
Copy link
Contributor

Thanks for getting this started on what looks (with a preliminary glance) like a great improvement! In reading through the current execute_daily_simulation and your docstrings, I wonder if one option for a solution to isolating the individual modules could be that the current execute_daily_simulation, becomes execute_whole _farm_daily_simulation that is called by a higher level/ introductory function that determines which of the modules are included in the simulation. So other options could be:

  • execute_only_animal_module_daily_simulation
  • execute_only_manure_module_daily_simulation
  • execute_only_field_module_daily_simulation
  • execute_only_feed_module__daily_simulation
  • execute_animal_and_manure_module__daily_simulation
  • ... you get the idea

Perhaps this is still too repetetive and it just adds another giant function? Something to add to the discussion at least :)

Copy link
Collaborator

@matthew7838 matthew7838 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks a lot cleaner, thanks, Niko. A few suggestions and ideas to discuss.
First, it would be easier to track by having the helper functions relocated to the order they're being called.
Second, I have an idea that can be discussed, which is a variant of what Kristan has. Instead of adding parallel/duplicate execution methods per module combination, we can define a single ordered function pipeline and build it once at startup based on enabled module flags. Since this PR already modularized module calls, each module step can be represented as a callable in that sequence. The daily simulation then just executes the precomputed list in order, which avoids per-day flag checks and keeps one canonical execution path.

@github-actions
Copy link
Contributor

Current Coverage: %

Mypy errors on refactor-daily-sim-2 branch: 1690
Mypy errors on dev branch: 1684
6 more errors on refactor-daily-sim-2 branch

@github-actions
Copy link
Contributor

🚨 Please update the changelog. This PR cannot be merged until changelog.md is updated.
🚨 Some tests have failed.

@github-actions
Copy link
Contributor

Current Coverage: %

Mypy errors on refactor-daily-sim-2 branch: 1691
Mypy errors on dev branch: 1684
7 more errors on refactor-daily-sim-2 branch

@github-actions
Copy link
Contributor

Current Coverage: %

Mypy errors on refactor-daily-sim-2 branch: 1251
Mypy errors on dev branch: 1245
6 more errors on refactor-daily-sim-2 branch

@github-actions
Copy link
Contributor

🚨 Unauthorized changes detected in protected files. Please remove these changes if they are not intended.
🚨 Flake8 linting errors were found. Please fix the linting issues.
🚨 Some tests have failed.

@github-actions
Copy link
Contributor

Current Coverage: 99%

Mypy errors on refactor-daily-sim-2 branch: 1251
Mypy errors on dev branch: 1249
2 more errors on refactor-daily-sim-2 branch

@github-actions
Copy link
Contributor

🚨 Unauthorized changes detected in protected files. Please remove these changes if they are not intended.

@github-actions
Copy link
Contributor

Current Coverage: %

Mypy errors on refactor-daily-sim-2 branch: 1249
Mypy errors on dev branch: 1249
No difference in error counts

@github-actions
Copy link
Contributor

🚨 Unauthorized changes detected in protected files. Please remove these changes if they are not intended.
🚨 Some tests have failed.

@github-actions
Copy link
Contributor

Current Coverage: 99%

Mypy errors on refactor-daily-sim-2 branch: 1249
Mypy errors on dev branch: 1249
No difference in error counts

@github-actions
Copy link
Contributor

🚨 Unauthorized changes detected in protected files. Please remove these changes if they are not intended.

@github-actions
Copy link
Contributor

Current Coverage: 99%

Mypy errors on refactor-daily-sim-2 branch: 1213
Mypy errors on dev branch: 1213
No difference in error counts

@github-actions
Copy link
Contributor

🚨 Unauthorized changes detected in protected files. Please remove these changes if they are not intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SimulationEngine] Clean up _daily_simulation()

6 participants