-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mrc-4339: Add a state saver object to configure returned years #12
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cee810b
Add WIP state saver
r-ash b7e4e8a
Add state saver object and interface
r-ash 0bce467
Update standalone example to save output from every year
r-ash 3f457c2
Fix codefactor blank line complaints
r-ash 47a5d8f
Add TODO note about refactoring inputs to OutputState object
r-ash d0f62af
Return const reference to object instead of object
r-ash 98d53b9
Update function names, remove unused arg
r-ash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#pragma once | ||
|
||
#include "types.hpp" | ||
|
||
namespace leapfrog { | ||
|
||
template<typename real_type> | ||
class StateSaver { | ||
public: | ||
struct OutputState { | ||
Tensor3<real_type> total_population; | ||
Tensor3<real_type> natural_deaths; | ||
Tensor3<real_type> hiv_population; | ||
Tensor3<real_type> hiv_natural_deaths; | ||
Tensor4<real_type> hiv_strat_adult; | ||
Tensor5<real_type> art_strat_adult; | ||
Tensor1<real_type> births; | ||
Tensor4<real_type> aids_deaths_no_art; | ||
Tensor3<real_type> infections; | ||
Tensor5<real_type> aids_deaths_art; | ||
Tensor4<real_type> art_initiation; | ||
Tensor3<real_type> hiv_deaths; | ||
|
||
OutputState(int age_groups_pop, | ||
int num_genders, | ||
int disease_stages, | ||
int age_groups_hiv, | ||
int treatment_stages, | ||
int no_output_years) | ||
: total_population(age_groups_pop, num_genders, no_output_years), | ||
natural_deaths(age_groups_pop, num_genders, no_output_years), | ||
hiv_population(age_groups_pop, num_genders, no_output_years), | ||
hiv_natural_deaths(age_groups_pop, num_genders, no_output_years), | ||
hiv_strat_adult(disease_stages, age_groups_hiv, num_genders, no_output_years), | ||
art_strat_adult(treatment_stages, | ||
disease_stages, | ||
age_groups_hiv, | ||
num_genders, | ||
no_output_years), | ||
births(no_output_years), | ||
aids_deaths_no_art(disease_stages, age_groups_hiv, num_genders, no_output_years), | ||
infections(age_groups_pop, num_genders, no_output_years), | ||
aids_deaths_art(treatment_stages, disease_stages, age_groups_hiv, num_genders, no_output_years), | ||
art_initiation(disease_stages, age_groups_hiv, num_genders, no_output_years), | ||
hiv_deaths(age_groups_pop, num_genders, no_output_years) { | ||
total_population.setZero(); | ||
natural_deaths.setZero(); | ||
hiv_population.setZero(); | ||
hiv_natural_deaths.setZero(); | ||
hiv_strat_adult.setZero(); | ||
art_strat_adult.setZero(); | ||
births.setZero(); | ||
aids_deaths_no_art.setZero(); | ||
infections.setZero(); | ||
aids_deaths_art.setZero(); | ||
art_initiation.setZero(); | ||
hiv_deaths.setZero(); | ||
} | ||
}; | ||
|
||
StateSaver(int time_steps, | ||
std::vector<int> save_steps, | ||
int age_groups_pop, | ||
int num_genders, | ||
int disease_stages, | ||
int age_groups_hiv, | ||
int treatment_stages) : | ||
save_steps(save_steps), | ||
full_state(age_groups_pop, num_genders, disease_stages, age_groups_hiv, treatment_stages, save_steps.size()) { | ||
for (int step: save_steps) { | ||
if (step < 0) { | ||
std::stringstream ss; | ||
ss << "Output step must be at least 0, got '" << step << "'." << std::endl; | ||
throw std::runtime_error(ss.str()); | ||
} | ||
if (step > time_steps) { | ||
std::stringstream ss; | ||
ss << "Output step can be at most number of time steps run which is '" << time_steps << "', got step '" << step | ||
<< "'." << std::endl; | ||
throw std::runtime_error(ss.str()); | ||
} | ||
} | ||
} | ||
|
||
|
||
void save_state(const State<real_type> &state, int current_year) { | ||
for (size_t i = 0; i < save_steps.size(); ++i) { | ||
if (current_year == save_steps[i]) { | ||
full_state.total_population.chip(i, full_state.total_population.NumDimensions - 1) = state.total_population; | ||
full_state.natural_deaths.chip(i, full_state.natural_deaths.NumDimensions - 1) = state.natural_deaths; | ||
full_state.hiv_population.chip(i, full_state.hiv_population.NumDimensions - 1) = state.hiv_population; | ||
full_state.hiv_natural_deaths.chip(i, full_state.hiv_natural_deaths.NumDimensions - 1) = | ||
state.hiv_natural_deaths; | ||
full_state.hiv_strat_adult.chip(i, full_state.hiv_strat_adult.NumDimensions - 1) = state.hiv_strat_adult; | ||
full_state.art_strat_adult.chip(i, full_state.art_strat_adult.NumDimensions - 1) = state.art_strat_adult; | ||
full_state.births(i) = state.births; | ||
full_state.aids_deaths_no_art.chip(i, full_state.aids_deaths_no_art.NumDimensions - 1) = | ||
state.aids_deaths_no_art; | ||
full_state.infections.chip(i, full_state.infections.NumDimensions - 1) = state.infections; | ||
full_state.aids_deaths_art.chip(i, full_state.aids_deaths_art.NumDimensions - 1) = state.aids_deaths_art; | ||
full_state.art_initiation.chip(i, full_state.art_initiation.NumDimensions - 1) = state.art_initiation; | ||
full_state.hiv_deaths.chip(i, full_state.hiv_deaths.NumDimensions - 1) = state.hiv_deaths; | ||
return; | ||
} | ||
} | ||
} | ||
|
||
const OutputState &get_full_state() const { | ||
return full_state; | ||
} | ||
|
||
|
||
private: | ||
std::vector<int> save_steps; | ||
OutputState full_state; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
most these values, defining the dimensions, are probably subsets of the model parameters? do you want to pass in the parameters object here, or create some sub-struct within the object so that this call signature does not grow unmanageably, and also that a state saver and its underlying model run are always compatible?
Do we imagine that all of these are always wanted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes they are, so these are all the dimensions of our state-space. We've started thinking about splitting this out in https://github.com/mrc-ide/frogger/pull/11/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R51 as part of that PR I will update the names and also create some sub-struct for the parameters. If it looks ok I would wait until that PR is pinned down and use whatever struct comes from that. I'll add a TODO note here into the README