By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

Python wrapper for C++ core

P: n/a
Hi All

Apologies in advance for the pretty basic question - but I can't seem
to find an answer anywhere else.

I am developing a fluid sim in C++ and have heard that many people use
Python in conjunction with C++ for this sort of thing (especially in
games dev).

What I can't understand why you would want to do this. Obviously the
core would be written in C++ (for my purposes anyway) so what parts
would be written in Python? What is the benefit, in this situation, of
using Python instead of C++?

thanks for your help

Holly

Oct 17 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
holly.woodl...@gmail.com wrote:
Hi All

Apologies in advance for the pretty basic question - but I can't seem
to find an answer anywhere else.

I am developing a fluid sim in C++ and have heard that many people use
Python in conjunction with C++ for this sort of thing (especially in
games dev).

What I can't understand why you would want to do this. Obviously the
core would be written in C++ (for my purposes anyway) so what parts
would be written in Python? What is the benefit, in this situation, of
using Python instead of C++?

thanks for your help

Holly
What would be the advantages? Let's see if I can knock it down for
you.

You didn't give any information about your fluid simulation other than
you are developing it in C++. Whether or not you'd want to expose your
fluid sim objects to Python depends on your purposes. If you code the
main fluid engine in C++ and expose it in Python, you might be able to:

1) Easily run your fluid simulation over a variety of parameters.
Vary the equation constants so you can see how the simulation works
with water, rubbing alcohol, air, and molasses.

2) Perform statistical analysis of your results. Use one of the
various statistical packages to examine the results. Use the CSV
module to save out an Excel-compatible set of results, or use the Win32
modules to start up Excel and populate a spreadsheet.

3) Use Python to set up the specific physical layout for the fluid
simulation. For a 2D fluid simulation, you might want to set up a
specific set of walls. You can do that automatically, or use your own
custom file format and let Python deal with parsing it.

4) Use Python to load models from different file formats into the
simulation. For 3D fluid simulations, you'd most likely use CAD
generated models. Create a basic C++/Python class for models that can
load triangles into the simulation. For each format that you support,
use or write a Python class, derived from the basic model class, that
can parse that format and put the triangles into the model using the
superclass. If the file format is text-based, Python's excellent
string manipulation can help out here.

5) Use Python to allow the user to "react" to certain conditions and
events during the simulation. Perhaps the model changes in one
simulation at a given time (as a valve opens or closes). Perhaps if
the model reaches a stable equilibrium, you'd like to end the
simulation so you don't need to know exactly when to end the
simulation.

6) Give your users the ability to perform any of the above.

What Python can give you is flexibility. Whether or not this is useful
for you depends entirely on the goals of your program, and the design
of your program. I'm sure there are other things Python can do for
you, but you'll need to figure them out (or post more information about
your program).

--Jason

Oct 17 '06 #2

P: n/a
At 17 Oct 2006 16:05:40 -0700 ho************@gmail.com wrote:
>
Hi All

Apologies in advance for the pretty basic question - but I can't seem
to find an answer anywhere else.

I am developing a fluid sim in C++ and have heard that many people use
Python in conjunction with C++ for this sort of thing (especially in
games dev).

What I can't understand why you would want to do this. Obviously the
core would be written in C++ (for my purposes anyway) so what parts
would be written in Python? What is the benefit, in this situation, of
using Python instead of C++?

thanks for your help
If they were using Python/Tk to develop a GUI, it makes sense (although
I would use Tcl/Tk, being a Tcl fan). In either case, SWIG can generate
the interface between C/C++ and Python/Tcl.
>
Holly

--
Robert Heller -- 978-544-6933
Deepwoods Software -- Linux Installation and Administration
http://www.deepsoft.com/ -- Web Hosting, with CGI and Database
he****@deepsoft.com -- Contract Programming: C/C++, Tcl/Tk

Oct 17 '06 #3

P: n/a
IMHO,

What I learned with the time and of course with books =) :

Python gives you rapid turnaround. You can try different approaches and
archictetures to solve the problem in much faster way than you would
with C++.

When you really need to crunch numbers, and numpy/othermodule is not
enough for you, then you would need to go down and optimize it in c++.

So, the ability to change, re-create and try another strategy would be
a killer reason for me to use python (like I have in the past) for
games or sim apps.

[]s
Robert Heller wrote:
At 17 Oct 2006 16:05:40 -0700 ho************@gmail.com wrote:

Hi All

Apologies in advance for the pretty basic question - but I can't seem
to find an answer anywhere else.

I am developing a fluid sim in C++ and have heard that many people use
Python in conjunction with C++ for this sort of thing (especially in
games dev).

What I can't understand why you would want to do this. Obviously the
core would be written in C++ (for my purposes anyway) so what parts
would be written in Python? What is the benefit, in this situation, of
using Python instead of C++?

thanks for your help

If they were using Python/Tk to develop a GUI, it makes sense (although
I would use Tcl/Tk, being a Tcl fan). In either case, SWIG can generate
the interface between C/C++ and Python/Tcl.

Holly

--
Robert Heller -- 978-544-6933
Deepwoods Software -- Linux Installation and Administration
http://www.deepsoft.com/ -- Web Hosting, with CGI and Database
he****@deepsoft.com -- Contract Programming: C/C++, Tcl/Tk
Oct 18 '06 #4

P: n/a
At 17 Oct 2006 17:19:03 -0700 "handful of nothing" <ha*****@gmail.comwrote:
>
IMHO,

What I learned with the time and of course with books =) :

