nutofem / nuto

NuTo - yet another finite element library
https://nuto.readthedocs.io
Boost Software License 1.0
17 stars 5 forks source link

PDE Passing global values to the integrands #145

Open joergfunger opened 6 years ago

joergfunger commented 6 years ago

In a recent pull request, @vhirtham passed the global time step to the integrand using the following approach auto MomentumGradientF = std::bind(&Integrands::MomentumBalance<1>::Gradient, momentumBalance, _1, _2, std::ref(delta_t)); or auto MomentumGradientF = [&momentumBalance, &delta_t](cellData cd, cellIPData cipd){ return momentumBalance.Gradient(cd,cipd,delta_t); IMO, passing the time (delta) as reference is essentially the usage of first setting x=0 and then return f() instead of returning f(x), which makes the code difficult to read.

TTitscher commented 6 years ago

You may be looking for something like:

auto TimeDepGradient = [&](double deltaT)
{
  return [&](CellData cd, CellIpData cipd)
  {
    return momentumBalance.Gradient(cd, cipd, deltaT);
  };
};
// ...
auto _ = assembler.BuildVector(cells, TimeDepGradient(currentDeltaT));

This can be wrapped in a generic template function that returns the method above. So we end up, with a template method that returns a lambda function that itself returns a lambda function. Inception!

edit: So the user code would look like

auto TimeDepGradient = SuitableNameOfBindFunction(&MomentumBalance::Gradient, momentumBalance);
// or better?
auto TimeDepGradient = Bind(momentumBalance, &MomentumBalance::Gradient);
vhirtham commented 6 years ago

Puh... I don't know. Sometimes I have the feeling our default user is some kind of coding Dr. Jekyll / Mr. Hyde. On the one hand side, setting delta_t before using the gradient is to confusing for him, but on the other hand he is totally fine to use some template/lambda/bind magic to get his time dependent gradient function... :trollface: :trollface: :trollface:

I mean, I get your point, but I am not sure if the solution is sometimes more confusing to the "user" than the original problem.