1

I have a file of models that I have saved that I want to be able to call in another program to be solved with NDSolve. Can I use Cases to select each model based upon the m# tag given in the model list? I've tried to avail the following.

test = {
    {
     m1-> 
     {
      {ul'[t]==k1p*s*(1-ul[t])/(km1+(1-ul[t]))-k2p*ul[t]/(km2+ul[t])-         
       k5p*ul[t]*um[t]/(km5+ul[t])},
      {um'[t]==k3p*uc[t]*(1-um[t])/(km3+(1-um[t]))-k4p*um[t]/(km4+um[t])},
      {uc'[t]==k6p*ul[t]*(1-uc[t])/(km6+(1-uc[t]))-k7p*uc[t]/(km7+uc[t])}
     },
      {ul[0]==0.,um[0]==0.,uc[0]==0.0},
      {s, k1p, km1, k2p, km2, k5p, km5, k3p, km3, k4p, km4, k6p, km6, k7p, km7}
    },
    {
     m2-> 
      {
       {ul'[t]==k1p*s*(1-ul[t])/(km1+(1-ul[t]))-k2p*ul[t]/(km2+ul[t])-
        k5p*um[t]*ul[t]/(km5+ul[t])},
       {um'[t]==k3p*ul[t]*(1-um[t])/(km3+(1-um[t]))-k4p*um[t]/(km4+um[t])}
      },
       {ul[0]==0.,um[0]==0.},
       {s, k1p, km1, k2p, km2, k5p, km5, k3p, km3, k4p, km4}
      }
   }

openModel[modelName_] := Cases[test, modelName]  

openModel[m1]
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
tarhawk
  • 769
  • 5
  • 15
  • Are you using Mathematica 10? If so, I have an very straightforward solution to your problem. – Jinxed Jan 26 '15 at 17:09
  • @m_goldberg: Please take a look at existing answers before editing the question. My answer diverged from the variable you introduced afterwards: You used "test", I used "models".

    I adjusted my answer accordingly.

    – Jinxed Jan 26 '15 at 17:45
  • @Jinxed. Your answer was fine even before you edited it. I don't think anyone would have been confused by the difference in identifier; but I apologize for not paying more attention to your answer and using models. – m_goldberg Jan 26 '15 at 22:58
  • @m_goldberg: Thanks for the positive review! – Jinxed Jan 27 '15 at 09:36

1 Answers1

3

Solution for Mathematica before version 10

You can use the following function to achieve your goal:

openModel[modelName_] := 
 Cases[test, Rule[key_, res__] :> res /; key == modelName, 2]

e.g.: openModel[m2] will return

{{{Derivative[1][ul][t] == (k1p s (1 - ul[t]))/(1 + km1 - ul[t]) - (
 k2p ul[t])/(km2 + ul[t]) - (k5p ul[t] um[t])/(
 km5 + ul[t])}, {Derivative[1][um][t] == (k3p ul[t] (1 - um[t]))/(
 1 + km3 - um[t]) - (k4p um[t])/(km4 + um[t])}}}

Solution for Mathematica 10

You can make use of associations in Mathematica 10 by changing your model definition according to this one (i.e.: make use of Association):

test = <|
  m1 -> {{{ul'[t] == 
       k1p*s*(1 - ul[t])/(km1 + (1 - ul[t])) - 
        k2p*ul[t]/(km2 + ul[t]) - 
        k5p*ul[t]*um[t]/(km5 + ul[t])}, {um'[t] == 
       k3p*uc[t]*(1 - um[t])/(km3 + (1 - um[t])) - 
        k4p*um[t]/(km4 + um[t])}, {uc'[t] == 
       k6p*ul[t]*(1 - uc[t])/(km6 + (1 - uc[t])) - 
        k7p*uc[t]/(km7 + uc[t])}}, {ul[0] == 0., um[0] == 0., 
     uc[0] == 0.0}, {s, k1p, km1, k2p, km2, k5p, km5, k3p, km3, k4p, 
     km4, k6p, km6, k7p, km7}}, 
  m2 -> {{{ul'[t] == 
       k1p*s*(1 - ul[t])/(km1 + (1 - ul[t])) - 
        k2p*ul[t]/(km2 + ul[t]) - 
        k5p*um[t]*ul[t]/(km5 + ul[t])}, {um'[t] == 
       k3p*ul[t]*(1 - um[t])/(km3 + (1 - um[t])) - 
        k4p*um[t]/(km4 + um[t])}}, {ul[0] == 0., um[0] == 0.}, {s, 
     k1p, km1, k2p, km2, k5p, km5, k3p, km3, k4p, km4}}
|>

Then, the openModel-method reduces to:

openModel[modelName_] := test[modelName]

yielding the same result as above.

I hope to have been of some help to you.

Jinxed
  • 3,753
  • 10
  • 24
  • Unless you need numeric equivalence in the modelName parameter you can reduce the first function to openModel[modelName_] := Cases[test, (modelName -> res_) :> res, 2] – Mr.Wizard Jan 26 '15 at 21:01
  • I've gone with the first iteration here, I would like to take advantage of the assoications method. Both answers from jinxed and mr. Wizard solve the problem. However Mathematica v.10 is so unstable with memory management that I've reverted back to v.9. So no use of Associations quite yet. Hopefully they'll fix those leaky memory issues soon :) – tarhawk Jan 27 '15 at 11:55
  • @tarhawk: Try disabling the Suggestion Bar. It has caused me some trouble already: (http://mathematica.stackexchange.com/q/71889/24763) – Jinxed Jan 27 '15 at 14:33