Line data Source code
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 : #include "neml2/base/DiagnosticsInterface.h"
26 : #include "neml2/base/NEML2Object.h"
27 : #include "neml2/misc/assertions.h"
28 :
29 : namespace neml2
30 : {
31 291 : Diagnosing::Diagnosing(bool ongoing)
32 291 : : prev_state(current_diagnostic_state())
33 : {
34 291 : current_diagnostic_state().ongoing = ongoing;
35 291 : }
36 :
37 291 : Diagnosing::~Diagnosing() { current_diagnostic_state() = prev_state; }
38 :
39 : DiagnosticState &
40 1362 : current_diagnostic_state()
41 : {
42 1362 : static DiagnosticState _diagnostic_state;
43 1362 : return _diagnostic_state;
44 : }
45 :
46 : std::vector<Diagnosis> &
47 492 : current_diagnoses()
48 : {
49 492 : static std::vector<Diagnosis> _diagnoses;
50 492 : return _diagnoses;
51 : }
52 :
53 : std::vector<Diagnosis>
54 291 : diagnose(const DiagnosticsInterface & patient)
55 : {
56 291 : auto & state = current_diagnostic_state();
57 291 : state.patient_name = patient.object().name();
58 291 : state.patient_type = patient.object().type();
59 :
60 : // Run diagnostics
61 : {
62 291 : Diagnosing guard;
63 291 : patient.diagnose();
64 291 : }
65 :
66 : // Get the new diagnoses
67 291 : auto new_diagnoses = current_diagnoses();
68 :
69 : // Reset to a clean state before returning
70 291 : if (!state.ongoing)
71 : {
72 196 : current_diagnoses().clear();
73 196 : state.reset();
74 : }
75 :
76 291 : return new_diagnoses;
77 0 : }
78 :
79 : void
80 193 : diagnose_and_throw(const DiagnosticsInterface & patient)
81 : {
82 193 : neml_assert(
83 193 : !current_diagnostic_state().ongoing,
84 : "diagnose_and_throw cannot be called inside a DiagnosticsInterface::diagnose method.");
85 193 : auto diagnoses = diagnose(patient);
86 193 : if (!diagnoses.empty())
87 : {
88 1 : std::ostringstream oss;
89 1 : oss << "Diagnostics for object '" << patient.object().name() << "':\n";
90 2 : for (const auto & d : diagnoses)
91 1 : oss << " - " << d.what() << '\n';
92 1 : throw Diagnosis(oss.str());
93 1 : }
94 193 : }
95 :
96 673 : DiagnosticsInterface::DiagnosticsInterface(NEML2Object * object)
97 673 : : _object(object)
98 : {
99 673 : }
100 : } // namespace neml2
|