-1

I've a bunch of equations that are loaded from a JSON file and I'd like to evaluate these expressions. These expressions have some constants and values of the constants (K1, K2, ...) are also saved in the json file. The values corresponding to the variables (var1, var2 ...) present in the expressions are defined in a dictionary vars. I want to plugin the values of the constants and the variables in the expressions and compute the numerical values.

import json
vars = {"var1": 1, "var2": 2, "var3": 3, "var4": 4, "var5": 5, "var6": 5}
with open("tst.json", 'r') as f:
     r = json.load(f)
     print(r)

json content:

{
  "EQ1": {
    "parameters": {
      "EQ1_coeff1": {
        "value": 1.0e-06
      },
      "P2": {
        "value": 3.0
      },
      "P3": {
        "value": 3.0
      }
    },
    "expression": "(EQ1_coeff1 * ((1 dimensionless)/(1 dimensionless)) / (1 dimensionless))*(var1^P3/(var1^P3 + P2^P3) )"
  },
  "EQ2": {
    "parameters": {
      "EQ2_coeff2": {
        "value": 5253
      },
      "K2": {
        "value": 3
      },
      "K5": {
        "value": 1
      },
      "K3": {
        "value": 525
      },
      "K4": {
        "value": 3
      },
      "K6": {
        "value": 2
      },
      "K7": {
        "value": 0.01
      }
    },
    "expression": "(EQ2_coeff2 *((var2*var3)/(K1*K2))* ((1 dimensionless - ((var3*var4)/(var2))/K6) / ((1 dimensionless + var1/K1)*(1 dimensionless + var2)+ 1 dimensionless)))*(1 dimensionless/(1 dimensionless + var5/K7))"
  }
}

I'd like to ask for suggestions on how these expressions (stored in expression) can be computed.

Side note:

1 dimensionless is nothing but 1.

MaxPowers
  • 5,235
  • 2
  • 44
  • 69
Natasha
  • 1,111
  • 5
  • 28
  • 66

1 Answers1

1

I will simply update the globals() with the dict and then use eval, as done below:

import json
vars = {"var1": 1, "var2": 2, "var3": 3, "var4": 4, "var5": 5, "var6": 5}
r = {
  "EQ1": {
    "parameters": {
      "EQ1_coeff1": {
        "value": 1.0e-06
      },
      "P2": {
        "value": 3.0
      },
      "P3": {
        "value": 3.0
      }
    },
    "expression": "(EQ1_coeff1 * ((1 dimensionless)/(1 dimensionless)) / (1 dimensionless))*(var1^P3/(var1^P3 + P2^P3) )"
  },
  "EQ2": {
    "parameters": {
      "EQ2_coeff2": {
        "value": 5253
      },
      "K2": {
        "value": 3
      },
      "K5": {
        "value": 1
      },
      "K3": {
        "value": 525
      },
      "K4": {
        "value": 3
      },
      "K6": {
        "value": 2
      },
      "K7": {
        "value": 0.01
      }
    },
    "expression": "(EQ2_coeff2 *((var2*var3)/(K1*K2))* ((1 dimensionless - ((var3*var4)/(var2))/K6) / ((1 dimensionless + var1/K1)*(1 dimensionless + var2)+ 1 dimensionless)))*(1 dimensionless/(1 dimensionless + var5/K7))"
  }
}

globals().update( **{k:v['value'] for k,v in r["EQ1"]["parameters"].items()})
globals().update( **{k:v['value'] for k,v in r["EQ2"]["parameters"].items()})
globals().update(**vars)

eval(r['EQ1']['expression'].replace("dimensionless", "").replace("^", "**"))

This gives: 3.571428571428571e-08

For EQ2 you do not have variable K1 defined. This is more of a hack, I am not aware of a more elegant way though. Also keep in mind eval is a not very safe function.

quest
  • 3,576
  • 2
  • 16
  • 26
  • thanks a lot, it helps! Could you please let me know in what form of expression evaluations `Also keep in mind eval is a not very safe function` this is not safe. – Natasha Dec 22 '20 at 13:29
  • M happy to help :). `eval` can cause trouble if you get the string from an untrusted source. Some malicious string when executed can do serious damage. – quest Dec 22 '20 at 13:37
  • Thanks for the clarifications. Also, could you please explain a bit on how global works. Thank you ver much! – Natasha Dec 22 '20 at 14:25
  • 1
    globals() returns a dictionary which tell all the variables available in the global scope. This answer is very instructive https://stackoverflow.com/a/12694065/4551984 – quest Dec 22 '20 at 14:34