@*
SIStepper.tmpl

Created by Joe Hope on 2008-03-24.

Copyright (c) 2008-2012, Joe Hope and 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.Segments.Integrators._Stepper

@def name: SI
@attr $ipPropagationStepFractions = ['0.5']
@attr $extraIntegrationArrayNames = ['oldCopy']
@attr $isCrossCapable = True
@attr $iterations = 3

@*
  Single integration step (SI)
*@
@def singleIntegrationStep($function)
  @#
  @set $arguments = {'_step': '_step', $propagationDimension: $propagationDimension}
${callFunction('nonconstantIPFields', arguments, _exponent = 1, parentFunction=function)}

${callFunction('ipEvolve', arguments, _exponent = +1, parentFunction=function)}

${propagationDimension} += 0.5*_step;

${transformVectorsToBasis($integrationVectors, $homeBasis)}@slurp

${copyVectors($integrationVectors, '_oldCopy')}@slurp

  @for $vector in $integrationVectors
_active_${vector.id} = _${vector.id};
  @end for

  @if $cross
// Interpolate dependencies to the half-step
${interpolateDependencies}@slurp
  @end if

for (int _iteration = 0; _iteration < ${iterations}; _iteration++) {
  if (_iteration < ${iterations} - 1) {
    ${callFunction('deltaA', arguments, _step = '0.5*_step', parentFunction=function), autoIndent=True}@slurp
  } else {
    ${callFunction('deltaA', arguments, parentFunction=function), autoIndent=True}@slurp
  }
  
  ${transformVectorsToBasis($integrationVectors, $homeBasis), autoIndent=True}@slurp
  
  ${loopOverVectorsWithInnerContentTemplate($integrationVectors,
"""// a = oldCopy + a
_${vector.id}[$index] += _oldCopy_${vector.id}[$index];
""", basis=$homeBasis), autoIndent=True}@slurp
}

${callFunction('ipEvolve', arguments, _exponent = +1, parentFunction=function)}

${propagationDimension} += 0.5*_step;

  @#
@end def

@def interpolateDependencies
  @#
  @# Insert code to interpolate the dependency vectors onto the half-step point.
  @# 
${reducedFieldCopy($integrator.dependencyMap, 
"""
_active_${reducedVector.id}[_${reducedVector.id}_index_pointer + ${componentIndex}] = \\
    0.5*(_active_${vector.id}[_${vector.id}_index_pointer + ${componentIndex}]
       + _active_${vector.id}[_${vector.id}_index_pointer + ${componentIndex} + ${skipSize} * _${vector.id}_ncomponents]);
""")}@slurp
  @#
@end def
