aboutsummaryrefslogtreecommitdiff
path: root/Tools/CodeGen/KrancTensor.m
blob: 13c40bfd34ea55bfb6ac0b9254e29c68cf48e6ea (plain)
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
(* ::Package:: *)

(*  Copyright 2004-2010
    Sascha Husa, Ian Hinder, Christiane Lechner, Barry Wardell

    This file is part of Kranc.

    Kranc 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.

    Kranc 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 Kranc; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*)

(****************************************************************************)
(* Wrapper providing tensor support to Kranc (from TensorTools or xTensor)  *)
(****************************************************************************)

$KrancTensorPackage = "TensorToolsKranc`";

BeginPackage["KrancTensor`", {"Errors`", "KrancThorn`", "MapLookup`", "KrancGroups`",
                              "Kranc`", $KrancTensorPackage, "ConservationCalculation`",
                              "TensorTools`", "KrancGroups`", "Differencing`"}];

CreateKrancThornTT::usage = "Construct a Kranc thorn using tensor expressions.";
CreateKrancThornTT2::usage = "Construct a Kranc thorn using tensor expressions.";

(* FIXME: Move CreateGroupFromTensor here *)

Begin["`Private`"];

(* --------------------------------------------------------------------------
   Tensor Tools
   -------------------------------------------------------------------------- *)

CreateKrancThornTT[groups_, parentDirectory_, thornName_, opts___] :=
  Module[{calcs, expCalcs, expGroups, options, derivs, expDerivs, reflectionSymmetries, declaredGroups, consCalcs, expConsCalcs},
    InfoMessage[Terse, "Creating thorn "<>thornName];
    InfoMessage[Terse, "Processing tensorial arguments"];
    calcs = lookup[{opts}, Calculations];
    consCalcs = lookupDefault[{opts}, ConservationCalculations, {}];
    derivs = lookupDefault[{opts}, PartialDerivatives, {}];
    Map[CheckCalculationTensors, calcs];
    expCalcs = Map[makeCalculationExplicit, calcs];
    expConsCalcs = Map[makeCalculationExplicit, consCalcs];

    InfoMessage[Info, "Group definitions:", groups];
    VerifyGroups[groups];

    expDerivs = Flatten[Map[ExpandComponents,derivs],1];
    expGroups = Map[makeGroupExplicit, groups];
    options = Join[DeleteCases[{opts}, Calculations -> _], {Calculations -> expCalcs}];
    options = mapReplace[options, Shorthands, ExpandComponents[lookup[options,Shorthands,{}]]];
    options = Join[DeleteCases[options, ConservationCalculations -> _],
      {ConservationCalculations -> expConsCalcs}];
    options = Join[DeleteCases[options, PartialDerivatives -> _], {PartialDerivatives -> expDerivs}];

    declaredGroups = lookupDefault[{opts}, DeclaredGroups, {}];
    odeGroups = lookupDefault[{opts}, ODEGroups, {}];
    evolutionTimelevels = lookupDefault[{opts}, EvolutionTimelevels, 3];
    defaultEvolutionTimelevels = lookupDefault[{opts}, DefaultEvolutionTimelevels, evolutionTimelevels];
    InfoMessage[Info, "Declared groups: " <> ToString[declaredGroups]];
    InfoMessage[Info, "ODE groups: " <> ToString[odeGroups]];
    InfoMessage[Terse, "Computing reflection symmetries"];
    reflectionSymmetries = computeReflectionSymmetries[declaredGroups, groups];
    InfoMessage[Info, "Reflection symmetries: ", reflectionSymmetries];

    InfoMessage[Terse, "Creating (component-based) Kranc thorn"];

    (* It is necessary to include the KrancThorn context here due to some annoying Needs[] dependency issue *)
    KrancThorn`CreateKrancThorn[expGroups, parentDirectory, thornName,
      Apply[Sequence, options], ReflectionSymmetries -> reflectionSymmetries]];

computeReflectionSymmetries[declaredGroups_, groups_] :=
  Module[{variables, syms},
    variables = variablesFromGroups[declaredGroups, groups];
    syms = Flatten[Map[ReflectionSymmetries, variables], 1];
    syms];

makeCalculationExplicit[calc_] :=
  mapValueMapMultiple[calc, 
    {Shorthands -> ExpandComponents,
     CollectList -> ExpandComponents,
     Equations -> ExpandComponents,
     PrimitiveEquations -> MakeExplicit,
     ConservedEquations -> MakeExplicit,
     Primitives -> MakeExplicit}];

(* DeleteDuplicates is not available in Mathematica before version 7 *)
deleteDuplicates[l_] :=
 Tally[Join@l][[All, 1]];

makeGroupExplicit[g_] :=
  Module[{variables, newVariables, newGroup},
    variables = groupVariables[g];
    newVariables = deleteDuplicates[ExpandComponents[variables]];
    newGroup = SetGroupVariables[g, newVariables];
    newGroup];

Options[CreateKrancThornTT2] = ThornOptions;

DefFn[CreateKrancThornTT2[thornName_String, opts:OptionsPattern[]] :=
  Module[
    {groups, pderivs, opts2, fdOrder = Global`fdOrder, PDstandard = Global`PDstandard},
    groups = Map[CreateGroupFromTensor, OptionValue[Variables]];

    pderivs =
    Join[OptionValue[PartialDerivatives],
         {
           PDstandard[i_] ->
           StandardCenteredDifferenceOperator[1,fdOrder/2,i],
           PDstandard[i_, i_] ->
           StandardCenteredDifferenceOperator[2,fdOrder/2,i],
           PDstandard[i_, j_] ->
           StandardCenteredDifferenceOperator[1,fdOrder/2,i] StandardCenteredDifferenceOperator[1,fdOrder/2,j]
         }];

    opts2 = mapReplaceAdd[{opts}, PartialDerivatives, pderivs];

    opts2 = mapReplaceAdd[opts2, IntParameters, Join[lookup[opts2,IntParameters,{}],
                                                     {{Name -> fdOrder, Default -> 2, AllowedValues -> {2, 4}}}]];

    opts2 = opts2 /. PD -> PDstandard;

    CreateKrancThornTT[groups,OptionValue[ParentDirectory],thornName,
                       DeclaredGroups -> Map[groupName, groups],
                       Sequence@@opts2]]];

End[];
EndPackage[];