Skip to content

Commit

Permalink
Addressed code review
Browse files Browse the repository at this point in the history
  • Loading branch information
reverendbedford committed Jan 2, 2025
1 parent 413629f commit 8ade035
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
namespace neml2
{
template <typename T>
class IncrementalRate : public Model
class IncrementToRate : public Model
{
public:
static OptionSet expected_options();

IncrementalRate(const OptionSet & options);
IncrementToRate(const OptionSet & options);

protected:
void set_value(bool out, bool dout_din, bool d2out_din2) override;
Expand All @@ -54,5 +54,8 @@ class IncrementalRate : public Model
Variable<T> & _dv_dt;
};

typedef IncrementalRate<R2> R2IncrementalRate;
typedef IncrementToRate<R2> R2IncrementToRate;
typedef IncrementToRate<Scalar> ScalarIncrementToRate;
typedef IncrementToRate<SR2> SR2IncrementToRate;
typedef IncrementToRate<Vec> VecIncrementToRate;
} // namespace neml2
7 changes: 6 additions & 1 deletion python/neml2/pyzag/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,21 @@ def _setup_parameters(self, exclude_parameters):
raise ValueError(errmsg.format(pname))
if pname in exclude_parameters:
continue
# We need to separately track the parameter names because of the reparameterization system
# What torch does when it reparamterizes models is replace the variable with a lambda method that calls the scaling function
# over some new, reparamterized, variable. If we rely on using torch.named_parameters() we will get the new "unscaled" variable, rather
# than the scaled value we want to pass to the model. Caching the names prevents this.
self.parameter_names.append(pname)
param.requires_grad_(True)
self.register_parameter(pname, torch.nn.Parameter(param.torch()))

def _update_parameter_values(self):
"""Copy over new parameter values"""
for pname in self.parameter_names:
# We may need to update the batch shapes
# See comment in _setup_parameters, using getattr here lets us reparamterize things with torch
new_value = getattr(self, pname)
current_value = self.model.get_parameter(pname).tensor()
# We may need to update the batch shape
batch_dim = new_value.dim() - current_value.base.dim()
self.model.set_parameter(pname, Tensor(new_value.clone(), batch_dim))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,30 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "neml2/models/IncrementalRate.h"
#include "neml2/models/IncrementToRate.h"

namespace neml2
{
register_NEML2_object(R2IncrementalRate);
register_NEML2_object(R2IncrementToRate);
register_NEML2_object(ScalarIncrementToRate);
register_NEML2_object(SR2IncrementToRate);
register_NEML2_object(VecIncrementToRate);

template <typename T>
OptionSet
IncrementalRate<T>::expected_options()
IncrementToRate<T>::expected_options()
{
OptionSet options = Model::expected_options();
options.doc() = "Calculate the first order discrete time derivative of a variable as \\f$ "
"\\dot{f} = \\frac{\\Delta f}{t-t_n} \\f$, where \\f$ \\Deltaf \\f$ is the "
"incremental force variable, "
"variable with the increment, "
"and \\f$ t \\f$ is time.";

options.set_output("rate");
options.set("rate").doc() = "The variable's rate of change";

options.set_input("variable");
options.set("variable").doc() = "The variable to take time derivative with";
options.set("variable").doc() = "The incremental value";

options.set_input("time") = VariableName(FORCES, "t");
options.set("time").doc() = "Time";
Expand All @@ -51,7 +54,7 @@ IncrementalRate<T>::expected_options()
}

template <typename T>
IncrementalRate<T>::IncrementalRate(const OptionSet & options)
IncrementToRate<T>::IncrementToRate(const OptionSet & options)
: Model(options),
_dv(declare_input_variable<T>("variable")),
_t(declare_input_variable<Scalar>("time")),
Expand All @@ -64,9 +67,9 @@ IncrementalRate<T>::IncrementalRate(const OptionSet & options)

template <typename T>
void
IncrementalRate<T>::set_value(bool out, bool dout_din, bool d2out_din2)
IncrementToRate<T>::set_value(bool out, bool dout_din, bool d2out_din2)
{
neml_assert(!d2out_din2, "IncrementalRate does not implement second derivatives");
neml_assert(!d2out_din2, "IncrementToRate does not implement second derivatives");

auto dt = _t - _tn;

Expand All @@ -84,5 +87,8 @@ IncrementalRate<T>::set_value(bool out, bool dout_din, bool d2out_din2)
}
}

template class IncrementalRate<R2>;
template class IncrementToRate<R2>;
template class IncrementToRate<Scalar>;
template class IncrementToRate<SR2>;
template class IncrementToRate<Vec>;
} // namespace neml2
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

[Models]
[model]
type = R2IncrementalRate
type = R2IncrementToRate
variable = 'forces/foo'
rate = 'forces/foo_rate'
[]
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/models/SR2IncrementToRate.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[Tensors]
[foo]
type = FillSR2
values = '1 2 3'
[]
[bar]
type = FillSR2
values = '5 10 15'
[]
[]

[Drivers]
[unit]
type = ModelUnitTest
model = 'model'
input_Scalar_names = 'forces/t old_forces/t'
input_Scalar_values = '1.3 1.1'
input_SR2_names = 'forces/foo'
input_SR2_values = 'foo'
output_SR2_names = 'forces/foo_rate'
output_SR2_values = 'bar'
[]
[]

[Models]
[model]
type = SR2IncrementToRate
variable = 'forces/foo'
rate = 'forces/foo_rate'
[]
[]
18 changes: 18 additions & 0 deletions tests/unit/models/ScalarIncrementToRate.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[Drivers]
[unit]
type = ModelUnitTest
model = 'model'
input_Scalar_names = 'forces/t old_forces/t forces/foo'
input_Scalar_values = '1.3 1.1 1.2'
output_Scalar_names = 'forces/foo_rate'
output_Scalar_values = '6.0'
[]
[]

[Models]
[model]
type = ScalarIncrementToRate
variable = 'forces/foo'
rate = 'forces/foo_rate'
[]
[]
31 changes: 31 additions & 0 deletions tests/unit/models/VecIncrementToRate.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[Tensors]
[foo]
type = Vec
values = '1 2 3'
[]
[bar]
type = Vec
values = '5 10 15'
[]
[]

[Drivers]
[unit]
type = ModelUnitTest
model = 'model'
input_Scalar_names = 'forces/t old_forces/t'
input_Scalar_values = '1.3 1.1'
input_Vec_names = 'forces/foo'
input_Vec_values = 'foo'
output_Vec_names = 'forces/foo_rate'
output_Vec_values = 'bar'
[]
[]

[Models]
[model]
type = VecIncrementToRate
variable = 'forces/foo'
rate = 'forces/foo_rate'
[]
[]

0 comments on commit 8ade035

Please sign in to comment.