NEML2 2.0.0
Loading...
Searching...
No Matches
DiagnosticsInterface.h
1// Copyright 2024, UChicago Argonne, LLC
2// All Rights Reserved
3// Software Name: NEML2 -- the New Engineering material Model Library, version 2
4// By: Argonne National Laboratory
5// OPEN SOURCE LICENSE (MIT)
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24
25#pragma once
26
27#include "neml2/misc/errors.h"
28#include "neml2/misc/string_utils.h"
29
30namespace neml2
31{
32// Forward decl
33class DiagnosticsInterface;
34class VariableBase;
35class NEML2Object;
36
38{
39 bool ongoing = false;
40 std::string patient_name = "";
41 std::string patient_type = "";
42
43 void reset()
44 {
45 ongoing = false;
46 patient_name.clear();
47 patient_type.clear();
48 }
49};
50
51// Guard a region when diagnostics are being performed
53{
54 Diagnosing(bool ongoing = true);
55
56 Diagnosing(const Diagnosing &) = delete;
57 Diagnosing(Diagnosing &&) = delete;
58 Diagnosing & operator=(const Diagnosing &) = delete;
61
63};
64
67
69std::vector<Diagnosis> & current_diagnoses();
70
72std::vector<Diagnosis> diagnose(const DiagnosticsInterface &);
73
76
78template <typename... Args>
79void diagnostic_assert(bool, Args &&...);
80
83{
84public:
87
92 virtual ~DiagnosticsInterface() = default;
93
107 virtual void diagnose() const = 0;
108
110 const NEML2Object & object() const { return *_object; }
111
112private:
113 NEML2Object * _object;
114};
115} // namespace neml2
116
118// Implementation
120
121namespace neml2
122{
123template <typename... Args>
124void
125diagnostic_assert(bool assertion, Args &&... args)
126{
127 if (assertion)
128 return;
129
130 auto & state = current_diagnostic_state();
131
132 if (!state.ongoing)
133 throw NEMLException("Diagnostics are not currently being run. diagnostic_assert should only be "
134 "called inside a DiagnosticsInterface::diagnose method.");
135
136 std::ostringstream oss;
138 "In object '",
139 state.patient_name,
140 "' of type ",
141 state.patient_type,
142 ": ",
143 std::forward<Args>(args)...);
144
145 current_diagnoses().emplace_back(oss.str());
146}
147} // namespace neml2
Interface for object making diagnostics about common setup errors.
Definition DiagnosticsInterface.h:83
DiagnosticsInterface(DiagnosticsInterface &&)=delete
const NEML2Object & object() const
Get the object.
Definition DiagnosticsInterface.h:110
virtual void diagnose() const =0
Check for common problems.
DiagnosticsInterface & operator=(const DiagnosticsInterface &)=delete
DiagnosticsInterface(const DiagnosticsInterface &)=delete
DiagnosticsInterface & operator=(DiagnosticsInterface &&)=delete
virtual ~DiagnosticsInterface()=default
The base class of all "manufacturable" objects in the NEML2 library.
Definition NEML2Object.h:51
Definition errors.h:34
void stream_all(std::ostringstream &ss, T &&val, Args &&... args)
Definition string_utils.h:61
Definition DiagnosticsInterface.cxx:30
std::vector< Diagnosis > diagnose(const DiagnosticsInterface &patient)
A helper function to diagnose common setup errors.
Definition DiagnosticsInterface.cxx:54
DiagnosticState & current_diagnostic_state()
Get the current diagnostic state.
Definition DiagnosticsInterface.cxx:40
void diagnostic_assert(bool, Args &&...)
Helper assertion function for diagnostics.
std::vector< Diagnosis > & current_diagnoses()
Get the current diagnoses.
Definition DiagnosticsInterface.cxx:47
void diagnose_and_throw(const DiagnosticsInterface &patient)
A helper function to diagnose common setup errors and throw an exception if any errors are found.
Definition DiagnosticsInterface.cxx:80
Definition DiagnosticsInterface.h:53
Diagnosing & operator=(Diagnosing &&)=delete
Diagnosing(bool ongoing=true)
Definition DiagnosticsInterface.cxx:31
const DiagnosticState prev_state
Definition DiagnosticsInterface.h:62
Diagnosing(Diagnosing &&)=delete
Diagnosing(const Diagnosing &)=delete
~Diagnosing()
Definition DiagnosticsInterface.cxx:37
Diagnosing & operator=(const Diagnosing &)=delete
Definition DiagnosticsInterface.h:38
bool ongoing
Definition DiagnosticsInterface.h:39
std::string patient_name
Definition DiagnosticsInterface.h:40
std::string patient_type
Definition DiagnosticsInterface.h:41
void reset()
Definition DiagnosticsInterface.h:43