Re: insert data in python script

2020-02-18 Thread DL Neil via Python-list

On 19/02/20 4:52 AM, alberto wrote:


Hi,
I solve it with external file as follows

import preos
# pass name, Tc, Pc, omega
methane = preos.Molecule("methane", -82.59 + 273.15, 45.99, 0.011)
methane.print_params()

thanks to everyone



It is difficult to learn a new programming language, even for 
professional programmers - and more-so if English is not one's 
native-language. Well done!


Now, if you could tell us how to capture and control methane, perhaps 
the world would become a better place...



Ciao!
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: insert data in python script

2020-02-18 Thread alberto
Il giorno martedì 18 febbraio 2020 11:01:11 UTC+1, David ha scritto:
> On Tue, 18 Feb 2020 at 20:45, alberto  wrote:
> > Il giorno martedì 18 febbraio 2020 09:34:51 UTC+1, DL Neil ha scritto:
> 
> > > > my code preos in one file preos.py
> > > > my commands are
> > > >
> > > > alberto@HENDRIX ~/PREOS $ python3.5
> > > > Python 3.5.2 (default, Oct  8 2019, 13:06:37)
> > > > [GCC 5.4.0 20160609] on linux
> > > > Type "help", "copyright", "credits" or "license" for more information.
> > >  import preos
> > >  methane = Molecule("methane", -82.59 + 273.15, 45.99, 0.011)
> > > > Traceback (most recent call last):
> > > >File "", line 1, in 
> > > > NameError: name 'Molecule' is not defined
> 
> > honestly i don't understand what i have to do.
> 
> $ python3
> Python 3.5.3 (default, Sep 27 2018, 17:25:39)
> [GCC 6.3.0 20170516] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> print(digits)
> Traceback (most recent call last):
>   File "", line 1, in 
> NameError: name 'digits' is not defined
> >>> print(string.digits)
> Traceback (most recent call last):
>   File "", line 1, in 
> NameError: name 'string' is not defined
> >>> import string
> >>> print(digits)
> Traceback (most recent call last):
>   File "", line 1, in 
> NameError: name 'digits' is not defined
> >>> print(string.digits)
> 0123456789
> >>>

Hi, 
I solve it with external file as follows

import preos
# pass name, Tc, Pc, omega
methane = preos.Molecule("methane", -82.59 + 273.15, 45.99, 0.011)
methane.print_params()

thanks to everyone

regards

Alberto
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: insert data in python script

2020-02-18 Thread Michael Torrie
On 2/18/20 2:41 AM, alberto wrote:
> Il giorno martedì 18 febbraio 2020 09:34:51 UTC+1, DL Neil ha scritto:
>> The first instruction (immediately above) imports the module preos.py. 
>> That works (no error message!).
>>
>> The second instruction refers to a Python class called Molecule. That 
>> fails. The error message says that 'Molecule' is not defined.
>>
>> Yet we can 'see' it. It *has* been defined! What is going on???
>>
>> In this case, we need to tell Python that Molecule is part of the preos 
>> module. So back to your original code (top):
>>
>> methane = preos.Molecule("methane", -82.59 + 273.15, 45.99, 0.011)
^^
He explicitly told you what to do.

The problem was "Molecule" is part of the "preos" module, so you have to
refer to it by the module name first.

>> Please refer to earlier message. If Module were called from code in the 
>> preos.py file, then the "preos." prefix would not be necessary.
>>
>> The formal term for this situation is "namespaces". Because Molecule is 
>> defined within the preos.py module's namespace we need to tell Python 
>> exactly where Molecule can be found. In the same way that we might say: 
>> if someone in Antarctica wants to see Alberto, (s)he will have to go to 
>> Italy to find him...
>>
>>
>> Don't hesitate to say if you think my reply is too complicated/advanced. 
>> People here are happy to help...
>> -- 
>> Regards =dn
> 
> hi
> honestly i don't understand what i have to do.
> I have been using python for too little time.
> could you help me understand

Did you read everything DL Neil said?  He told you what to do, including
giving you the line of code you need to replace, but also explained why.
 I don't know how one could make it more clear than what he said.

Spend some time to really try to understand what DL Neil is saying.
this is essential Python knowledge he's trying to impart.  Learning to
work with modules and namespaces in Python is pretty much required when
learning and using Python.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: insert data in python script

2020-02-18 Thread David
On Tue, 18 Feb 2020 at 20:45, alberto  wrote:
> Il giorno martedì 18 febbraio 2020 09:34:51 UTC+1, DL Neil ha scritto:

> > > my code preos in one file preos.py
> > > my commands are
> > >
> > > alberto@HENDRIX ~/PREOS $ python3.5
> > > Python 3.5.2 (default, Oct  8 2019, 13:06:37)
> > > [GCC 5.4.0 20160609] on linux
> > > Type "help", "copyright", "credits" or "license" for more information.
> >  import preos
> >  methane = Molecule("methane", -82.59 + 273.15, 45.99, 0.011)
> > > Traceback (most recent call last):
> > >File "", line 1, in 
> > > NameError: name 'Molecule' is not defined

