From d7612a318433c8e99daa8348570d0866d37d9f57 Mon Sep 17 00:00:00 2001 From: Ian Hinder Date: Thu, 12 Sep 2013 20:29:28 +0200 Subject: CodeGenSymmetries.m: Reformat code --- Tools/CodeGen/CodeGenSymmetries.m | 141 +++++++++++++++++--------------------- 1 file changed, 64 insertions(+), 77 deletions(-) (limited to 'Tools') diff --git a/Tools/CodeGen/CodeGenSymmetries.m b/Tools/CodeGen/CodeGenSymmetries.m index 5cdad9f..033fdb9 100644 --- a/Tools/CodeGen/CodeGenSymmetries.m +++ b/Tools/CodeGen/CodeGenSymmetries.m @@ -21,7 +21,7 @@ BeginPackage[ "CodeGenSymmetries`", {"Errors`", "Helpers`", "Kranc`", "CodeGenCactus`", "MapLookup`", "CodeGenKranc`", - "CodeGenC`"}]; + "CodeGenC`", "CodeGen`"}]; CreateSymmetriesRegistrationSource::usage = ""; @@ -34,94 +34,81 @@ Begin["`Private`"]; (* Symmetries registration spec = {FullName -> "impl::GFname", Sym -> {symX, symY, symZ}} *) -symmetriesBlock[spec_] := +DefFn[symmetriesBlock[spec_] := + Module[ + {i, dim}, - Module[{i, KrancDim}, + If[!MatchQ[spec, {FullName -> _String, Sym -> {_,_,_}}], + ThrowError["symmetriesBlock: Expecting a symmetry registration spec but got ", spec]]; - If[!MatchQ[spec, {FullName -> _String, Sym -> {_,_,_}}], - ThrowError["symmetriesBlock: Expecting a symmetry registration spec but got ", spec]]; - - KrancDim = 3; - - sym = lookup[spec, Sym]; - - {Table["sym[" <> ToString[i - 1] <> "] = " <> - ToString@sym[[i]] <> ";\n", {i, 1, KrancDim}], - - "SetCartSymVN(cctkGH, sym, \"" <> lookup[spec, FullName] <> "\");\n\n" -} -]; + sym = lookup[spec, Sym]; + dim = Length[sym]; + + {Table[{"sym[", i - 1, "] = ", sym[[i]], ";\n"}, {i, 1, dim}], + "SetCartSymVN(cctkGH, sym, ", Quote[lookup[spec, FullName]], ");\n\n"}]]; (* syms is a list of rules mapping gridfunctions to their symmetry structures *) -calcSymmetry[gf_, syms_] := - Module[{}, +DefFn[ + calcSymmetry[gf_, syms_] := + Module[ + {}, If[mapContains[syms, gf], - Return[lookup[syms,gf]], - (* FIXME: We are defaulting to scalar symmetries if no information is - available. This shouldn't happen, but I am bypassing this check - temporarily. *) - Print["WARNING: defaulting to symmetries of a scalar for "<>ToString[gf]]; - Return[{1,1,1}]]]; + Return[lookup[syms,gf]], + (* FIXME: We are defaulting to scalar symmetries if no information is + available. This shouldn't happen, but I am bypassing this check + temporarily. *) + Print["WARNING: defaulting to symmetries of a scalar for "<>ToString[gf]]; + Return[{1,1,1}]]]]; (* This function guesses the symmetries based on component names as we have not been given them *) -calcSymmetry[gf_] := Module[{sym, q, string}, - -sym = {1, 1, 1}; (* default *) - -string = ToString@gf; - -While[IntegerQ[q = ToExpression@StringTake[string, -1]], - -Module[{}, - sym[[q]] = -sym[[q]]; - string = StringDrop[string, -1] - ] -]; -sym -]; +DefFn[ + calcSymmetry[gf_] := + Module[ + {sym = {1,1,1} (* default *), q, string}, + string = ToString@gf; + While[ + IntegerQ[q = ToExpression@StringTake[string, -1]], + sym[[q]] = -sym[[q]]; + string = StringDrop[string, -1]]; + sym]]; (* Given a symmetries registration structure as defined above, return a C CodeGen structure of a source file which will register the symmetries. *) -CreateSymmetriesRegistrationSource[thornName_, implementationName_, GFs_, reflectionSymmetries_, debug_] := - Module[{spec, j, lang, tmp}, - - If[debug, - Print["Registering Symmetries for: ", GFs]; - ]; - - lang = CodeGenC`SOURCELANGUAGE; - CodeGenC`SOURCELANGUAGE = "C"; - - spec = Table[{FullName -> implementationName <> "::" <> ToString@GFs[[j]], - Sym -> If[reflectionSymmetries === False, - calcSymmetry[GFs[[j]]], - calcSymmetry[GFs[[j]], Union@reflectionSymmetries]]}, {j, 1, Length@GFs}]; - - tmp = {FileHeader["C"], - - Map[IncludeFile, - {"cctk.h", "cctk_Arguments.h", "cctk_Parameters.h", "Symmetry.h"}], - - DefineCCTKFunction[ thornName <> "_RegisterSymmetries", "void", - If[Length[spec] > 0, - {CommentedBlock["array holding symmetry definitions", - - "CCTK_INT sym[3];\n\n"], - - CommentedBlock["Register symmetries of grid functions", - - Map[symmetriesBlock, spec]]}, - {}] -] - }; - - CodeGenC`SOURCELANGUAGE = lang; - -tmp -]; - +DefFn[ + CreateSymmetriesRegistrationSource[thornName_, implementationName_, GFs_, reflectionSymmetries_, debug_] := + Module[ + {spec, j, lang, tmp}, + + If[debug, Print["Registering Symmetries for: ", GFs]]; + + lang = CodeGenC`SOURCELANGUAGE; + CodeGenC`SOURCELANGUAGE = "C"; + + spec = Table[{FullName -> implementationName <> "::" <> ToString@GFs[[j]], + Sym -> If[reflectionSymmetries === False, + calcSymmetry[GFs[[j]]], + calcSymmetry[GFs[[j]], Union@reflectionSymmetries]]}, + {j, 1, Length@GFs}]; + + tmp = {FileHeader["C"], + + Map[IncludeFile, + {"cctk.h", "cctk_Arguments.h", "cctk_Parameters.h", "Symmetry.h"}], + + DefineCCTKFunction[ + thornName <> "_RegisterSymmetries", "void", + If[Length[spec] > 0, + {CommentedBlock["array holding symmetry definitions", + "CCTK_INT sym[3];\n\n"], + CommentedBlock["Register symmetries of grid functions", + Map[symmetriesBlock, spec]]}, + {}]]}; + + CodeGenC`SOURCELANGUAGE = lang; + + tmp]]; End[]; -- cgit v1.2.3