@*
NonConstantIPOperator.tmpl

Interaction-picture transverse derivative operator

Created by Graham Dennis on 2007-11-21.

Copyright (c) 2007-2012, Graham Dennis

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*@
@extends xpdeint.Operators._IPOperator

@from xpdeint.CallOnceGuards import callOncePerInstanceGuard

@attr $calculateOperatorFieldFunctionArguments = [('real', '_step'), ('int', '_exponent')]

@def globals
  @#
  @super
  @#
real _${id}_last_timestep_size_map[${len($integrator.ipPropagationStepFractions)}];
  @#
@end def

@@callOncePerInstanceGuard
@def initialise
  @#
memset(_${id}_last_timestep_size_map, 0, sizeof(_${id}_last_timestep_size_map));
  @#
@end def

@def calculateOperatorFieldFunctionContents($function)
  @#
  @if len($integrator.ipPropagationStepFractions) > 1
static const real _propagationStepFractions[] = {
  @for $propagationStepFraction in $integrator.ipPropagationStepFractions
  $propagationStepFraction,
  @end for
};
const long _arrayIndex = _exponent - 1;
const real _propagationStepFraction = _propagationStepFractions[_arrayIndex];
  @else
const real _propagationStepFraction = ${integrator.ipPropagationStepFractions[0]};
const long _arrayIndex = 0;
  @end if

// If the timestep hasn't changed from the last time, then we're done.
if (_propagationStepFraction * _step == _${id}_last_timestep_size_map[_arrayIndex])
  return;

  @#
  @super(function)
  @#

_${id}_last_timestep_size_map[_arrayIndex] = _propagationStepFraction * _step;
  @#
@end def

@def insideCalculateOperatorFieldLoops($codeString)
  @#
${insideCalculateOperatorFieldLoopsBegin}@slurp
  @#
  @# We expect the integrator to have defined '_step'
  @#
// The purpose of the following define is to give a (somewhat helpful) compile-time error
// if the user has attempted to use the propagation dimension variable in a constant IP operator/
// The user probably shouldn't be doing this, but if they must, they should use a non-constant EX
// operator instead
#define ${propagationDimension} Dont_use_propagation_dimension_${propagationDimension}_in_constant_IP_operator___Use_non_constant_EX_operator_instead
// ************** Operator code *****************
${codeString}@slurp
// **********************************************
#undef ${propagationDimension}

  @# Loop over each operator component
  @for $operatorComponentNumber, $operatorComponent in enumerate($operatorComponents.iterkeys())
_${operatorVector.id}[_${operatorVector.id}_index_pointer + _arrayIndex * $len($operatorComponents) + ${operatorComponentNumber}]@slurp
 = ${expFunction}(${operatorComponent}${valueSuffix} * _propagationStepFraction * _step);
  @end for
  @#
@end def

@def exponentIndex
  @#
(abs(_exponent) - 1) * ${len($operatorComponents)}@slurp
  @#
@end def
