ianhinder / Kranc

A Mathematica package for generating code for solving time dependent partial differential equations
http://kranccode.org
GNU General Public License v2.0
28 stars 10 forks source link

Kranc does not allow grid functions that start with the letter u #141

Open rhaas80 opened 6 years ago

rhaas80 commented 6 years ago

Taking the http://kranccode.org/SimpleWave.m example and adding ut to groups:

groups = {{"evolved_group", {phi, pi, ut}}};

produces the following error:

Using Kranc installation at /home/rhaas/postdoc/gr/cactus/ET_trunk/repos/Kranc/Bin/..
Mathematica 11.0.0 for Linux x86 (64-bit)
Copyright 1988-2016 Wolfram Research, Inc.
Profiling disabled
Creating thorn SimpleWave
Processing tensorial arguments
Computing reflection symmetries
Creating (component-based) Kranc thorn
Processing arguments to CreateKrancThorn
Verifying arguments
Error:
"phi is not a member of any of the following groups: "
{{"evolved_group", {{phi, pi, 1}, {phi, pi, 2}, {phi, pi, 3}}}, {"grid::coordinates", {x, y, z, r}}}
in CreateKrancThorn/MoLProcessCode/MoLNonevolvedGroups

Changing ut to Ut removes the error.

ianhinder commented 6 years ago

The problem isn't that it starts with the letter u, it's likely that it is a reserved symbol in TensorTools to indicate an upper index "t". So ut is actually TensorIndex["t", "u"] or something. In fact, it looks like Kranc has expanded the index ut into 1, 2, 3, which is where those numbers come from in the error message. That is a little embarrassing! It would be expected if this were a tensor; v[ut] would be expanded into variables v1, v2 and v3 in the group definition. But for some reason, Kranc is expanding a raw index, rather than requiring that the variable is a tensor.

rhaas80 commented 6 years ago

yup. Inside of v[ut] it is clear that ut is an "upper index t" but I would have expected that any symbol that Mathematica has not yet claimed itself is fair game for tensor names. Is there any hope of fixing this?

ianhinder commented 6 years ago

It is not possible to define variables with the same names as the tensor indices, as those indices are Mathematica variables with values TensorIndex[...]. The only fix would be to catch this usage and give a clear error message. If I were to design it again, I would make the indices inert objects which were recognised inside tensors, but that is not how it works at the moment.

rhaas80 commented 6 years ago

So that basically means that I cannot use anything like "ua" to "uz" as variable names? And also no single letter variables? Both would be tensor indices by this logic. Basically Kranc claims those 52 variables for its own use it seems. This should be documented.

ianhinder commented 6 years ago

You cannot use ua-uz or la-lz as variable names, as they are indices in TensorTools. I cannot think of a reason why single-letter variable names would be a problem for Kranc; what did you have in mind? Yes, it should be documented, but more importantly (since people don't tend to read documentation), it should be caught by Kranc and the error message improved.

rhaas80 commented 6 years ago

It has been too long since I last used Kranc. I thought hat upper / lower pair was ui and i rather than ui and li. Wouldn't something like this (remember I don't really use Mathematica) work

In[1]:= MakeTensorIndex[ua] := TensorIndex[u, a];
MakeTensorIndex[la] := TensorIndex[l, a];
MakeTensorIndex[ub] := TensorIndex[u, b];
MakeTensorIndex[lb] := TensorIndex[l, b];
In[5]:= DefineTensor[tname_[_]] := tname[ind_] := Tensor[tname][MakeTensorIndex[ind]]
In[6]:= DefineTensor[foo[ua]]
In[7]:= foo[ub]
Out[7]= Tensor[foo][TensorIndex[u, b]]
In[8]:= foo[lb]
Out[8]= Tensor[foo][TensorIndex[l, b]]

ie have DefineTensor create a function for the tensor that takes the correct arguments? There is some legwork since one has to manually define the mappings MakeTensorIndex[ua] := TensorIndex[u, a]; for every letter of the alphabet but otherwise this would seem to me (again rarely if ever using MMA and not knowing at all how TensorTools actually implements tensors) to work.

ianhinder commented 6 years ago

So tensor indices would be interpreted every time a tensor expression, e.g. foo[lb] was invoked, rather than having lb as a symbol with a definition. This could probably be made to work. On the other hand, there's really no need for this to happen when foo is "evaluated". Instead, it could happen whenever the entire expression needs to be interpreted as tensorial (i.e. converted into components, or having a covariant derivative taken, etc). Then the user-entered expression would be entirely inert, which I think is a lot more elegant. Either way though, changing something so fundamental is likely to involve quite a lot of work to make sure it is correct, and I don't think it is a high priority. In the bigger picture, I would either completely rewrite the tensor implementation (it was one of my first Mathematica packages, written during my PhD), or just use xAct. I have several times looked at how a new version of TensorTools could work, learning from all the experience gained with the current version. This would be a fun project, but I don't think it's important enough to work on at the moment.