@*
RK4Stepper.tmpl

Created by Graham Dennis on 2007-09-23.

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.Segments.Integrators._Stepper

@def name: RK4
@attr $ipPropagationStepFractions = ['0.5']
@attr $extraIntegrationArrayNames = ['akfield', 'aifield']
@attr $isCrossCapable = True

@attr $resetFieldName = 'aifield'

@attr $integrationOrder = 4.0

@*
  Single integration step (RK4)
*@
@def singleIntegrationStep($function)
  @#
  @set $arguments = {'_step': '_step', $propagationDimension: $propagationDimension}
${transformVectorsToBasis($integrationVectors, $homeBasis)}@slurp

// a_k = a
${copyVectors($integrationVectors, '_akfield')}@slurp

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

// a = D[a]
${callFunction('ipEvolve', arguments, _exponent = +1, parentFunction = function)}
${transformVectorsToBasis($integrationVectors, $homeBasis)}@slurp

// a_i = a
${copyVectors($integrationVectors, '_aifield')}@slurp

  @for $vector in $integrationVectors
_active_${vector.id} = _akfield_${vector.id};
  @end for
  
// a_k = G[a_k, t]
${callFunction('deltaA', arguments, parentFunction = function)}

// a_k = D[a_k]
${callFunction('ipEvolve', arguments, _exponent = +1, parentFunction = function)}
${transformVectorsToBasis($integrationVectors, $homeBasis)}@slurp

${loopOverVectorsWithInnerContentTemplate($integrationVectors, 
"""// a = a + a_k/6
_${vector.id}[$index] += _akfield_${vector.id}[$index]/6.0;
// a_k = a_i + a_k/2
_akfield_${vector.id}[$index] = _aifield_${vector.id}[$index] + 0.5*_akfield_${vector.id}[$index];
""", basis = $homeBasis)}@slurp

${propagationDimension} += 0.5*_step;
  @if $cross

${interpolateDependencies}@slurp
  @end if

// a_k = G[a_k, t + h/2]
${callFunction('deltaA', arguments, parentFunction = function)}
${transformVectorsToBasis($integrationVectors, $homeBasis)}@slurp

${loopOverVectorsWithInnerContentTemplate($integrationVectors,
"""// a = a + a_k/3
_${vector.id}[$index] += _akfield_${vector.id}[$index]/3.0;
// a_k = a_i + a_k/2
_akfield_${vector.id}[$index] = _aifield_${vector.id}[$index] + 0.5*_akfield_${vector.id}[$index];
""", basis = $homeBasis)}@slurp

// a_k = G[a_k, t + h/2]
${callFunction('deltaA', arguments, parentFunction = function)}
${transformVectorsToBasis($integrationVectors, $homeBasis)}@slurp

${loopOverVectorsWithInnerContentTemplate($integrationVectors,
"""// a = a + a_k/3
_${vector.id}[$index] += _akfield_${vector.id}[$index]/3.0;
// a_k = a_i + a_k
_akfield_${vector.id}[$index] = _aifield_${vector.id}[$index] + _akfield_${vector.id}[$index];
""", basis = $homeBasis)}@slurp

// a_k = D[a_k]
${callFunction('ipEvolve', arguments, _exponent = +1, parentFunction = function)}

${propagationDimension} += 0.5*_step;
  @if $cross

${updateDependenciesForNextStep}@slurp
  @end if

// a_k = G[a_k, t + h]
${callFunction('deltaA', arguments, parentFunction = function)}
${transformVectorsToBasis($integrationVectors, $homeBasis)}@slurp

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

// a = D[a]
${callFunction('ipEvolve', arguments, _exponent = +1, parentFunction = function)}
${transformVectorsToBasis($integrationVectors, $homeBasis)}@slurp

${loopOverVectorsWithInnerContentTemplate($integrationVectors, 
"""// a = a + a_k/6
_${vector.id}[$index] += _akfield_${vector.id}[$index]/6.0;
""", basis = $homeBasis)}@slurp
  @#
@end def


@def interpolateDependencies
  @#
  @# Insert code to interpolate the dependency vectors onto the half-step point.
  @# 
// Create 4th order midpoint interpolation for dependencies
if (_istep == 0) {
  // We are at the first point
  ${reducedFieldCopy($integrator.dependencyMap, 
"""
_active_${reducedVector.id}[_${reducedVector.id}_index_pointer + ${componentIndex}] = \\
    1.0/16.0*( 5.0*_active_${vector.id}[_${vector.id}_index_pointer + ${componentIndex}]
            + 15.0*_active_${vector.id}[_${vector.id}_index_pointer + ${componentIndex} + ${skipSize} * _${vector.id}_ncomponents]
            -  5.0*_active_${vector.id}[_${vector.id}_index_pointer + ${componentIndex} + 2 * ${skipSize} * _${vector.id}_ncomponents]
            +      _active_${vector.id}[_${vector.id}_index_pointer + ${componentIndex} + 3 * ${skipSize} * _${vector.id}_ncomponents]);
"""), autoIndent=True}@slurp
} else if (_istep == (${integrator.stepCount} - 1)) {
  // We are at the last point
  ${reducedFieldCopy($integrator.dependencyMap, 
"""
_active_${reducedVector.id}[_${reducedVector.id}_index_pointer + ${componentIndex}] = \\
    1.0/16.0*(15.0*_active_${vector.id}[_${vector.id}_index_pointer + ${componentIndex}]
            +  5.0*_active_${vector.id}[_${vector.id}_index_pointer + ${componentIndex} + ${skipSize} * _${vector.id}_ncomponents]
            -  5.0*_active_${vector.id}[_${vector.id}_index_pointer + ${componentIndex} - ${skipSize} * _${vector.id}_ncomponents]
            +      _active_${vector.id}[_${vector.id}_index_pointer + ${componentIndex} - 2 * ${skipSize} * _${vector.id}_ncomponents]);
"""), autoIndent=True}@slurp
} else {
  // We are somewhere in the middle
  ${reducedFieldCopy($integrator.dependencyMap, 
"""
_active_${reducedVector.id}[_${reducedVector.id}_index_pointer + ${componentIndex}] = \\
    9.0/16.0*(_active_${vector.id}[_${vector.id}_index_pointer + ${componentIndex}]
            + _active_${vector.id}[_${vector.id}_index_pointer + ${componentIndex} + ${skipSize} * _${vector.id}_ncomponents])
  - 1.0/16.0*(_active_${vector.id}[_${vector.id}_index_pointer + ${componentIndex} - ${skipSize} * _${vector.id}_ncomponents]
            + _active_${vector.id}[_${vector.id}_index_pointer + ${componentIndex} + 2 * ${skipSize} * _${vector.id}_ncomponents]);
"""), autoIndent=True}@slurp
}
  @#
@end def
