aboutsummaryrefslogtreecommitdiff
path: root/Tools/CodeGen/Jacobian.m
blob: ea74e0539120087b9f69c8174b90db4f4c2a78db (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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
(*  Copyright 2011 Ian Hinder

    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
*)

BeginPackage["Jacobian`", {"Errors`", "Helpers`", "Kranc`", "Differencing`", "MapLookup`",
                           "CodeGen`", "CodeGenC`", "KrancGroups`"}];

JacobianQ;
InsertJacobian;
CreateJacobianVariables;
JacobianGenericFDParameters;
JacobianSymbols;
JacobianGroups;
JacobianCheckGroups;
JacobianConditionalGridFunctions;

Begin["`Private`"];

Options[JacobianQ] = ThornOptions;
JacobianQ[opts:OptionsPattern[]] :=
  Length[OptionValue[Jacobian]] > 0;

(* Assign a shorthand containing the Jacobian multiplied by the passed
   1st derivative *)
jacobianShorthand[d:(deriv_[var_, i_])] :=
  Module[{},
     derivToJacDeriv[d] ->
      IfThen["use_jacobian", Sum[Symbol["J"<>ToString[j]<>ToString[i]] deriv[var, j], {j, 1 3}], deriv[var, i]]
  ];

(* Assign a shorthand containing the Jacobian multiplied by the passed
   2nd derivative *)
jacobianShorthand[d:(deriv_[var_, i_,j_])] :=
  Module[{ip,jp},
     {ip,jp} = Sort[{i,j}]; (* dJ is symmetric in the last two indices *)
     derivToJacDeriv[d] ->
      IfThen["use_jacobian", Sum[Symbol["dJ"<>ToString[a]<>ToString[ip]<>ToString[jp]] deriv[var, a], {a, 1 3}] + 
      Sum[Symbol["J"<>ToString[a]<>ToString[i]] Symbol["J"<>ToString[b]<>ToString[j]] deriv[var, a, b], {a, 1 3}, {b, 1, 3}],
      deriv[var, i, j]]
  ];

(* Convert a 1st derivative to a Jacobian-multiplied derivative *)
derivToJacDeriv[deriv_[var_, i_]] :=
  Symbol["Global`Jac"<>ToString[deriv]<>ToString[i]<>ToString[var]];

(* Convert a 2nd derivative to a Jacobian-multiplied derivative *)
derivToJacDeriv[deriv_[var_, i_, j_]] :=
  Symbol["Global`Jac"<>ToString[deriv]<>ToString[i]<>ToString[j]<>ToString[var]];

shorthandsInDerivDef[def_, shorthands_] :=
  Module[{allAtoms},
    allAtoms = Union[Level[def, {-1}]];
    Intersection[shorthands, allAtoms]];

(* Insert a Jacobian shorthand definition into a list of equations
   after all the shorthands that it uses *)
insertDerivInEqs[deriv_, defs_, eqs_, shorthands_, zeroDims_] :=
  Module[{shortsUsed, lhss, positions, positions2, position, newShort, derivsInShort, defsUsed},
    newShort = jacobianShorthand[deriv]; (* Definition expression for new shorthand *)
    derivsInShort = GridFunctionDerivativesInExpression[defs, newShort, zeroDims]; (* Derivatives needed *)
    defsUsed = Map[GridFunctionDerivativeToDef[#, defs, zeroDims] &, derivsInShort]; (* Definitions of those derivatives *)
    shortsUsed = Union[Flatten[Map[shorthandsInDerivDef[#, shorthands] &, defsUsed],1]];
    lhss = Map[First, eqs];
    positions = Map[Position[lhss, #, {1}, 1] &, shortsUsed];
    MapThread[If[Length[#2] === 0,
      ThrowError["Shorthand " <> ToString[#1] <> " used in derivative " <> ToString[deriv] <>
        " but not defined in calculation"]] &, {shortsUsed, positions}];
    positions2 = Map[#[[1,1]] &, positions];
    position = If[Length[positions2] === 0, 1, Max[positions2]+1];
    Insert[eqs, newShort, position]
  ];

(* Given a calculation containing partial derivatives, return a
   version of the calculation with all the partial derivatives multiplied
   by the Jacobian *)
Options[InsertJacobian] = ThornOptions;
InsertJacobian[calc_List, opts:OptionsPattern[]] :=
  Module[{pdDefs, derivs, newShortDefs, newShorts, combinedShorts, combinedEqs, combinedCalc, eqs, newEqs,
          shorts},
    shorts = lookupDefault[calc, Shorthands, {}];
    pdDefs = OptionValue[PartialDerivatives];
    derivs = GridFunctionDerivativesInExpression[pdDefs, lookup[calc, Equations],
                                                 OptionValue[ZeroDimensions]];
    If[Length[derivs] === 0, Return[calc]];
    newShortDefs = Map[jacobianShorthand, derivs];
    newShorts = Map[First, newShortDefs];
    combinedShorts = Join[shorts, newShorts];
    eqs = lookup[calc, Equations];
    newEqs = eqs /. (x_?(MemberQ[derivs, #] &) :> derivToJacDeriv[x]);
    combinedEqs = newEqs;

    Do[
      combinedEqs = insertDerivInEqs[derivs[[i]], pdDefs, combinedEqs, shorts,
                                     OptionValue[ZeroDimensions]],
      {i, Length[derivs], 1, -1}];

    combinedCalc = mapReplace[mapReplace[mapEnsureKey[calc, Shorthands, {}], Shorthands, combinedShorts], Equations, combinedEqs];
    combinedCalc];

(* Define local pointers to the members of the Jacobian and Jacobian
   derivatives groups *)
CreateJacobianVariables[] :=
CommentedBlock["Jacobian variable pointers",
  {"const bool use_jacobian1 = (!CCTK_IsFunctionAliased(\"MultiPatch_GetMap\") || MultiPatch_GetMap(cctkGH) != jacobian_identity_map)\n",
   "                      && strlen(jacobian_group) > 0;\n",
   "const bool use_jacobian = assume_use_jacobian>=0 ? assume_use_jacobian : use_jacobian1;\n",
   "const bool usejacobian CCTK_ATTRIBUTE_UNUSED = use_jacobian;\n",
   "if (use_jacobian && (" (*, "strlen(jacobian_determinant_group) == 0) || strlen(jacobian_inverse_group) == 0 || " *), "strlen(jacobian_derivative_group) == 0))\n",
   "{\n",
   "  CCTK_WARN(1, \"GenericFD::jacobian_group " (*<> ", GenericFD::jacobian_determinant_group, GenericFD::jacobian_inverse_group, " *) , "and GenericFD::jacobian_derivative_group must both be set to valid group names\");\n",
   "}\n\n",
   "const CCTK_REAL* restrict jacobian_ptrs[9];\n",
   "if (use_jacobian) GenericFD_GroupDataPointers(cctkGH, jacobian_group,\n",
   "                                              9, jacobian_ptrs);\n",
    "\n",
    Table[{"const CCTK_REAL* restrict const J",i,j," CCTK_ATTRIBUTE_UNUSED = use_jacobian ? jacobian_ptrs[",(i-1)*3+j-1,"] : 0;\n"},{i,1,3},{j,1,3}],
    "\n",
   (* "const CCTK_REAL* restrict jacobian_determinant_ptrs[1] CCTK_ATTRIBUTE_UNUSED;\n", *)
   (* "if (use_jacobian) GenericFD_GroupDataPointers(cctkGH, jacobian_determinant_group,\n", *)
   (* "                                              1, jacobian_determinant_ptrs);\n", *)
   (*  "\n", *)
   (*  {"const CCTK_REAL* restrict const detJ CCTK_ATTRIBUTE_UNUSED = use_jacobian ? jacobian_ptrs[0] : 0;\n"}, *)
   (*  "\n", *)
   (* "const CCTK_REAL* restrict jacobian_inverse_ptrs[9] CCTK_ATTRIBUTE_UNUSED;\n", *)
   (* "if (use_jacobian) GenericFD_GroupDataPointers(cctkGH, jacobian_inverse_group,\n", *)
   (* "                                              9, jacobian_inverse_ptrs);\n", *)
   (*  "\n", *)
   (*  Table[{"const CCTK_REAL* restrict const iJ",i,j," CCTK_ATTRIBUTE_UNUSED = use_jacobian ? jacobian_inverse_ptrs[",(i-1)*3+j-1,"] : 0;\n"},{i,1,3},{j,1,3}], *)
   (* "\n", *)
   "const CCTK_REAL* restrict jacobian_derivative_ptrs[18] CCTK_ATTRIBUTE_UNUSED;\n",
   "if (use_jacobian) GenericFD_GroupDataPointers(cctkGH, jacobian_derivative_group,\n",
   "                                              18, jacobian_derivative_ptrs);\n",
    "\n",
    Module[{syms = Flatten[Table[{"dJ",i,j,k},{i,1,3},{j,1,3},{k,j,3}],2]},
      MapIndexed[{"const CCTK_REAL* restrict const ", #1, " CCTK_ATTRIBUTE_UNUSED = use_jacobian ? jacobian_derivative_ptrs[", #2-1, "] : 0;\n"} &, syms]]}];

(* List of symbols which should be allowed in a calculation *)
JacobianSymbols[] :=
  Map[Symbol, Join[
    Flatten[Table[FlattenBlock[{"J",i,j}],{i,1,3},{j,1,3}],1],
    {FlattenBlock[{"detJ"}]},
    Flatten[Table[FlattenBlock[{"iJ",i,j}],{i,1,3},{j,1,3}],1],
    Flatten[Table[FlattenBlock[{"dJ",i,j,k}],{i,1,3},{j,1,3},{k,j,3}],2]]];

(* Parameters to inherit from GenericFD *)
JacobianGenericFDParameters[] :=
  {{Name -> "assume_use_jacobian",        Type -> "CCTK_INT"},
   {Name -> "jacobian_group",             Type -> "CCTK_STRING"},
   {Name -> "jacobian_determinant_group", Type -> "CCTK_STRING"},
   {Name -> "jacobian_inverse_group",     Type -> "CCTK_STRING"},
   {Name -> "jacobian_derivative_group",  Type -> "CCTK_STRING"},
   {Name -> "jacobian_identity_map",      Type -> "CCTK_INT"}};

(* The symbols which are used for the Jacobian variables in the
   generated source code.  These do not have to coincide with the
   actual variable names, as the variable pointers are read using
   CCTK_VarDataPtr. *)
JacobianGroups[] :=
  {{"unknown::unknown", {Global`J11, Global`J12, Global`J13, Global`J21, Global`J22, Global`J23, Global`J31, Global`J32, Global`J33}},
   {"unknown::unknown", {Global`detJ}},
   {"unknown::unknown", {Global`iJ11, Global`iJ12, Global`iJ13, Global`iJ21, Global`iJ22, Global`iJ23, Global`iJ31, Global`iJ32, Global`iJ33}},
   {"unknown::unknown", {Global`dJ111, Global`dJ112, Global`dJ113, Global`dJ122, Global`dJ123, Global`dJ133,
                         Global`dJ211, Global`dJ212, Global`dJ213, Global`dJ222, Global`dJ223, Global`dJ233,
                         Global`dJ311, Global`dJ312, Global`dJ313, Global`dJ322, Global`dJ323, Global`dJ333}}};

JacobianCheckGroups[groups_] :=
  Module[{int},
    int = Intersection[allGroupVariables[groups], allGroupVariables[JacobianGroups[]]];
    If[int =!= {},
      ThrowError["Error: Some group variables conflict with reserved Jacobian variable names: " <> ToString[int]]]];

(* These gridfunctions are only given local variable copies if the use_jacobian variable is true *)
JacobianConditionalGridFunctions[] :=
  {("J" ~~ DigitCharacter ~~ DigitCharacter) |
   ("detJ") |
   ("iJ" ~~ DigitCharacter ~~ DigitCharacter) |
   ("dJ" ~~ DigitCharacter ~~ DigitCharacter ~~ DigitCharacter),
   "use_jacobian",
   None};

End[];

EndPackage[];