Menu

Using numpy and scipy modules in DWSIM

2015-12-01
2024-04-20
1 2 > >> (Page 1 of 2)
  • HarshaVardhanBabu

    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.

     
  • Daniel Medeiros

    Daniel Medeiros - 2015-12-01

    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.

     
    • HarshaVardhanBabu

      Hi Daniel,
      Thank you very much for your reply. I am looking for ode and linear system solvers. I will look into .NET equivalents.

       
  • Hans Eisenstein

    Hans Eisenstein - 2016-11-06

    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

     
  • Daniel Medeiros

    Daniel Medeiros - 2016-11-06

    Hi Hans,

    Where can I download this version?

     
  • Hans Eisenstein

    Hans Eisenstein - 2016-11-06

    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

    1. Download the IronPython-2.7.msi and install it.
    2. Download ironpkg-1.0.0.py from the above link.
    3. 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
    4. 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',]
    5. Then run the following to install numpy/scipy ironpkg scipy
    6. Check whether the install worked using ipy -X:Frames -c "import scipy"

    Best, Hans

     
  • Hans Eisenstein

    Hans Eisenstein - 2016-11-06

    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)

     
  • Daniel Medeiros

    Daniel Medeiros - 2016-11-06

    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

     
  • Hans Eisenstein

    Hans Eisenstein - 2016-11-06

    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

     
    • Daniel Medeiros

      Daniel Medeiros - 2016-11-06

      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.

       
  • Hans Eisenstein

    Hans Eisenstein - 2016-11-07

    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!

     
  • Daniel Medeiros

    Daniel Medeiros - 2016-11-07

    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

    import clr
    import sys
    
    clr.AddReference('DWSIM.MathOps.DotNumerics')
    
    from DotNumerics import *
    from DotNumerics.LinearAlgebra import *
    
    A = Matrix(3, 3)
    A[0, 0] = 2; A[0, 1] = 5; A[0, 2] = 3;
    A[1, 0] = 1; A[1, 1] = 5; A[1, 2] = 7;
    A[2, 0] = 8; A[2, 1] = 2; A[2, 2] = 3;
    
    B = Matrix(3, 1)
    B[0, 0] = 5
    B[1, 0] = 3
    B[2, 0] = 8
    
    leq = LinearEquations();
    X = leq.Solve(A, B);
    
    print X.MatrixToString()
    

    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.aspx

    I'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
  • Hans Eisenstein

    Hans Eisenstein - 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!

     
  • Jiri Nepovim

    Jiri Nepovim - 2016-12-15

    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:

    import clr
    clr.AddReference('DotNumerics')
    
    from DotNumerics import *
    from DotNumerics.Optimization import *
    
    def BananaFunction(f,x):
        f=100*math.pow((x[1] - x[0] * x[0]), 2) + math.pow((1 - x[0]), 2);
        return f
    
    initialGuess = [0.0 for i in range (2)]
    initialGuess[0]=0.1
    initialGuess[1]=2
    simplex = Simplex()
    minimum = simplex.ComputeMin(BananaFunction,initialGuess)
    

    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.

     
  • Daniel Medeiros

    Daniel Medeiros - 2016-12-15

    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.

    import clr
    clr.AddReference('DWSIM.MathOps.DotNumerics')
    
    from DotNumerics import *
    from DotNumerics.Optimization import *
    from System import Array, Math
    
    def BananaFunction(x):
        f = 100 * Math.Pow((x[1] - x[0] * x[0]), 2) + Math.Pow((1 - x[0]), 2)
        return f
    
    f = OptMultivariateFunction(BananaFunction)
    
    initialGuess = [0.0 for i in range (2)]
    initialGuess[0]=0.1
    initialGuess[1]=2
    
    ival = Array[float](initialGuess)
    
    simplex = Simplex()
    minimum = simplex.ComputeMin(f, ival)
    
    print minimum
    

    Output is

    Array[float]((0.99999999796052164, 0.99999999561733155))
    

    Regards,
    Daniel

     
  • Jiri Nepovim

    Jiri Nepovim - 2016-12-16

    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.

     
  • Jiri Nepovim

    Jiri Nepovim - 2016-12-20

    Hi Daniel, I have one more question. Is there any way how to call "BananaFunction" defined with several parameters?
    I.E.

    par1 = 100
    par2 = 1
    def BananaFunction(x,par1,par2):
        f = par1 * Math.Pow((x[1] - x[0] * x[0]), 2) + Math.Pow((par2 - x[0]), 2)
        return f
    

    While I try to run ComputeMin:

    minimum = simplex.ComputeMin(f, ival)
    

    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:

    import clr
    clr.AddReference('DWSIM.MathOps.DotNumerics')
    
    from DotNumerics import *
    from DotNumerics.Optimization import *
    from System import Array, Math
    par1=100
    par2=1
    def BananaFunction(x,par1,par2):
    
        f = par1 * Math.Pow((x[1] - x[0] * x[0]), 2) + Math.Pow((par2 - x[0]), 2)
    
        return f
    
    f = OptMultivariateFunction(BananaFunction)
    
    initialGuess = [0.0 for i in range (2)]
    initialGuess[0]=0.1
    initialGuess[1]=2
    
    ival = Array[float](initialGuess)
    print(ival)
    
    simplex = Simplex()
    minimum = simplex.ComputeMin(f, ival)
    
    print minimum
    
     
  • Daniel Medeiros

    Daniel Medeiros - 2016-12-20

    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().

     
  • Charan

    Charan - 2018-06-08

    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.

     
  • Charan

    Charan - 2018-06-08

    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?

    yprime =[0,0,0]
    def ode(t,y):
        yprime[0] = y[1] * y[2] *t
        yprime[1] = -y[0] * y[2]
        yprime[2] = -0.51 * y[0] * y[1]
        return Array[float](yprime)
    
    func = OdeFunction(ode)
    init = OdeExplicitRungeKutta45(func,3)
    init.InitializeODEs(func,3)
    x = Array[float]([0,0,0])
    ans = init.Solve(x,0,1,15)
    print ans
    
     
  • Daniel Medeiros

    Daniel Medeiros - 2018-06-08

    Try this at the end:

    ans2 = []
    
    for i in ans:
        ans2.append(i)
    
    print ans2
    
     
1 2 > >> (Page 1 of 2)

Log in to post a comment.