LCOV - code coverage report
Current view: top level - models - VariableRate.cxx (source / functions) Coverage Total Hit
Test: coverage.info Lines: 97.6 % 41 40
Test Date: 2025-06-29 01:25:44 Functions: 66.7 % 12 8

            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/VariableRate.h"
      26              : #include "neml2/tensors/Scalar.h"
      27              : #include "neml2/tensors/Vec.h"
      28              : #include "neml2/tensors/SR2.h"
      29              : #include "neml2/tensors/SSR4.h"
      30              : 
      31              : namespace neml2
      32              : {
      33              : template <typename T>
      34              : OptionSet
      35            6 : VariableRate<T>::expected_options()
      36              : {
      37            6 :   OptionSet options = Model::expected_options();
      38            6 :   options.doc() = "Calculate the first order discrete time derivative of a variable as \\f$ "
      39              :                   "\\dot{f} = \\frac{f-f_n}{t-t_n} \\f$, where \\f$ f \\f$ is the force variable, "
      40              :                   "and \\f$ t \\f$ is time.";
      41              : 
      42           12 :   options.set_output("rate");
      43           12 :   options.set("rate").doc() = "The variable's rate of change";
      44              : 
      45           12 :   options.set_input("variable");
      46            6 :   options.set("variable").doc() = "The variable to take time derivative with";
      47              : 
      48           18 :   options.set_input("time") = VariableName(FORCES, "t");
      49            6 :   options.set("time").doc() = "Time";
      50              : 
      51            6 :   return options;
      52            0 : }
      53              : 
      54              : template <typename T>
      55            8 : VariableRate<T>::VariableRate(const OptionSet & options)
      56              :   : Model(options),
      57            8 :     _v(declare_input_variable<T>("variable")),
      58            8 :     _vn(declare_input_variable<T>(_v.name().old())),
      59            8 :     _t(declare_input_variable<Scalar>("time")),
      60            8 :     _tn(declare_input_variable<Scalar>(_t.name().old())),
      61           16 :     _dv_dt(options.get<VariableName>("rate").empty()
      62           18 :                ? declare_output_variable<T>(_v.name().with_suffix("_rate"))
      63           16 :                : declare_output_variable<T>("rate"))
      64              : {
      65            8 : }
      66              : 
      67              : template <typename T>
      68              : void
      69            6 : VariableRate<T>::diagnose() const
      70              : {
      71            6 :   Model::diagnose();
      72            6 :   diagnostic_assert_force(_t);
      73            6 : }
      74              : 
      75              : template <typename T>
      76              : void
      77           13 : VariableRate<T>::set_value(bool out, bool dout_din, bool /*d2out_din2*/)
      78              : {
      79           13 :   auto dv = _v - _vn;
      80           13 :   auto dt = _t - _tn;
      81              : 
      82           13 :   if (out)
      83           11 :     _dv_dt = dv / dt;
      84              : 
      85           13 :   if (dout_din)
      86              :   {
      87            5 :     auto I = T::identity_map(_v.options());
      88              : 
      89            5 :     if (_v.is_dependent())
      90            3 :       _dv_dt.d(_v) = I / dt;
      91              : 
      92            5 :     if (_vn.is_dependent())
      93            2 :       _dv_dt.d(_vn) = -I / dt;
      94              : 
      95            5 :     if (currently_solving_nonlinear_system())
      96            3 :       return;
      97              : 
      98            2 :     _dv_dt.d(_t) = -dv / dt / dt;
      99            2 :     _dv_dt.d(_tn) = dv / dt / dt;
     100            5 :   }
     101           16 : }
     102              : 
     103              : #define REGISTER(T)                                                                                \
     104              :   using T##VariableRate = VariableRate<T>;                                                         \
     105              :   register_NEML2_object(T##VariableRate);                                                          \
     106              :   template class VariableRate<T>
     107              : REGISTER(Scalar);
     108              : REGISTER(Vec);
     109              : REGISTER(SR2);
     110              : } // namespace neml2
        

Generated by: LCOV version 2.0-1