> honestly i don't understand what i have to do.

$ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(digits)
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'digits' is not defined
>>> print(string.digits)
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'string' is not defined
>>> import string
>>> print(digits)
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'digits' is not defined
>>> print(string.digits)
0123456789
>>>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: insert data in python script

2020-02-18 Thread alberto
Il giorno martedì 18 febbraio 2020 09:34:51 UTC+1, DL Neil ha scritto:
> ...
> 
> >> import preos
> >> # pass name, Tc, Pc, omega
> >> methane = preos.Molecule("methane", -82.59 + 273.15, 45.99, 0.011)
> >> methane.print_params()
> 
> ...
> 
> > my code preos in one file preos.py
> > my commands are
> > 
> > alberto@HENDRIX ~/PREOS $ python3.5
> > Python 3.5.2 (default, Oct  8 2019, 13:06:37)
> > [GCC 5.4.0 20160609] on linux
> > Type "help", "copyright", "credits" or "license" for more information.
>  import preos
>  methane = Molecule("methane", -82.59 + 273.15, 45.99, 0.011)
> > Traceback (most recent call last):
> >File "", line 1, in 
> > NameError: name 'Molecule' is not defined
> 
> 
> The first instruction (immediately above) imports the module preos.py. 
> That works (no error message!).
> 
> The second instruction refers to a Python class called Molecule. That 
> fails. The error message says that 'Molecule' is not defined.
> 
> Yet we can 'see' it. It *has* been defined! What is going on???
> 
> In this case, we need to tell Python that Molecule is part of the preos 
> module. So back to your original code (top):
> 
> methane = preos.Molecule("methane", -82.59 + 273.15, 45.99, 0.011)
> 
> 
> Please refer to earlier message. If Module were called from code in the 
> preos.py file, then the "preos." prefix would not be necessary.
> 
> The formal term for this situation is "namespaces". Because Molecule is 
> defined within the preos.py module's namespace we need to tell Python 
> exactly where Molecule can be found. In the same way that we might say: 
> if someone in Antarctica wants to see Alberto, (s)he will have to go to 
> Italy to find him...
> 
> 
> Don't hesitate to say if you think my reply is too complicated/advanced. 
> People here are happy to help...
> -- 
> Regards =dn

hi
honestly i don't understand what i have to do.
I have been using python for too little time.
could you help me understand

regards 

Alberto

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: insert data in python script

2020-02-18 Thread DL Neil via Python-list

...


import preos
# pass name, Tc, Pc, omega
methane = preos.Molecule("methane", -82.59 + 273.15, 45.99, 0.011)
methane.print_params()


...


my code preos in one file preos.py
my commands are

alberto@HENDRIX ~/PREOS $ python3.5
Python 3.5.2 (default, Oct  8 2019, 13:06:37)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.

import preos
methane = Molecule("methane", -82.59 + 273.15, 45.99, 0.011)

Traceback (most recent call last):
   File "", line 1, in 
NameError: name 'Molecule' is not defined



The first instruction (immediately above) imports the module preos.py. 
That works (no error message!).


The second instruction refers to a Python class called Molecule. That 
fails. The error message says that 'Molecule' is not defined.


Yet we can 'see' it. It *has* been defined! What is going on???

In this case, we need to tell Python that Molecule is part of the preos 
module. So back to your original code (top):


methane = preos.Molecule("methane", -82.59 + 273.15, 45.99, 0.011)


Please refer to earlier message. If Module were called from code in the 
preos.py file, then the "preos." prefix would not be necessary.


The formal term for this situation is "namespaces". Because Molecule is 
defined within the preos.py module's namespace we need to tell Python 
exactly where Molecule can be found. In the same way that we might say: 
if someone in Antarctica wants to see Alberto, (s)he will have to go to 
Italy to find him...



Don't hesitate to say if you think my reply is too complicated/advanced. 
People here are happy to help...

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: insert data in python script

