NEML2 2.0.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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
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
75template <typename... Args>
76void diagnostic_assert(bool, Args &&...);
77
80{
81public:
84
89 virtual ~DiagnosticsInterface() = default;
90
104 virtual void diagnose() const = 0;
105
107 const NEML2Object & object() const { return *_object; }
108
109private:
110 NEML2Object * _object;
111};
112} // namespace neml2
113
115// Implementation
117
118namespace neml2
119{
120template <typename... Args>
121void
122diagnostic_assert(bool assertion, Args &&... args)
123{
124 if (assertion)
125 return;
126
127 auto & state = current_diagnostic_state();
128
129 if (!state.ongoing)
130 throw NEMLException("Diagnostics are not currently being run. diagnostic_assert should only be "
131 "called inside a DiagnosticsInterface::diagnose method.");
132
133 std::ostringstream oss;
135 "In object '",
136 state.patient_name,
137 "' of type ",
138 state.patient_type,
139 ": ",
140 std::forward<Args>(args)...);
141
142 current_diagnoses().emplace_back(oss.str());
143}
144} // namespace neml2
Interface for object making diagnostics about common setup errors.
Definition DiagnosticsInterface.h:80
DiagnosticsInterface(DiagnosticsInterface &&)=delete
const NEML2Object & object() const
Get the object.
Definition DiagnosticsInterface.h:107
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:42
Definition errors.h:34
Base class of variable.
Definition Variable.h:52
void stream_all(std::ostringstream &ss, T &&val, Args &&... args)
Definition string_utils.h:64
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
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