-
Notifications
You must be signed in to change notification settings - Fork 8
Use relative errors by default when comparing output files #449
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
Open
PhilipFackler
wants to merge
1
commit into
develop
Choose a base branch
from
PhilipFackler/rel-errors
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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 |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| #include <cmath> | ||
| #include <format> | ||
| #include <limits> | ||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| #include <GridKit/Testing/OutputAtTime.hpp> | ||
|
|
@@ -11,96 +13,196 @@ namespace GridKit | |
| namespace Testing | ||
| { | ||
|
|
||
| inline constexpr double DEFAULT_ABS_ERROR_THRESHOLD = | ||
| std::numeric_limits<double>::epsilon(); | ||
|
|
||
| /** | ||
| * @brief Aggregate error data for a single variable | ||
| * @brief Aggregate norm data for a single variable over time | ||
| */ | ||
| struct ErrorAggregate | ||
| struct TemporalNormAggregate | ||
| { | ||
| std::string label; | ||
| double max_error{0.0}; | ||
| double max_error_time{0.0}; | ||
| double error_norm_L2{0.0}; | ||
| double max{0.0}; | ||
| double max_time{0.0}; | ||
| double L2{0.0}; | ||
|
Comment on lines
+25
to
+27
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert to more verbose names and add comments explaining what each variable represents. Also naming something |
||
|
|
||
| void push(double err, double time) | ||
| { | ||
| if (err > max_error) | ||
| if (err > max) | ||
| { | ||
| max_error = err; | ||
| max_error_time = time; | ||
| max = err; | ||
| max_time = time; | ||
| } | ||
| error_norm_L2 += err * err; | ||
| L2 += err * err; | ||
| } | ||
|
|
||
| void wrap() | ||
| { | ||
| error_norm_L2 = std::sqrt(error_norm_L2); | ||
| L2 = std::sqrt(L2); | ||
| } | ||
|
|
||
| void scale(const TemporalNormAggregate& ref, double threshold) | ||
| { | ||
| if (ref.max > threshold) | ||
| { | ||
| max /= ref.max; | ||
| } | ||
| if (ref.L2 > threshold) | ||
| { | ||
| L2 /= ref.L2; | ||
| } | ||
| } | ||
|
|
||
| std::ostream& display( | ||
| std::ostream& os = std::cout, const std::string& indent = " ") const | ||
| { | ||
| os << indent << label << ":\n" | ||
| << indent << indent << "max : " | ||
| << std::format("{:.6e} (at time {:.3e})", max_error, max_error_time) | ||
| << std::format("{:.6e} (at time {:.3e})", max, max_time) | ||
| << '\n' | ||
| << indent << indent << "L2-norm : " | ||
| << std::format("{:.6e}", error_norm_L2) << '\n'; | ||
| << std::format("{:.6e}", L2) << '\n'; | ||
| return os; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * @brief A set of ErrorAggregate for each variable plus a total (combined) | ||
| * ErrorAggregate | ||
| * | ||
| * @note The "total" aggregate is based on the L-infinity norm of the local | ||
| * error of variables at a given time step. | ||
| */ | ||
| struct ErrorSet | ||
| { | ||
| ErrorAggregate total{"Total"}; | ||
| std::vector<ErrorAggregate> vars{}; | ||
| TemporalNormAggregate total_error{"Total"}; | ||
| std::vector<TemporalNormAggregate> var_errors{}; | ||
|
|
||
| template <typename C> | ||
| explicit ErrorSet(const C& labels) | ||
| : vars(std::size(labels)) | ||
| : var_errors(std::size(labels)) | ||
| { | ||
| for (std::size_t i = 0; i < std::size(labels); ++i) | ||
| { | ||
| vars[i].label = labels[i]; | ||
| var_errors[i].label = labels[i]; | ||
| } | ||
| } | ||
|
|
||
| void push(const OutputAtTime& err) | ||
| virtual ~ErrorSet() | ||
| { | ||
| for (std::size_t i = 0; i < vars.size(); ++i) | ||
| { | ||
| vars[i].push(std::abs(err.data[i]), err.t); | ||
| } | ||
| total.push(lInfNorm(err), err.t); | ||
| } | ||
|
|
||
| void wrap() | ||
| virtual void wrap() | ||
| { | ||
| for (auto& agg : vars) | ||
| for (auto& agg : var_errors) | ||
| { | ||
| agg.wrap(); | ||
| } | ||
| total.wrap(); | ||
| total_error.wrap(); | ||
| } | ||
|
|
||
| virtual void push(const OutputAtTime&, const OutputAtTime&) = 0; | ||
|
|
||
| std::ostream& display(std::ostream& os = std::cout) const | ||
| { | ||
| std::string indent{" "}; | ||
| os << "Error Set:\n"; | ||
| for (const auto& var : vars) | ||
| for (const auto& var : var_errors) | ||
| { | ||
| var.display(os, indent); | ||
| } | ||
| total.display(os, indent); | ||
| total_error.display(os, indent); | ||
| return os; | ||
| } | ||
| }; | ||
|
|
||
| enum class ErrorType | ||
| { | ||
| RELATIVE, | ||
| ABSOLUTE | ||
| }; | ||
|
|
||
| struct RelativeError; | ||
| struct AbsoluteError; | ||
|
|
||
| /** | ||
| * @brief A set of TemporalNormAggregate for the error in each variable plus | ||
| * a TemporalNormAggregate representing total (combined) error | ||
| * | ||
| * @note The "total_error" aggregate is based on the L-infinity norm of the local | ||
| * error of variables at a given time step. | ||
| */ | ||
| template <typename error_type = RelativeError> | ||
| struct ErrorSetImpl; | ||
|
|
||
| template <> | ||
| struct ErrorSetImpl<RelativeError> : ErrorSet | ||
| { | ||
| template <typename C> | ||
| explicit ErrorSetImpl( | ||
| const C& labels, | ||
| double abs_threshold = DEFAULT_ABS_ERROR_THRESHOLD) | ||
| : ErrorSet(labels), | ||
| abs_threshold_(abs_threshold), | ||
| ref_norms_(std::size(labels)) | ||
| { | ||
| } | ||
|
|
||
| void push(const OutputAtTime& test, const OutputAtTime& ref) override | ||
| { | ||
| auto err = test - ref; | ||
| for (std::size_t i = 0; i < var_errors.size(); ++i) | ||
| { | ||
| var_errors[i].push(std::abs(err.data[i]), err.t); | ||
| ref_norms_[i].push(std::abs(ref.data[i]), ref.t); | ||
| } | ||
| total_error.push(lInfNorm(err), err.t); | ||
| ref_total_norm_.push(lInfNorm(ref), ref.t); | ||
| } | ||
|
|
||
| void wrap() override | ||
| { | ||
| ErrorSet::wrap(); | ||
| for (std::size_t i = 0; i < var_errors.size(); ++i) | ||
| { | ||
| ref_norms_[i].wrap(); | ||
| var_errors[i].scale(ref_norms_[i], abs_threshold_); | ||
| } | ||
| ref_total_norm_.wrap(); | ||
| total_error.scale(ref_total_norm_, abs_threshold_); | ||
| } | ||
|
|
||
| private: | ||
| double abs_threshold_{}; | ||
| TemporalNormAggregate ref_total_norm_{}; | ||
| std::vector<TemporalNormAggregate> ref_norms_{}; | ||
| }; | ||
|
|
||
| template <> | ||
| struct ErrorSetImpl<AbsoluteError> : ErrorSet | ||
| { | ||
| using ErrorSet::ErrorSet; | ||
|
|
||
| void push(const OutputAtTime& test, const OutputAtTime& ref) override | ||
| { | ||
| auto err = test - ref; | ||
| for (std::size_t i = 0; i < var_errors.size(); ++i) | ||
| { | ||
| var_errors[i].push(std::abs(err.data[i]), err.t); | ||
| } | ||
| total_error.push(lInfNorm(err), err.t); | ||
| } | ||
| }; | ||
|
|
||
| template <typename C> | ||
| std::unique_ptr<ErrorSet> makeErrorSet( | ||
| ErrorType type, | ||
| const C& labels, | ||
| double abs_threshold = DEFAULT_ABS_ERROR_THRESHOLD) | ||
| { | ||
| switch (type) | ||
| { | ||
| case ErrorType::RELATIVE: | ||
| return std::make_unique<ErrorSetImpl<RelativeError>>(labels, abs_threshold); | ||
| case ErrorType::ABSOLUTE: | ||
| return std::make_unique<ErrorSetImpl<AbsoluteError>>(labels); | ||
| default: | ||
| throw std::runtime_error("Invalid error type"); | ||
| } | ||
| } | ||
|
|
||
| } // namespace Testing | ||
| } // namespace GridKit | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.