https://mooseframework.inl.gov
Loading...
Searching...
No Matches
NEML2Utils.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#include "NEML2Utils.h"
11#include "MooseError.h"
12
13#ifdef NEML2_ENABLED
14#include "ConsoleStream.h"
15#include "neml2/csrc/aoti/log.h"
16#include <torch/script.h> // torch::jit::Module (TorchScript serialization)
17#include <cctype>
18#endif
19
20namespace NEML2Utils
21{
22#ifdef NEML2_ENABLED
23
24void
25dumpInputsToTorchScript(const std::map<std::string, at::Tensor> & inputs,
26 const std::string & filename)
27{
28 // Stash each defined input tensor (moved to host) as a buffer on a TorchScript module, then save.
29 // Buffer names must be valid identifiers, so sanitize '~'/'/' etc. to '_'.
30 torch::jit::Module mod("neml2_failed_inputs");
31 for (const auto & [name, tensor] : inputs)
32 {
33 if (!tensor.defined())
34 continue;
35 std::string key = name;
36 for (auto & c : key)
37 if (!std::isalnum(static_cast<unsigned char>(c)) && c != '_')
38 c = '_';
39 mod.register_buffer(key, tensor.detach().to(at::kCPU).contiguous());
40 }
41 mod.save(filename);
42}
43
44void
46{
47 // Capture a copy of the ConsoleStream (it binds the app-owned OutputWarehouse by reference, so
48 // the copy stays valid for the app's lifetime) and hand every NEML2 log line to it. The line
49 // already carries the [neml2:<channel>] prefix; NEML2's verbosity levels are left at their
50 // defaults.
51 neml2::aoti::log::set_sink([console](neml2::aoti::log::Level, const std::string & line)
52 { console << line << std::endl; });
53}
54
55std::string
57{
58 switch (type)
59 {
61 return "TIME";
63 return "SCALAR";
65 return "FUNCTION";
67 return "VARIABLE";
69 return "MATERIAL";
70 default:
71 mooseError("Unknown MOOSE IO type.");
72 }
73}
74#endif // NEML2_ENABLED
75
76static const std::string missing_neml2 = "The `NEML2` library is required but not enabled. Refer "
77 "to the documentation for guidance on how to enable it.";
78
79std::string
80docstring(const std::string & desc)
81{
82#ifndef NEML2_ENABLED
83 return missing_neml2 + " (Original description: " + desc + ")";
84#else
85 return desc;
86#endif
87}
88
89void
91{
92#ifndef NEML2_ENABLED
94#endif
95}
96
97} // namespace NEML2Utils
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
A helper class for re-directing output streams to Console output objects form MooseObjects.
void dumpInputsToTorchScript(const std::map< std::string, at::Tensor > &inputs, const std::string &filename)
Serialize a set of named NEML2 input tensors to a TorchScript archive at filename (a torch::jit::Modu...
Definition NEML2Utils.C:25
std::string docstring(const std::string &desc)
Augment docstring if NEML2 is not enabled.
Definition NEML2Utils.C:80
static const std::string missing_neml2
Definition NEML2Utils.C:76
void redirectLogsToConsole(const ConsoleStream &console)
Route NEML2's log output into MOOSE's console.
Definition NEML2Utils.C:45
void assertNEML2Enabled()
Assert that NEML2 is enabled.
Definition NEML2Utils.C:90
std::string stringify(MOOSEIOType type)
Definition NEML2Utils.C:56