2020-02-17 Thread alberto
Il giorno lunedì 17 febbraio 2020 17:48:07 UTC+1, alberto ha scritto:
> Hi, 
> I would use this script to evaluate fugacity coefficient with PENG-ROBINSON 
> equation, but I don't understand the correct mode to insert data
> 
> import numpy as np
> import matplotlib.pyplot as plt
> from scipy.optimize import newton
> 
> R = 8.314e-5  # universal gas constant, m3-bar/K-mol
> class Molecule:
> """
> Store molecule info here
> """
> def __init__(self, name, Tc, Pc, omega):
> """
> Pass parameters desribing molecules
> """
> #! name
> self.name = name
> #! Critical temperature (K)
> self.Tc = Tc
> #! Critical pressure (bar)
> self.Pc = Pc
> #! Accentric factor
> self.omega = omega
> 
> def print_params(self):
> """
> Print molecule parameters.
> """
> print("""Molecule: %s.
> \tCritical Temperature = %.1f K
> \tCritical Pressure = %.1f bar.
> \tAccentric factor = %f""" % (self.name, self.Tc, self.Pc, 
> self.omega))
> 
> def preos(molecule, T, P, plotcubic=True, printresults=True):
> """
> Peng-Robinson equation of state (PREOS)
> 
> http://en.wikipedia.org/wiki/Equation_of_state#Peng.E2.80.93Robinson_equation_of_state
> :param molecule: Molecule molecule of interest
> :param T: float temperature in Kelvin
> :param P: float pressure in bar
> :param plotcubic: bool plot cubic polynomial in compressibility factor
> :param printresults: bool print off properties
> Returns a Dict() of molecule properties at this T and P.
> """
> # build params in PREOS
> Tr = T / molecule.Tc  # reduced temperature
> a = 0.457235 * R**2 * molecule.Tc**2 / molecule.Pc
> b = 0.0777961 * R * molecule.Tc / molecule.Pc
> kappa = 0.37464 + 1.54226 * molecule.omega - 0.26992 * molecule.omega**2
> alpha = (1 + kappa * (1 - np.sqrt(Tr)))**2
> 
> A = a * alpha * P / R**2 / T**2
> B = b * P / R / T
> 
> # build cubic polynomial
> def g(z):
> """
> Cubic polynomial in z from EOS. This should be zero.
> :param z: float compressibility factor
> """
> return z**3 - (1 - B) * z**2 + (A - 2*B - 3*B**2) * z - (
> A * B - B**2 - B**3)
> 
> # Solve cubic polynomial for the compressibility factor
> z = newton(g, 1.0)  # compressibility factor
> rho = P / (R * T * z)  # density
> 
> # fugacity coefficient comes from an integration
> fugacity_coeff = np.exp(z - 1 - np.log(z - B) - A / np.sqrt(8) / B * 
> np.log(
> (z + (1 + np.sqrt(2)) * B) / (z + (1 - np.sqrt(2)) * B)))
> 
> if printresults:
> print("""PREOS calculation at
> \t T = %.2f K
> \t P = %.2f bar""" % (T, P))
> print("\tCompressibility factor : ", z)
> print("\tFugacity coefficient: ", fugacity_coeff)
> print("\tFugacity at pressure %.3f bar = %.3f bar" % (
> P, fugacity_coeff * P))
> print("\tDensity: %f mol/m3" % rho)
> print("\tMolar volume: %f L/mol" % (1.0 / rho * 1000))
> print("\tDensity: %f v STP/v" % (rho * 22.4 / 1000))
> print("\tDensity of ideal gas at same conditions: %f v STP/v" % (
> rho * 22.4/ 1000 * z))
> 
> if plotcubic:
> # Plot the cubic equation to visualize the roots
> zz = np.linspace(0, 1.5)  # array for plotting
> 
> plt.figure()
> plt.plot(zz, g(zz), color='k')
> plt.xlabel('Compressibility, $z$')
> plt.ylabel('Cubic $g(z)$')
> plt.axvline(x=z)
> plt.axhline(y=0)
> plt.title('Root found @ z = %.2f' % z)
> plt.show()
> return {"density(mol/m3)": rho, "fugacity_coefficient": fugacity_coeff,
> "compressibility_factor": z, "fugacity(bar)": fugacity_coeff * P,
> "molar_volume(L/mol)": 1.0 / rho * 1000.0}
> 
> def preos_reverse(molecule, T, f, plotcubic=False, printresults=True):
> """
> Reverse Peng-Robinson equation of state (PREOS) to obtain pressure for a 
> particular fugacity
> :param molecule: Molecule molecule of interest
> :param T: float temperature in Kelvin
> :param f: float fugacity in bar
> :param plotcubic: bool plot cubic polynomial in compressibility factor
> :param printresults: bool print off properties
> Returns a Dict() of molecule properties at this T and f.
> """
> # build function to minimize: difference between desired fugacity and 
> that obtained from preos
> def g(P):
> """
> :param P: pressure
> """
> return (f - preos(molecule, T, P, plotcubic=False, 
> printresults=False)["fugacity(bar)"])
> 
> # Solve preos for the pressure
> P = newton(g, f)  # pressure
> 
> # Obtain remaining parameters
> pars = preos(molecule, T, P, plotcubic=plotcubic, 
> printresults=printresults)
> rho = pars["density(mol

Re: insert data in python script

2020-02-17 Thread DL Neil via Python-list

Please help us to help you!
1 is all of this code in a single file or spread across (at least) two 
modules? What are their names? What is the directory structure?

2 copy-paste the actual error message received.


It works for me!
1 not knowing your circumstances, I put all the code in one file
2 updated one line (possibly due to above)

# methane = preos.Molecule("methane", -82.59 + 273.15, 45.99, 0.011)
methane = Molecule("methane", -82.59 + 273.15, 45.99, 0.011)

dn $ python3 Projects/molecule.py
Molecule: methane.
Critical Temperature = 190.6 K
Critical Pressure = 46.0 bar.
Accentric factor = 0.011000


NB if the bulk of the code is stored in a module named preos.py then it 
is necessary to first:


import peos

and restore the above change.

NBB under such conditions peos.py must be located in the file-system 
where the 'mainline' can find (and import) it!

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list