Hi All,
I am trying to rewrite a unit operation written in Matlab to Ipython. It requires functionality from scipy and numpy. Is there any way to import scipy and numpy modules in DWSIM IPython scripting. I have added the paths (python installation directory and numpy directory) in the scripts tab of general settings in DWSIM, still I am getting "No Module name numpy" in the console window. Please help me resolve this issue.
Thanks in advance.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I would be grateful for a basic example on how to use the linear solvers in an Ironpython script in DWSIM. The lack of a useful Ironpython documentation makes me struggling with the array and matrix definitions, resulting in 'not callable' objects. Hereby, I am not sure if it is a problem of using the wrong types or a wrong call of the respective functions (e.g. solve from Mapack). Looking for solutions on the web did not provide any useful solutions.
As an alternative, installing Ironpyhon via the Entought website as posted above with numpy and scipy works well, but DWSIM throws an exception when I call a script from the Script Manager with that version installed. Hence, I guess that this approach will not work without modifications of the DWSIM source.
Thanks, Hans
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You posted a link to the procedure to retrieve the latest available IronPython with numpy and scipy before (by user Nilster at stackoverflow.com):
For those struggling to get numpy/scipy install for ironpythopn, enthought have moved the download link to https://store.enthought.com/repo/.iron/ . The link would only allow you in if you are registered.
Therefore first up you'd have to register yourself for free, then open the above link, then follow the steps below
Download the IronPython-2.7.msi and install it.
Download ironpkg-1.0.0.py from the above link.
Using command line navigate to the directory where you placed ironpkg-1.0.0.py and run ipy ironpkg-1.0.0.py --install Check whether the install worked using ironpkg -h
The last step is lightly different to the one suggested by enthoughts. Running ironpkg scipy won't work as it looks at the old web address for download. Instead download all the eggs and index-depend.txt from the above link. For installation to work, you would have to modify the download location in the config file to point to the local drive instead of website. The config file can be found at user directory eg.C:\Users\Nilster.ironpkg . Open it in the textpad and change the location to directory where you downloaded the eggs Eg, mine looks like
IndexedRepos = ['file://C:\Work\Python\Enthought_Eggs',]
Then run the following to install numpy/scipy ironpkg scipy
Check whether the install worked using ipy -X:Frames -c "import scipy"
Best, Hans
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Btw., that is the error DWSIM throws out when having the aforementioned IronPython version installed (run from Script Manager):
System.Reflection.TargetInvocationException: Failed to load language 'IronPython 2.7.5': Method not found: 'System.Collections.Generic.IEnumerable1<!!0> Microsoft.Scripting.Utils.ReflectionUtils.GetCustomAttributes(System.Reflection.Assembly, Boolean)'. ---> System.MissingMethodException: Method not found: 'System.Collections.Generic.IEnumerable1<!!0> Microsoft.Scripting.Utils.ReflectionUtils.GetCustomAttributes(System.Reflection.Assembly, Boolean)'.
at IronPython.Runtime.Binding.PythonBinder.DomainManager_AssemblyLoaded(Object sender, AssemblyLoadedEventArgs e)
at IronPython.Runtime.Binding.PythonBinder..ctor(PythonContext pythonContext, CodeContext context)
at IronPython.Runtime.PythonContext..ctor(ScriptDomainManager manager, IDictionary`2 options)
--- End of inner exception stack trace ---
at Microsoft.Scripting.Runtime.LanguageConfiguration.LoadLanguageContext(ScriptDomainManager domainManager, Boolean& alreadyLoaded)
at Microsoft.Scripting.Runtime.DlrConfiguration.LoadLanguageContext(ScriptDomainManager manager, LanguageConfiguration config)
at Microsoft.Scripting.Runtime.DlrConfiguration.TryLoadLanguage(ScriptDomainManager manager, AssemblyQualifiedTypeName providerName, LanguageContext& language)
at Microsoft.Scripting.Runtime.ScriptDomainManager.GetLanguageByTypeName(String providerAssemblyQualifiedTypeName)
at Microsoft.Scripting.Hosting.ScriptRuntime.GetEngineByTypeName(String assemblyQualifiedTypeName)
at DWSIM.FormScript.RunScript(String scripttext, FormFlowsheet fsheet) in DWSIM\Forms\FormScript.vb:line 111
at DWSIM.FormScript.Button1_Click(Object sender, EventArgs e) in DWSIM\Forms\FormScript.vb:line 102
at DWSIM.FormScript.scriptcontrol_KeyDown(Object sender, KeyEventArgs e) in DWSIM\Forms\FormScript.vb:line 546
at System.Windows.Forms.Control.OnKeyDown(KeyEventArgs e)
at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If you remove the ironpython version that you installed in the GAC (v 2.7) and keep numpy and scipy on the system and then reference these packages in your script, it could work...
I did some testing and was able to address the related libraries and DLLs. However, I guess that the different ipy versions avoid this to work properly (I cannot import numpy). Hence, one would have to compile numpy for ipy, using a quite complex workflow without any available reports on successful accomplishment. This is actually too much effort for me at this time.
Coming back to my first question, if you can provide a simple example on how to use one of the linear solvers provided with DWSIM to solve a simple linear system of equations from the Script Manager, I would be very grateful and would have all necessary tools available to work with the Script Manager.
Best, Hans
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I see. If you only need a syslin solver, there are some options in DWSIM. I'l try something tomorrow and let you know when I manage to have a working script.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
For now, it is just the syslin solver. I guess that I will be able to abstract any other assembly application from that example. Many thanks in advance!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Great, I knew it is just a matter of asking someone who is highly familiar with .NET. This is definitely not the case for me. Now I have everything at hand I need for productive use of ipy in DWSIM.
Many thanks!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi, Daniel
would it be possible to add an optimization example from DotNumerics?
I tried to do it my self, but still obtain an error during running following script in DWSIM manager(IronPython):
line 15
TypeError: expected OptMultivariateFunction, got function
While running this code in Python 2.7, I get this error:
minimum = simplex.ComputeMin(BananaFunction,initialGuess)
TypeError: No method matches given arguments
Do you have any suggestion where is the fault? I need to optimize more complicated problem, but I can not deal with such simple one.
Many thanks for any suggestion.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You'd need to fix the reference to DotNumerics and add a few data conversion lines. math from python doesn't work (don't know why), but you can use the Math class from .NET.
I get the Error: TypeError: BananaFunction() takes exactly 3 arguments (1 given)
However I do not know how to send arguments (par1 and par2) to the function ComputeMin.
I am thinking about using global variables to set par1 and par2 in BananaFunction, but there must be any other way how to run ComputeMin with function defined by several parameters.
Could you pleas help me once again?
ComputeMin() requires a function with only one argument (a vector of doubles). You can either use global variables or, if the additional parameters are also of the double type, you can add them at the end of the vector and separate/process the variables inside BananaFunction().
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Daniel,
Can u please post an example using the ODE Solver too. I tried the sample program given in the DotNumerics website under the explicit Runge-Kutta method. Im able to run it without any errors but the answer i get are always zero. Thanks in advance.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Daniel,
This is code that i have used. I am able to call the ODE solver without any problems and it is similar to what is present in the link you sent me. I still have trouble accesing the result of the solve() function. Do i need to use any function like MatrixToString() to access it?
Hi All,
I am trying to rewrite a unit operation written in Matlab to Ipython. It requires functionality from scipy and numpy. Is there any way to import scipy and numpy modules in DWSIM IPython scripting. I have added the paths (python installation directory and numpy directory) in the scripts tab of general settings in DWSIM, still I am getting "No Module name numpy" in the console window. Please help me resolve this issue.
Thanks in advance.
Hi,
You could try this: http://stackoverflow.com/questions/29397540/how-to-install-numpy-and-scipy-for-ironpython27-old-method-doenst-work
Which specific scipy/numpy functions are you using? Maybe you can replace them with .NET-equivalents.
Hi Daniel,
Thank you very much for your reply. I am looking for ode and linear system solvers. I will look into .NET equivalents.
DWSIM already has these solvers, take a look at:
ODE solver: https://github.com/DanWBR/dwsim3/blob/master/DWSIM/Objects/Classes/Math/BS.vb
ODE solver usage example: https://github.com/DanWBR/dwsim3/blob/master/DWSIM/Objects/Reactors/PFR.vb, line 570
Linear solver lpsolve: https://github.com/DanWBR/dwsim3/blob/master/DWSIM/Objects/Classes/Math/LP_Solve.vb
lpsolve usage: https://github.com/DanWBR/dwsim3/blob/master/DWSIM/Objects/Reactors/Gibbs.vb, lines 926 to 968
Other linear solvers: https://github.com/DanWBR/dwsim3/blob/master/DWSIM/Objects/Classes/Math/SysLin.vb
Hi Daniel,
Thanks for the infomation. I will go through them. I have a small doubt, can we use these in the Ipython scripting module?
Thanking you.
Sure you can. I'll see if I can come up with a sample for you.
Hi Daniel,
I would be grateful for a basic example on how to use the linear solvers in an Ironpython script in DWSIM. The lack of a useful Ironpython documentation makes me struggling with the array and matrix definitions, resulting in 'not callable' objects. Hereby, I am not sure if it is a problem of using the wrong types or a wrong call of the respective functions (e.g. solve from Mapack). Looking for solutions on the web did not provide any useful solutions.
As an alternative, installing Ironpyhon via the Entought website as posted above with numpy and scipy works well, but DWSIM throws an exception when I call a script from the Script Manager with that version installed. Hence, I guess that this approach will not work without modifications of the DWSIM source.
Thanks, Hans
Hi Hans,
Where can I download this version?
Daniel,
You posted a link to the procedure to retrieve the latest available IronPython with numpy and scipy before (by user Nilster at stackoverflow.com):
For those struggling to get numpy/scipy install for ironpythopn, enthought have moved the download link to https://store.enthought.com/repo/.iron/ . The link would only allow you in if you are registered.
Therefore first up you'd have to register yourself for free, then open the above link, then follow the steps below
IndexedRepos = ['file://C:\Work\Python\Enthought_Eggs',]
Best, Hans
Btw., that is the error DWSIM throws out when having the aforementioned IronPython version installed (run from Script Manager):
System.Reflection.TargetInvocationException: Failed to load language 'IronPython 2.7.5': Method not found: 'System.Collections.Generic.IEnumerable
1<!!0> Microsoft.Scripting.Utils.ReflectionUtils.GetCustomAttributes(System.Reflection.Assembly, Boolean)'. ---> System.MissingMethodException: Method not found: 'System.Collections.Generic.IEnumerable
1<!!0> Microsoft.Scripting.Utils.ReflectionUtils.GetCustomAttributes(System.Reflection.Assembly, Boolean)'.at IronPython.Runtime.Binding.PythonBinder.DomainManager_AssemblyLoaded(Object sender, AssemblyLoadedEventArgs e)
at IronPython.Runtime.Binding.PythonBinder..ctor(PythonContext pythonContext, CodeContext context)
at IronPython.Runtime.PythonContext..ctor(ScriptDomainManager manager, IDictionary`2 options)
--- End of inner exception stack trace ---
at Microsoft.Scripting.Runtime.LanguageConfiguration.LoadLanguageContext(ScriptDomainManager domainManager, Boolean& alreadyLoaded)
at Microsoft.Scripting.Runtime.DlrConfiguration.LoadLanguageContext(ScriptDomainManager manager, LanguageConfiguration config)
at Microsoft.Scripting.Runtime.DlrConfiguration.TryLoadLanguage(ScriptDomainManager manager, AssemblyQualifiedTypeName providerName, LanguageContext& language)
at Microsoft.Scripting.Runtime.ScriptDomainManager.GetLanguageByTypeName(String providerAssemblyQualifiedTypeName)
at Microsoft.Scripting.Hosting.ScriptRuntime.GetEngineByTypeName(String assemblyQualifiedTypeName)
at DWSIM.FormScript.RunScript(String scripttext, FormFlowsheet fsheet) in DWSIM\Forms\FormScript.vb:line 111
at DWSIM.FormScript.Button1_Click(Object sender, EventArgs e) in DWSIM\Forms\FormScript.vb:line 102
at DWSIM.FormScript.scriptcontrol_KeyDown(Object sender, KeyEventArgs e) in DWSIM\Forms\FormScript.vb:line 546
at System.Windows.Forms.Control.OnKeyDown(KeyEventArgs e)
at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
If you remove the ironpython version that you installed in the GAC (v 2.7) and keep numpy and scipy on the system and then reference these packages in your script, it could work...
http://stackoverflow.com/a/1372777/1771296
Thanks for the suggestion, Daniel.
I did some testing and was able to address the related libraries and DLLs. However, I guess that the different ipy versions avoid this to work properly (I cannot import numpy). Hence, one would have to compile numpy for ipy, using a quite complex workflow without any available reports on successful accomplishment. This is actually too much effort for me at this time.
Coming back to my first question, if you can provide a simple example on how to use one of the linear solvers provided with DWSIM to solve a simple linear system of equations from the Script Manager, I would be very grateful and would have all necessary tools available to work with the Script Manager.
Best, Hans
I see. If you only need a syslin solver, there are some options in DWSIM. I'l try something tomorrow and let you know when I manage to have a working script.
For now, it is just the syslin solver. I guess that I will be able to abstract any other assembly application from that example. Many thanks in advance!
Got the linear solver from the DotNumerics library working very easily. I've reproduced this example in IronPython: http://www.dotnumerics.com/NumericalLibraries/Samples/LinearAlgebra/Linear-Equations.aspx
The
Matrix
class has a few handy functions/methods, you may want to take a look at the library documentation: http://www.dotnumerics.com/Downloads.aspxI've added this library to DWSIM mainly for the optimization classes. I haven't used the linear system solver until now. It seems very easy to use...
Regards
Daniel
Last edit: Daniel Medeiros 2016-11-07
Great, I knew it is just a matter of asking someone who is highly familiar with .NET. This is definitely not the case for me. Now I have everything at hand I need for productive use of ipy in DWSIM.
Many thanks!
Hi, Daniel
would it be possible to add an optimization example from DotNumerics?
I tried to do it my self, but still obtain an error during running following script in DWSIM manager(IronPython):
line 15
TypeError: expected OptMultivariateFunction, got function
My code:
While running this code in Python 2.7, I get this error:
minimum = simplex.ComputeMin(BananaFunction,initialGuess)
TypeError: No method matches given arguments
Do you have any suggestion where is the fault? I need to optimize more complicated problem, but I can not deal with such simple one.
Many thanks for any suggestion.
Hi Jiri,
You'd need to fix the reference to DotNumerics and add a few data conversion lines. math from python doesn't work (don't know why), but you can use the Math class from .NET.
Output is
Regards,
Daniel
Hi Daniel,
that is great, it works, thanks a lot. I did not realize it is possible to set function as OptMultivariateFunction.
Once again thank you.
Hi Daniel, I have one more question. Is there any way how to call "BananaFunction" defined with several parameters?
I.E.
While I try to run ComputeMin:
I get the Error: TypeError: BananaFunction() takes exactly 3 arguments (1 given)
However I do not know how to send arguments (par1 and par2) to the function ComputeMin.
I am thinking about using global variables to set par1 and par2 in BananaFunction, but there must be any other way how to run ComputeMin with function defined by several parameters.
Could you pleas help me once again?
The whole code:
Hi Jiri,
ComputeMin() requires a function with only one argument (a vector of doubles). You can either use global variables or, if the additional parameters are also of the double type, you can add them at the end of the vector and separate/process the variables inside BananaFunction().
Hi Daniel,
Can u please post an example using the ODE Solver too. I tried the sample program given in the DotNumerics website under the explicit Runge-Kutta method. Im able to run it without any errors but the answer i get are always zero. Thanks in advance.
Hi Charan,
In the PFR model code you can check how the ODE solver is called: https://github.com/DanWBR/dwsim5/blob/master/DWSIM.UnitOperations/Reactors/PFR.vb#L560
Daniel,
This is code that i have used. I am able to call the ODE solver without any problems and it is similar to what is present in the link you sent me. I still have trouble accesing the result of the solve() function. Do i need to use any function like MatrixToString() to access it?
Try this at the end: