Skip to content
This repository
Browse code

Add ParameterConditions option for CreateThorn

This option allows the user to set certain conditions on parameters which must be satisfied otherwise the thorn aborts with a given error message (details in Doc/KrancDoc.tex).
  • Loading branch information...
commit 58eb73177f4ea930b8380772bd4c7fdaa663dc8f 1 parent 4ac728a
ianhinder authored
6 Doc/KrancDoc.tex
@@ -968,6 +968,12 @@ \subsubsection{Named Arguments}
968 968
969 969 KeywordParameters & list of KeywordParameterDefinition structures & A list of parameter definition structures for all the keyword parameters which this thorn will define and use. (optional) & \{\} \\
970 970
  971 + ParameterConditions & list of parameter conditions & Each condition
  972 + is of the form \{\textit{condition}, \textit{message}\}, where
  973 + \textit{condition} is a boolean expression in parameter names, and
  974 + \textit{message} is the message for a fatal error which is generated
  975 + when the thorn starts if \textit{condition} is not satisfied & \{\} \\
  976 +
971 977 InheritedImplementations & list of strings & A list of all the implementations which this thorn
972 978 will inherit from. This is necessary to use grid functions
973 979 provided by these implementations. (optional) & \{\} \\
3  Tools/CodeGen/Kranc.m
@@ -94,7 +94,8 @@
94 94 Variables -> Automatic,
95 95 Shorthands -> {},
96 96 ParentDirectory -> ".",
97   - GenerateScript -> False};
  97 + GenerateScript -> False,
  98 + ParameterConditions -> {}};
98 99
99 100 ExecuteOn;
100 101 Device;
10 Tools/CodeGen/KrancThorn.m
@@ -29,7 +29,7 @@
29 29 "MapLookup`", "KrancGroups`", "Differencing`",
30 30 "CalculationFunction`", "Errors`", "Helpers`", "CactusBoundary`",
31 31 "KrancTensor`", "Param`", "Schedule`", "Interface`", "Kranc`", "Jacobian`",
32   - "ConservationCalculation`", "CaKernel`", "Calculation`"}];
  32 + "ConservationCalculation`", "CaKernel`", "Calculation`", "ParamCheck`"}];
33 33
34 34 CreateKrancThorn::usage = "Construct a Kranc thorn";
35 35
@@ -331,7 +331,7 @@ Thorn generation (main entry point for non-tensorial thorns)
331 331 (* Makefile *)
332 332 InfoMessage[Terse, "Creating make file"];
333 333 make = CreateMakefile[Join[{"Startup.cc", "RegisterSymmetries.cc"},
334   - {"RegisterMoL.cc"},
  334 + {"RegisterMoL.cc"}, If[Length[OptionValue[ParameterConditions]] > 0, {"ParamCheck.cc"}, {}],
335 335 incFilenames,
336 336 Map[lookup[#, Filename] &, boundarySources]]];
337 337
@@ -350,7 +350,11 @@ Thorn generation (main entry point for non-tensorial thorns)
350 350 {Filename -> "RegisterSymmetries.cc", Contents -> symregister},
351 351 {Filename -> "Differencing.h", Contents -> diffHeader}},
352 352 MapThread[{Filename -> #1, Contents -> #2} &,
353   - {calcFilenames, calcSources}], boundarySources]};
  353 + {calcFilenames, calcSources}], boundarySources,
  354 + If[Length[OptionValue[ParameterConditions]] > 0,
  355 + {{Filename -> "ParamCheck.cc",
  356 + Contents -> ParameterCheckSource[thornName, OptionValue[ParameterConditions]]}},
  357 + {}]]};
