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

Provide reference-like functionality to MapLookup. #113

Open dmkoppelman opened 9 years ago

dmkoppelman commented 9 years ago

The key/value store implemented by the functions in MapLookup is passed by value. This requires that the store is passed up and down the evaluation stack. (Down to a called function, and back up to the caller if caller needs callee's modifications.)

In this ticket provide a reference type value. (Note: This is something that I have working, I'm not asking for anyone else to do it.)

Here is a quick example:

calcs = { Name -> "Fred" } c2 = mapRefAdd[calcs,Later]; // Add a reference-type key, Later. Value unset. // c2: { Name -> "Fred", Later -> someString },

c3 = c2;

// c3: { Name -> "Fred", Later -> someString },

mapRefSet[c3, Later, 123]; // Set the value for Later.

// c2: { Name -> "Fred", Later -> 123 }, // c3: { Name -> "Fred", Later -> 123 },

mapReplace[c3, Later, 456]; // Either mapRefSet or mapReplace can be used.

// c2: { Name -> "Fred", Later -> 456 }, // c3: { Name -> "Fred", Later -> 456 },

Notice that the value of Later in c2 has changed even though mapRefSet and mapRefSet operated on c3.

My immediate need for this is to retrieve values for the stencil bounding box and resolved "Automatic" value of "Where", which are currently computed in code which does not return the "calcs" key/value store.

An alternative would be to refactor the code so that it does pass the calcs store up the evaluation stack. That's not as easy as it sounds because I'd need to know which modified or added values should be passed back to the caller. With the reference type I am proposing those values which should be visible are explicitly named with mapRefAdd.

Here is how this is implemented:

mapRefSet[calcs,Key] generates a string, valKey, (using MMA's built in symbol naming mechanism) and uses that as the value.

mapRefSet[calcs,Key,Value] will look up valKey using Key and then add a definition: obarray[valKey] = Value.

lookup[calcs,Key] will return obarray[valKey]. Symbol obarray is defined so that non-reference types evaluate to themselves and unset reference types throw an error.

I'll post a patch and maybe push the change to a new branch.

eschnett commented 9 years ago

Instead of defining a new type, you could pass a list with a single mapRef to the caller. The caller can the modify the list. The modification is then visible in the caller.

dmkoppelman commented 9 years ago

I'm not 100% sure what you're trying to say, but it sounds like it would work. There are many ways of doing this, I chose a method that would require fewest changes to the existing Kranc code.

dmkoppelman commented 9 years ago

I was going to attach a patch to the issue but it looks like github's issue tracker doesn't support attachments. I'll push a branch as soon as LSU's network problems are resolved or I figure out a way around them.

dmkoppelman commented 9 years ago

I've pushed the branch, its dmk-chemora. Since I can't attach a patch, here's a command for generating the patch:

git diff 607e2f479a95004c56c6d8081dd91ef22d0ba723 b4fde2809fb75961389d5b5364429fe8e6bb418a

eschnett commented 9 years ago

you can compare branch and master, which gives you a nice diff in the browser.

-erik

On Wednesday, January 14, 2015, David M. Koppelman notifications@github.com wrote:

I've pushed the branch, its dmk-chemora. Since I can't attach a patch, here's a command for generating the patch:

git diff 607e2f4 b4fde28

Reply to this email directly or view it on GitHub.< https://ci5.googleusercontent.com/proxy/-eZ8QdAXGhq9STsnhtaAmbcKazbhdKlO0flZr04JbjPu0pw8bm-cMhSKL4Xu_cwsJXqrh-IOX-c4Moi47aMGnoWlGyljbhEX1AnwLsHQvOX5hy0Cm6y2G-NFh5HpAUIkyzDI9w1K-NiI8b0YeKK9s-1pImYjeA=s0-d-e1-ft#https://github.com/notifications/beacon/AANCCiNwIFrIZ1WGquqb0wA4JTH0Mnqoks5nhwKVgaJpZM4DSilG.gif

Erik Schnetter schnetter@gmail.com http://www.perimeterinstitute.ca/personal/eschnetter/