Skip to content

Commit

Permalink
rev: remove previous timestep caching in lumped forcings engine provider
Browse files Browse the repository at this point in the history
  • Loading branch information
program-- committed May 21, 2024
1 parent 283282a commit c0d44f0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
12 changes: 4 additions & 8 deletions include/forcing/ForcingsEngineLumpedDataProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,23 @@ struct ForcingsEngineLumpedDataProvider
*
* @param divide_id Divide ID to index at
* @param variable Forcings variable to get
* @param previous If true, return the previous timestep values.
* @return double
*/
double at(
const std::string& divide_id,
const std::string& variable,
bool previous = false
const std::string& variable
);

/**
* @brief Get a forcing value from the instance
*
* @param divide_index Divide index
* @param variable_index Variable index
* @param previous If true, return the previous timestep values.
* @return double
*/
double at(
std::size_t divide_idx,
std::size_t variable_idx,
bool previous = false
std::size_t variable_idx
);

/**
Expand All @@ -95,15 +91,15 @@ struct ForcingsEngineLumpedDataProvider
std::unordered_map<int, int> var_divides_{};

/**
* Values are stored indexed on (2, divide_id, variable),
* Values are stored indexed on (divide_id, variable),
* such that the structure can be visualized as:
*
* Divide ID : || D0 | D1 || D0 ...
* Variable : || V1 V2 V3 | V1 V2 V3 || V1 ...
* Value : || 9 11 31 | 3 4 5 || 10 ...
*
* Some notes for future reference:
* - Time complexity to update is approximately O(2*V*D) = O(V*D),
* - Time complexity to update is approximately O(V*D),
* where V is the number of variables and D is the number of divides.
* In general, D will dominate and, V will be some small constant amount.
*/
Expand Down
20 changes: 7 additions & 13 deletions src/forcing/ForcingsEngineLumpedDataProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Provider::ForcingsEngineLumpedDataProvider(
var_divides_[id] = i;
}

var_cache_ = decltype(var_cache_){{ 2, id_dim, var_dim }};
var_cache_ = decltype(var_cache_){{ id_dim, var_dim }};

// Cache initial iteration
update_value_storage_();
Expand Down Expand Up @@ -140,18 +140,14 @@ void Provider::update_value_storage_()
};

for (std::size_t di = 0; di < size; di++) {
// Move 0 time (current) to 1 (previous), and set the
// current values from var_span
var_cache_.at({{1, di, vi}}) = var_cache_.at({{0, di, vi}});
var_cache_.at({{0, di, vi}}) = var_span[di];
var_cache_.at({{di, vi}}) = var_span[di];
}
}
}

double Provider::at(
const std::string& divide_id,
const std::string& variable,
bool previous
const std::string& variable
)
{
const auto d_idx = divide_index(divide_id);
Expand All @@ -174,13 +170,12 @@ double Provider::at(
};
}

return at(d_idx, v_idx, previous);
return at(d_idx, v_idx);
}

double Provider::at(
std::size_t divide_idx,
std::size_t variable_idx,
bool previous
std::size_t variable_idx
)
{

Expand All @@ -190,8 +185,7 @@ double Provider::at(
std::to_string(shape[1]) + ", " +
std::to_string(shape[2]);

const auto index_str = (previous ? "1" : "0") + std::string{", "} +
(divide_idx == bad_index ? "?" : std::to_string(divide_idx)) + ", " +
const auto index_str = (divide_idx == bad_index ? "?" : std::to_string(divide_idx)) + ", " +
(variable_idx == bad_index ? "?" : std::to_string(variable_idx));

throw std::out_of_range{
Expand All @@ -200,7 +194,7 @@ double Provider::at(
};
}

return var_cache_.at({{previous ? 1U : 0U, divide_idx, variable_idx}});
return var_cache_.at({{divide_idx, variable_idx}});
}

double Provider::get_value(
Expand Down

0 comments on commit c0d44f0

Please sign in to comment.