LCOV - code coverage report
Current view: top level - models/solid_mechanics - Normality.cxx (source / functions) Coverage Total Hit
Test: coverage.info Lines: 92.9 % 42 39
Test Date: 2025-06-29 01:25:44 Functions: 100.0 % 3 3

            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/models/solid_mechanics/Normality.h"
      26              : #include "neml2/base/guards.h"
      27              : #include "neml2/misc/assertions.h"
      28              : 
      29              : namespace neml2
      30              : {
      31              : register_NEML2_object(Normality);
      32              : 
      33              : OptionSet
      34            2 : Normality::expected_options()
      35              : {
      36            2 :   OptionSet options = Model::expected_options();
      37            2 :   options.doc() = "Store the first derivatives of a scalar-valued function in given variables, "
      38            2 :                   "i.e. \\f$ u_i = \\dfrac{f(\\boldsymbol{v})}{v_i} \\f$.";
      39              : 
      40            4 :   options.set<std::string>("model");
      41            4 :   options.set("model").doc() = "The model which evaluates the scalar-valued function";
      42              : 
      43            4 :   options.set<VariableName>("function");
      44            4 :   options.set("function").doc() = "Function to take derivative";
      45              : 
      46            4 :   options.set<std::vector<VariableName>>("from");
      47            4 :   options.set("from").doc() = "Function arguments to take derivatives w.r.t.";
      48              : 
      49            4 :   options.set<std::vector<VariableName>>("to");
      50            2 :   options.set("to").doc() = "Variables to store the first derivatives";
      51              : 
      52            2 :   return options;
      53            0 : }
      54              : 
      55            4 : Normality::Normality(const OptionSet & options)
      56              :   : Model(options),
      57            8 :     _model(register_model(options.get<std::string>("model"))),
      58           12 :     _f(options.get<VariableName>("function"))
      59              : {
      60              :   // Set up the conjugate pairs
      61            8 :   const auto from = options.get<std::vector<VariableName>>("from");
      62            4 :   const auto to = options.get<std::vector<VariableName>>("to");
      63            4 :   neml_assert(from.size() == to.size(),
      64              :               "The conjugate pairs should have a one-to-one correspondance. ",
      65            4 :               from.size(),
      66              :               " variables are being mapped to ",
      67            4 :               to.size(),
      68              :               " variables.");
      69              : 
      70              :   // Declare output variables
      71           11 :   for (size_t i = 0; i < from.size(); i++)
      72            7 :     _conjugate_pairs.emplace(from[i], clone_output_variable(_model.input_variable(from[i]), to[i]));
      73            4 : }
      74              : 
      75              : void
      76            8 : Normality::set_value(bool out, bool dout_din, bool /*d2out_din2*/)
      77              : {
      78              :   {
      79            8 :     SolvingNonlinearSystem guard(false);
      80              :     // Since normality maps the derivatives to the output variables, we need to evaluate the
      81              :     // sub-model's derivatives if normality asks for output variables, and evaluate the sub-model's
      82              :     // second derivatives if normality asks for derivatives of output variables.
      83            8 :     _model.forward_maybe_jit(false, out, dout_din);
      84            8 :   }
      85              : 
      86            8 :   const auto & fvar = _model.output_variable(_f);
      87           22 :   for (auto && [iname, ivar] : _conjugate_pairs)
      88              :   {
      89           14 :     if (!fvar.derivatives().count(iname))
      90              :     {
      91            0 :       (*ivar) = Tensor::zeros(ivar->base_sizes(), fvar.options());
      92            0 :       continue;
      93              :     }
      94              : 
      95           14 :     if (out)
      96           11 :       (*ivar) = fvar.derivatives().at(iname).base_reshape(ivar->base_sizes());
      97              : 
      98           14 :     if (dout_din)
      99           24 :       for (auto && [jname, jvar] : _model.input_variables())
     100           28 :         if (jvar->is_dependent() && fvar.second_derivatives().count(iname) &&
     101           11 :             fvar.second_derivatives().at(iname).count(jname))
     102            8 :           ivar->d(*jvar) = fvar.second_derivatives().at(iname).at(jname);
     103              :   }
     104            8 : }
     105              : } // namespace neml2
        

Generated by: LCOV version 2.0-1