Comments (4)
Hello Miles,
I chose not to build each function around numpy for performance reasons and to simplify the code. Haaland and many other functions are straightforward, but others use if statements, loops, and all sorts of stuff that would be painful to support the many different broadcasting rule cases. The following snippet shows the difference in speed:
from math import *
def Haaland(Re, eD):
ff = (-3.6*log10(6.9/Re +(eD/3.7)**1.11))**-2
return 4*ff
%timeit [Haaland(x, 0.0001/0.01) for x in [1e5, 2e5, 3e5]]
100000 loops, best of 3: 2.39 µs per loop
from numpy import log10
import numpy as np
def Haaland2(Re, eD):
ff = (-3.6*log10(6.9/Re +(eD/3.7)**1.11))**-2
return 4*ff
%timeit Haaland2(np.array([1e5, 2e5, 3e5]),0.0001/0.01)
100000 loops, best of 3: 12.8 µs per loop
Furthermore, pure python can be accelerated by pypy and is more often portable to other Python implementations, even if the library as a whole is not.
However, I am a pragmatist and supporting numpy arrays is something I would like fluids to be able to do. I realize few Python users develop applications where performance ends up being concern. I spend quite some time trying to develop an alternative math library I called anymath which could support numpy, sympy, mpmath, pint, and uncertainties. It had a little overhead, but the larger issue was that it made my code really ugly and not all math libraries supported all features; I've abandoned it for now.
I have therefore provided a new module, fluids.vectorized, which exports all of the functions in fluids after wrapping them in np.vectorize. It is available in pypi now. I hope you find it useful and can understand the design decision. The documentation is available here. Please feel free to report further issues.
from fluids.
Hi Caleb,
Thanks for the response. I do care about speed, but I am not sure if the above code is fair when looking at larger arrays. If you look at arrays of length 10+, there seems to be a crossover where numpy becomes faster. Maybe I am missing something? See this code for a length of 20:
import numpy as np
import math
def Haaland(Re, eD):
ff = (-3.6*math.log10(6.9/Re +(eD/3.7)**1.11))**-2
return 4*ff
def Haaland2(Re, eD):
ff = (-3.6*np.log10(6.9/Re +(eD/3.7)**1.11))**-2
return 4*ff
%timeit [Haaland(x, 0.0001/0.01) for x in [1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5]]
%timeit Haaland2(np.array([1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5, 1e5]) ,0.0001/0.01)
100000 loops, best of 3: 18.9 µs per loop
100000 loops, best of 3: 12.2 µs per loop
Thanks for pointing me to fluids.vectorized, but I tried using:
import fluids.vectorized
from fluids.vectorized import *
and got the following error:
Traceback (most recent call last):
File "C:\Users\miles\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 1, in
from fluids.vectorized import *
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.2\helpers\pydev_pydev_bundle\pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ImportError: No module named vectorized
Any ideas what the issue might be? Thanks again for the help!
Miles
from fluids.
Hello Miles,
Have you upgrade fluids to 0.1.61? What is the output from the following: "fluids.version"? You may need to close and restart pycharm? If you have upgraded and it still does not work that's definitely a problem. I added unit testing for fluids.vectorized and the tests passed and are run on Windows, Linux, and OSX through Travis CI and Appveyor.
I agree that numpy yields higher performance for large input sizes, even as small as 20. Normally it is nor parallel performance that's critical though - it's performance in series; for example, integrating pressure drop across the length of a pipe. The larger reason is that dealing with the complexity supporting numpy requires would be painful; porting something like this to work with all numpy's broadcasting would be a painful, and I'm not sure it would have any more performance than the naive version I've created in fluids.vectorized.
Sincerely,
Caleb
from fluids.
It looks like I had 0.1.60 from when I installed the ht package. I uninstalled it and re-installed fluids, and it works! Thanks for the help. I did check the Haaland from the vectorized package, and it is about 5X slower than Haaland2 above, but no worries. I can custom make functions when needed. This was a useful discussion.
Have you ever considered using a dynamically defined package? e.g.:
import numpy as np
import math
def Haaland(Re, eD, package=math):
ff = (-3.6*package.log10(6.9/Re +(eD/3.7)**1.11))**-2
return 4*ff
I sometimes find that useful. You can also do cool things like package=sympy for documentation (although it can get tricky when functions have different names in different packages, which is actually the case of log10, which is non-existent in sympy).
Miles
from fluids.
Related Issues (20)
- Units issue in some Crane methods HOT 3
- C and epsilon mixed up in return from differential_pressure_meter_C_epsilon HOT 3
- Mistake in doc. formula fluids.fittings.contraction_conical HOT 1
- vertical torispherical tank, volume at h_max HOT 4
- Bubbling flow in reactors HOT 2
- docs, unit of measurement for f and k in fluids.geometry.TANK HOT 1
- How to get the accurate average compression factor in the empirical formula HOT 3
- lots of KeyError:NUMBER while running pytest HOT 2
- Initiation error in Stream HOT 3
- model name bug in <two_phase.py> HOT 1
- Feature Request: Use np.sqrt() instead of math.sqrt() HOT 4
- Flow_meter.py Division by Zero Error HOT 1
- Feature request: Fittings Database addition HOT 7
- Particle Size Distribution Tutorial HOT 1
- Long radius nozzle error when C_specified = None HOT 1
- LGTM reports keyword argument `ags` is not a supported parameter name of function `quad_adaptive` HOT 1
- Error in contextual help: Reynolds function HOT 1
- Plate Heat Exchanger HOT 6
- IPython dependency with Numba HOT 3
- Error in the Hooper method for conical diffuser/expansion HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fluids.