1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
@shBang #!/usr/bin/env python3
@*
Operator.tmpl
Created by Graham Dennis on 2007-10-13.
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._Operator
@from xpdeint.Vectors.VectorElement import VectorElement
@from xpdeint.CallOnceGuards import callOncePerInstanceGuard
@def insideEvaluateOperatorLoops
@#
@# Content must be provided by a subclass
@assert 0
@#
@end def
@@callOncePerInstanceGuard
@def initialise
@#
@super
@#
@if $operatorVector
_${parent.id}_calculate_${name}_field();
@end if
@#
@end def
@def calculateOperatorFieldFunctionContents($function)
@#
${codeBlocks['operatorDefinition'].loop(self.insideCalculateOperatorFieldLoops)}@slurp
@#
@end def
@def insideCalculateOperatorFieldLoopsBegin
@#
@for $operatorComponentName in $operatorComponents.keys()
@if $operatorVector and operatorComponentName in $operatorVector.components
@continue
@end if
${operatorVector.type} $operatorComponentName;
@end for
@#
@end def
@attr $evaluateOperatorFunctionArgument = 'real _step'
@def evaluateOperatorFunctionContents($function)
@#
@if not 'calculateOperatorField' in $functions
${evaluateOperatorFunctionContentsWithCodeBlock(function)}@slurp
@else
${evaluateOperatorFunctionContentsWithoutCodeBlock(function)}@slurp
@end if
@#
@end def
@def evaluateOperatorFunctionContentsWithCodeBlock($function)
@#
${callEvaluateLoop}@slurp
@#
@if $resultVector and $resultVector.needsTransforms
_${resultVector.id}_basis = $basisIndexForBasis($operatorBasis);
@end if
@#
@end def
@def callEvaluateLoop
${codeBlocks['operatorDefinition'].loop(self.insideEvaluateOperatorLoops)}@slurp
@end def
@def insideEvaluateOperatorLoopsBegin
@#
@if not $operatorVector
@# If we don't have an operator vector, then we need to create
@# the component variables so that the user can set them inside
@# the loop
@for $operatorComponentName, $operatorComponentDictionary in $operatorComponents.items()
@set $typeNamesSet = set([vector.type for vector in operatorComponentDictionary.keys()])
@set $typeName = 'real'
@if 'complex' in typeNamesSet
@set $typeName = 'complex'
@end if
${typeName} ${operatorComponentName};
@end for
@end if
@#
@end def
@def evaluateOperatorFunctionContentsWithoutCodeBlock($function)
@#
@# We need to loop over all of the vectors to which we are applying our operators.
@#
${transformVectorsToBasis($targetVectors, $operatorBasis)}@slurp
@#
@# We have an operator vector and we will need to loop over it
@set $setOfVectorsToLoopOver = $targetVectors.copy()
@silent $setOfVectorsToLoopOver.add($operatorVector)
@if $resultVector
@silent $setOfVectorsToLoopOver.add($resultVector)
@end if
@#
${evaluateOperatorLoop(setOfVectorsToLoopOver)}@slurp
@#
@if $resultVector and $resultVector.needsTransforms
_${resultVector.id}_basis = $basisIndexForBasis($operatorBasis);
@end if
@#
@end def
@def optimisedOperatorCopyBegin
@# This optimisation only works in a restricted set of circumstances. It's common enough, but not as general
@# as it could be. Basically, you need a single operator name (e.g. L, not Lx and Ly) which is applied to all
@# components of a vector.
@if len($targetVectors) != 1 or not $resultVector or len($operatorNames) != 1
@return
@end if
@set targetVector = $anyObject($targetVectors)
@if $targetVector.nComponents != $resultVector.nComponents or not targetVector.needsTransforms
@return
@end if
@# For this operator, we don't modify the target vector, so if we just transform it, we'll have to transform it back again
@# Here, we copy the target vector into the result vector because it's the same size. This way we can avoid the second transform
memcpy(_active_${resultVector.id}, _active_${targetVector.id}, ${targetVector.allocSize} * sizeof(${targetVector.type}));
${targetVector.type} *__backup_ptr = _active_${targetVector.id};
ptrdiff_t __backup_basis = _${targetVector.id}_basis;
_active_${targetVector.id} = _active_${resultVector.id};
@#
@end def
@def optimisedOperatorCopyEnd
@if len($targetVectors) != 1 or not $resultVector or len($operatorNames) != 1
@return
@end if
@set targetVector = $anyObject($targetVectors)
@if $targetVector.nComponents != $resultVector.nComponents or not targetVector.needsTransforms
@return
@end if
_active_${targetVector.id} = __backup_ptr;
_${targetVector.id}_basis = __backup_basis;
@#
@end def
|