354 358 InfoMessage[Terse, "Creating thorn"];
355 359 CreateThorn[thornspec]];
356 360
83 Tools/CodeGen/ParamCheck.m
... ... @@ -0,0 +1,83 @@
  1 +
  2 +(* Copyright 2012 Ian Hinder
  3 +
  4 + This file is part of Kranc.
  5 +
  6 + Kranc is free software; you can redistribute it and/or modify
  7 + it under the terms of the GNU General Public License as published by
  8 + the Free Software Foundation; either version 2 of the License, or
  9 + (at your option) any later version.
  10 +
  11 + Kranc is distributed in the hope that it will be useful,
  12 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + GNU General Public License for more details.
  15 +
  16 + You should have received a copy of the GNU General Public License
  17 + along with Kranc; if not, write to the Free Software
  18 + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 +*)
  20 +
  21 +BeginPackage["ParamCheck`", {"Errors`", "Helpers`", "Kranc`", "CodeGenCactus`",
  22 + "CodeGenC`", "CodeGen`"}];
  23 +
  24 +ParameterCheckSource;
  25 +ParameterCheckSchedule;
  26 +
  27 +Begin["`Private`"];
  28 +
  29 +DefFn[
  30 + ParameterCheckSource[thornName_String, conditions_List] :=
  31 + {Map[IncludeFile,
  32 + {"cctk.h", "cctk_Arguments.h", "cctk_Parameters.h"}],
  33 +
  34 + DefineCCTKFunction[
  35 + thornName, "void",
  36 + ConditionalOnParameterTextual[
  37 + "CCTK_MyProc(cctkGH) == 0",
  38 + Map[checkCondition, conditions]]]}];
  39 +
  40 +DefFn[
  41 + ParameterCheckSchedule[thornName_String] :=
  42 + {Name -> thornName<>"_ParamCheck",
  43 + SchedulePoint -> "at CCTK_PARAMCHECK",
  44 + Options -> "global",
  45 + Language -> "C",
  46 + Comment -> "Check parameter consistency"}];
  47 +
  48 +DefFn[checkCondition[{cond_, error_String}] :=
  49 + Module[
  50 + {render, renderbool, paramPattern},
  51 +
  52 + paramPattern = Except[True | False, _Symbol | _Parameter];
  53 +
  54 + renderbool[Equal[a:paramPattern,b_String]] := {"CCTK_EQUALS(", rendervalue[a], ",\"", b,"\")"};
  55 + renderbool[Unequal[a:paramPattern,b_String]] := {"!CCTK_EQUALS(", rendervalue[a], ",\"", b,"\")"};
  56 + renderbool[Equal[a:paramPattern,b_?NumberQ]] := {rendervalue[a], " == ", rendervalue[b]};
  57 + renderbool[Unequal[a:paramPattern,b_?NumberQ]] := {rendervalue[a], " != ", rendervalue[b]};
  58 +
  59 + renderbool[Or[a_,b_]] := {"(",renderbool[a]," || ", renderbool[b],")"};
  60 + renderbool[And[a_,b_]] := {"(",renderbool[a]," && ", renderbool[b],")"};
  61 + renderbool[Not[a_]] := {"(!", renderbool[a],")"};
  62 + renderbool[a:paramPattern] := ToString[a/.(Parameter[x_]->x)]; (* Boolean parameter *)
  63 +
  64 + (* rendervalue[a_String] := a; -- Allow literal pass-through *)
  65 + rendervalue[a_?NumberQ] := ToString[a];
  66 + rendervalue[Parameter[a_String]] := a;
  67 + rendervalue[a_ /; MemberQ[params,a]] := ToString[a];
  68 + renderbool[x_] := ThrowError["Unexpected value in run-time conditional expression (boolean):", x, "in", cond];
  69 + render[x_] := ThrowError["Unexpected value in run-time conditional expression (value):", x, "in", cond];
  70 +
  71 + unparen[s_] :=
  72 + Module[
  73 + {s2 = FlattenBlock[s],result},
  74 + result = StringReplace[FlattenBlock[s2],StartOfString ~~ "(" ~~ any__ ~~ ")" ~~ EndOfString :> any];
  75 + If[result === s2, result, unparen[result]]];
  76 +
  77 + ConditionalOnParameterTextual[
  78 + unparen@renderbool[cond],
  79 + {"CCTK_WARN(0, ", StringDrop[Stringify[error],-1], ");\n"}]]];
  80 +
  81 +End[];
  82 +
  83 +EndPackage[];
6 Tools/CodeGen/Schedule.m
@@ -18,7 +18,7 @@
18 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 19 *)
20 20
21   -BeginPackage["Schedule`", {"Thorn`", "KrancGroups`", "MapLookup`", "Errors`", "Helpers`", "Kranc`", "CaKernel`", "Calculation`"}];
  21 +BeginPackage["Schedule`", {"Thorn`", "KrancGroups`", "MapLookup`", "Errors`", "Helpers`", "Kranc`", "CaKernel`", "Calculation`", "ParamCheck`"}];
22 22
23 23 CreateKrancScheduleFile;
24 24
@@ -300,7 +300,9 @@
300 300 scheduledFunctions =
301 301 Join[{scheduledStartup, scheduleRegisterSymmetries},
302 302 scheduledCalcs, CactusBoundary`GetScheduledFunctions[thornName, evolvedGroups],
303   - {scheduleMoLRegister}];
  303 + {scheduleMoLRegister}, If[Length[OptionValue[ParameterConditions]] > 0,
  304 + {ParameterCheckSchedule[thornName]},
  305 + {}]];
304 306
305 307 If[OptionValue[UseCaKernel],
306 308 scheduledFunctions = Join[scheduledFunctions, CaKernelSchedule[thornName]]];

0 comments on commit 58eb731

Please sign in to comment.
Something went wrong with that request. Please try again.