https://mooseframework.inl.gov
Loading...
Searching...
No Matches
TorchFEMUtils.h
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#pragma once
11
12#ifdef MOOSE_LIBTORCH_ENABLED
13
14#include <cmath>
15#include <ATen/ATen.h>
16
25namespace TorchFEM
26{
31inline at::Tensor
32scatter(const at::Tensor & v, const at::Tensor & dof_map)
33{
34 return v.index({dof_map});
35}
36
42inline at::Tensor
43interpolate(const at::Tensor & elem_dofs, const at::Tensor & basis)
44{
45 auto ed = elem_dofs;
46 while (ed.dim() < basis.dim())
47 ed = ed.unsqueeze(-1);
48 return (ed * basis).sum(1);
49}
50
56inline at::Tensor
57assemble(const at::Tensor & scattered, const at::Tensor & dof_map, int64_t ndof)
58{
59 return at::zeros({ndof}, scattered.options())
60 .scatter_add_(0, dof_map.flatten(), scattered.flatten());
61}
62
69inline at::Tensor
70fullToMandel(const at::Tensor & full)
71{
72 const double s2 = std::sqrt(2.0);
73 auto c = [&full](int i, int j) { return full.select(-2, i).select(-1, j); };
74 return at::stack({c(0, 0), c(1, 1), c(2, 2), s2 * c(1, 2), s2 * c(0, 2), s2 * c(0, 1)}, -1);
75}
76
81inline at::Tensor
82mandelToFull(const at::Tensor & mandel)
83{
84 const double is2 = 1.0 / std::sqrt(2.0);
85 auto m = [&mandel](int k) { return mandel.select(-1, k); };
86 auto d0 = m(0), d1 = m(1), d2 = m(2);
87 auto o12 = m(3) * is2, o02 = m(4) * is2, o01 = m(5) * is2;
88 auto row0 = at::stack({d0, o01, o02}, -1);
89 auto row1 = at::stack({o01, d1, o12}, -1);
90 auto row2 = at::stack({o02, o12, d2}, -1);
91 return at::stack({row0, row1, row2}, -2);
92}
93} // namespace TorchFEM
94
95#endif // MOOSE_LIBTORCH_ENABLED
Small raw-ATen helpers for the batched finite-element assembly used by the Torch FEM kernels.
at::Tensor fullToMandel(const at::Tensor &full)
Convert a full (symmetric) rank-2 tensor to its 6-component Mandel representation,...
at::Tensor scatter(const at::Tensor &v, const at::Tensor &dof_map)
Gather global solution values into element-local DOF values: out[e,a,...] = v[dof_map[e,...
at::Tensor mandelToFull(const at::Tensor &mandel)
Inverse of fullToMandel: expand a 6-component Mandel tensor to a full symmetric (....
at::Tensor assemble(const at::Tensor &scattered, const at::Tensor &dof_map, int64_t ndof)
Scatter-add element-local contributions into a global vector: out[dof_map.flatten()[k]] += scattered....
at::Tensor interpolate(const at::Tensor &elem_dofs, const at::Tensor &basis)
Interpolate element-local DOF values onto quadrature points by contracting over the per-element DOF a...