Python gives you rapid turnaround. You can try different approaches and
archictetures to solve the problem in much faster way than you would
with C++.
Yes, this is especially true of GUI programming. I can create a Tcl/Tk,
*cross platform* GUI front end very rapidly and that GUI can be easily
and quickly 'tweaked' as needed, again very rapidly. I guess a Python
program could build and tweak a Python/Tk GUI as quickly.
>
When you really need to crunch numbers, and numpy/othermodule is not
enough for you, then you would need to go down and optimize it in c++.

So, the ability to change, re-create and try another strategy would be
a killer reason for me to use python (like I have in the past) for
games or sim apps.

[]s
Robert Heller wrote:
At 17 Oct 2006 16:05:40 -0700 ho************@gmail.com wrote:
>
Hi All
>
Apologies in advance for the pretty basic question - but I can't seem
to find an answer anywhere else.
>
I am developing a fluid sim in C++ and have heard that many people use
Python in conjunction with C++ for this sort of thing (especially in
games dev).
>
What I can't understand why you would want to do this. Obviously the
core would be written in C++ (for my purposes anyway) so what parts
would be written in Python? What is the benefit, in this situation, of
using Python instead of C++?
>
thanks for your help
If they were using Python/Tk to develop a GUI, it makes sense (although
I would use Tcl/Tk, being a Tcl fan). In either case, SWIG can generate
the interface between C/C++ and Python/Tcl.
>
Holly
>
>
--
Robert Heller -- 978-544-6933
Deepwoods Software -- Linux Installation and Administration
http://www.deepsoft.com/ -- Web Hosting, with CGI and Database
he****@deepsoft.com -- Contract Programming: C/C++, Tcl/Tk

--
Robert Heller -- 978-544-6933
Deepwoods Software -- Linux Installation and Administration
http://www.deepsoft.com/ -- Web Hosting, with CGI and Database
he****@deepsoft.com -- Contract Programming: C/C++, Tcl/Tk

Oct 18 '06 #5

P: n/a
ho************@gmail.com wrote:
Hi All

Apologies in advance for the pretty basic question - but I can't seem
to find an answer anywhere else.

I am developing a fluid sim in C++ and have heard that many people use
Python in conjunction with C++ for this sort of thing (especially in
games dev).

What I can't understand why you would want to do this. Obviously the
core would be written in C++ (for my purposes anyway) so what parts
would be written in Python? What is the benefit, in this situation, of
using Python instead of C++?
I see "fluid sim" and "games dev" in the same post, which kind of makes
me scratch my head. What would you need fluid sim for in a game?
Maybe it's a flight sim; I suppose now computers are fast enough to
attempt a low-fidelity CFD (computational fluid dynamics) in real time.
(Or maybe you mean something different by fluid sim, like a puzzle
game involving valves or something.)

Anyways, combining Python with C is useful for all kinds of
applications, not just games and simulations, because Python has
different strengths than C. C is faster and can inteface easily with
hardware and the OS; Python is better at pretty much everything else.
It takes care of a lot of stuff you'd have to do yourself in C.

Speaking as someone who studied a lot of CFD in college, I think Python
in particularly is very useful for extreme numerical work. (If that's
not what you're talking about, never mind the rest of the post.)

The people who do fluid simulations are engineers (classical sense),
not programmers. These people do NOT like to program. They like to
fiddle with arrays and do arithmetic; stuff like memory management,
input/output, and other computer-science-ish tasks are a pain in the
neck. Time spent doing that kind of stuff is time not spent doing
useful things like hand-optimizing the hell out of the numerical code.

Python (even with Numeric) is simply too slow for high-end CFD
applications, so the numerical part is almost always written in C or
Fortran. But C and Fortran are terrible languages to do things like
memory management and I/O in. Python, OTOH, is terrific for these
things. It's a great language for people who don't like to program.
Comparable languages such as Ruby are also good for I/O and stuff, but
I think Python's the best of all for numerical work:

1. Large support community
2. Large standard library and lots of third-party packages out there,
addresses all kinds of needs
3. A very spiffy package, pyfort, to interface with Fortran, which can
also works for C functions that act like Fortran (which is often the
case in a numerical application). Relatively straightforward general
interface to C if pyfort won't do.
4. numpy, a very good, well-maintained package which allows Python
programs to operate on arrays directly (useful for tasks that don't
need to be hyper-optimized).
5. Superficially similar to C and Fortran, which is no small thing for
engineers
Carl

Oct 18 '06 #6

P: n/a

Thanks everyone for the responses - I think I have a better
understanding now. I can handle all the messy I/O in Python and use C
for the number crunching. I especially like the idea of using python to
output the results to excel.

OK time to learn Python i think - any tips on best place to start
(tutorials/documentation etc). I have a pretty comprehensive background
in C/C++/Java and a bit of experience some other scripting languages so
its really a matter of syntax/libraries rather than programming
theory/constructs
>see "fluid sim" and "games dev" in the same post, which kind of makes
me scratch my head. What would you need fluid sim for in a game?
sorry i was just listing places I had seem people waxing lyrical on the
benefits of python. I am not doing games development - I have just
heard lots of games developers saying they use python

Thanks again for all the help!

Holly

Oct 19 '06 #7

P: n/a
On 19 Oct 2006 03:10:55 -0700, Holly <ho************@gmail.comwrote:
>
Thanks everyone for the responses - I think I have a better
understanding now. I can handle all the messy I/O in Python and use C
for the number crunching. I especially like the idea of using python to
output the results to excel.

OK time to learn Python i think - any tips on best place to start
(tutorials/documentation etc). I have a pretty comprehensive background
in C/C++/Java and a bit of experience some other scripting languages so
its really a matter of syntax/libraries rather than programming
theory/constructs
Be sure to check Boost.Python to create Python bindings for your project(s):
http://boost.org/libs/python/doc/tut...tml/index.html

--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
Oct 19 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.