Jump to content
Geochemist's Workbench Support Forum

Script in Chemplugin


Recommended Posts

Hi Brian

We struggle to write a script in Chemplugin for the reaction rate of kinetic minerals. The script for dolomite is given below.

Python also don't like the double notation: "H+" for the parameters.

 "kinetic Dolomite-Mn 12.1 volume% rate_law = script 'rate = (1.49e-02*EXP(-29000/(8.3144598*TK))*activity("H+")^0.75 + 1.00e-02*EXP(-34800/(8.3144598*TK))*activity("H2CO3")^0.75 + 2.20e-7*EXP(-52200/(8.3144598*TK))*activity("H2O")^0.75) * surface * (1 - QoverK) RETURN rate' pre-exp = ??? act_eng = ??? surface = .65"

We would appreciate your help.

Regards Johan

Link to comment
Share on other sites

Hi Brian

I tried different ways with the quotation marks. If I use single quotes, then there is no error, however chemplugin just ignores the line.

I may have a similar problem when I want to assign a different database when using python. There is no error but chemplugin just ignores this and use the default database anyway, e.g.

cmds = ('data = "C:\Program Files\ChemPlugin\Gtdata\thermo.com.V8.R6+_MRM_JF.tdat" ',          …...etc

I have tried to use all different ways with the quotation marks but it doesn't help.

Regards

Johan

Link to comment
Share on other sites

Hi Johan,

The inner quotes in the kinetic rate law need to be escaped with a backslash (e.g. \"H+\" or \'H+\') .

For the thermo data, the issue is the same. You can do something like this: cmds = ('data = "C:\\Program Files\\ChemPlugin\\Gtdata\\thermo.com.V8.R6+_MRM_JF.tdat" ',      

Hope this helps. We appreciate your patience.

Regards,

Brian

Link to comment
Share on other sites

  • 4 weeks later...

Hi Brian

I have not come right with this yet and would appreciate if you can help.

To make the problem more tangible I have edited the chemplugin example: Flowthrough1.py and attached it here.

 

1) The thermo database does not seem to need the escape character. In the attached python script I can run the 'surface data' without any escape character:

      'surface_data = "C:\Program Files\ChemPlugin\Gtdata\FeOH.sdat"',

However, if you deactivate it with # and activate the wateqdatabase (for instance), with or without the escape character it does not work:

'data = "C:\Program Files\Gwb\Gtdata\thermo_wateq4f.tdat" verify', 

 or

 'data = "C:\\Program Files\\Gwb\\Gtdata\\thermo_wateq4f.tdat" verify',

I have tested it by changing a mineral name in the database i.e. calcite --> calcite2, however chemplugin couldn't read the database.

 

2) Then I also cant get the script to run. I have tried different ways but cant get it to run.

I dont know where is the problem. It is not maybe at the "….RETURN rate" part?

   #     "kinetic Calcite .1 volume% rate_law = script 'rate = (1.49e-02*EXP(-29000/(8.3144598*TK))*activity(\'H+\')^0.75 + 1.00e-02*EXP(-34800/(8.3144598*TK))*activity(\'H2CO3\')^0.75 + 2.20e-07*EXP(-52200/(8.3144598*TK))*activity(\'H2O\')^0.75) * surface * (1 - QoverK) RETURN rate' pre-exp = ??? act_eng = ??? surface = 200",
     

My problem is that my real model have temperatures up to 220 C and I need to include to higher kinetic rate in some way or other. I have come right with the sliding temperature question I had in the other post and now my model can run at different temperature scenarios. I think chemplugin works great as it surely gives you a lot of flexibility to work with.

Regards

Johan

 

FlowThrough_JF2.py

Link to comment
Share on other sites

Hi Johan,

1) The surface data one happens to work because \P \C \G and \F are not special escape sequences in Python. However in the thermo database path you have a \t which is a special escape sequence that represents a "tab" character. Here is one list of special escape sequences https://docs.python.org/2.0/ref/strings.html . However it is usually easiest if you are using backslashes to just always escape them if you want a literal backslash character in the string - that way you don't need to remember which letters are special. Also some versions of Python will throw an error if you use an invalid escape sequence. You may also use forward slashes when specifying thermo and surface data paths (linux style paths) for ChemPlugin

e.g.

'data = "C:/Program Files/Gwb/Gtdata/thermo_wateq4f.tdat" verify'

2) There are a couple different issues I see in the script, but still mostly Python string related. For your rate law instead of having 'rate = (expression) RETURN rate' you can instead just have it return the expression directly without setting that temporary variable. e.g. 'RETURN (expression)'. Alternatively you could also add newline "\n" before the RETURN rate part since that should be on a separate line. 

a) return value directly:

   "kinetic Calcite .1 volume% rate_law = script 'RETURN (1.49e-02*EXP(-29000/(8.3144598*TK))*activity(\'H+\')^0.75 + 1.00e-02*EXP(-34800/(8.3144598*TK))*activity(\'H2CO3\')^0.75 + 2.20e-07*EXP(-52200/(8.3144598*TK))*activity(\'H2O\')^0.75) * surface * (1 - QoverK) RETURN rate' pre-exp = ??? act_eng = ??? surface = 200",

b) properly separate the lines of script with newline \n:

   "kinetic Calcite .1 volume% rate_law = script 'rate = (1.49e-02*EXP(-29000/(8.3144598*TK))*activity(\'H+\')^0.75 + 1.00e-02*EXP(-34800/(8.3144598*TK))*activity(\'H2CO3\')^0.75 + 2.20e-07*EXP(-52200/(8.3144598*TK))*activity(\'H2O\')^0.75) * surface * (1 - QoverK) \nRETURN rate' pre-exp = ??? act_eng = ??? surface = 200",

 

To get a better feel for Python strings I would recommend opening an interactive Python session and just try things out interactively with the print command. 

Here are the dataset examples:

>>> print('surface_data = "C:\Program Files\ChemPlugin\Gtdata\FeOH.sdat"')
surface_data = "C:\Program Files\ChemPlugin\Gtdata\FeOH.sdat"


>>> print('data = "C:\Program Files\Gwb\Gtdata\thermo_wateq4f.tdat" verify')
data = "C:\Program Files\Gwb\Gtdata     hermo_wateq4f.tdat" verify


>>> print('data = "C:\\Program Files\\Gwb\\Gtdata\\thermo_wateq4f.tdat" verify')
data = "C:\Program Files\Gwb\Gtdata\thermo_wateq4f.tdat" verify

 

One last thing that may also help with Python strings that contain nested single and double quotes is that instead of using one single or double quote to mark string start/end Python also allows you to use three single or double quotes to start and end a string which allows you to then use the same type of quote inside the string without escaping it.

>>> print("""can now freely use unescaped double quotes like these " " inside the string""")
can now freely use unescaped double quotes like these " " inside the string
>>> print('''can now freely use unescaped single quotes like these ' ' inside the string''')
can now freely use unescaped single quotes like these ' ' inside the string

 

Hope this helps,

Dan

 

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...