KineticsKit - A Physics-Based General 3D Mass-Spring System
Hi! I'd like to present you KineticsKit - A Physics-Based General 3D Mass-Spring System. It's purely written in Python and has several example programs included. Just give it a try - you will like it. Requirements: ------------- Python 2.0 or higher VPython available from http://virtualphoton.pc.cc.cmu.edu/projects/visual/ Objective: ---------- KineticsKit provides some classes to simulate the behaviour of a 3D mass-spring-system. It can be used to investigate some basic kinetics, but I wrote it mostly for the fun of programming it and playing with it. Usage: ------ First a System has to be generated. The System has several attributes like gravity, viscosity and timestep which must be given in the constructor. Some of them are mandatory (timestep), the rest of them are optional. Next Masses are generated and inserted into the System. Masses also have lots of attributes like m, pos, if it should be fixed at its initial location, its initial velocity and the color. Most of them are implemented as keyword arguments and can thus be omitted if they are not used. After this, the Masses can be connected with Springs. There is no restriction in making the connections so one Mass can be connected with several others. A Spring needs to know which two Masses it should connect, and its spring-constant. The rest of the constructor arguments are again implemented as keyword arguments, but I recommend to take a look at them. They are useful to tense the Spring, specify a damping value or give it a nicer color. Finally the System has to be started and kept running with an infinite loop. Interaction with the system is possible with the mouse. A Mass is picked by clicking at it. It can be moved around and the rest of the system responds to the movement. To release the Mass the mouse must be clicked again. """A Physics-Based General 3D Mass-Spring System. by Markus Gritsch (gritsch@iue.tuwien.ac.at) Objective: ---------- KineticsKit provides some classes to simulate the behaviour of a 3D mass-spring-system. It can be used to investigate some basic kinetics, but I wrote it mostly for the fun of programming it and playing with it. Usage: ------ First a System has to be generated. The System has several attributes like gravity, viscosity and timestep which must be given in the constructor. Some of them are mandatory (timestep), the rest of them are optional. Next Masses are generated and inserted into the System. Masses also have lots of attributes like m, pos, if it should be fixed at its initial location, its initial velocity and the color. Most of them are implemented as keyword arguments and can thus be omitted if they are not used. After this, the Masses can be connected with Springs. There is no restriction in making the connections so one Mass can be connected with several others. A Spring needs to know which two Masses it should connect, and its spring-constant. The rest of the constructor arguments are again implemented as keyword arguments, but I recommend to take a look at them. They are useful to tense the Spring, specify a damping value or give it a nicer color. Finally the System has to be started and kept running with an infinite loop. Interaction with the system is possible with the mouse. A Mass is picked by clicking at it. It can be moved around and the rest of the system responds to the movement. To release the Mass the mouse must be clicked again. Zooming / rotating of the scene is also done with the mouse by holding the left / right mouse button down. For further details please have a look at the example programs and the constructors of the classes System, Mass and Spring. Have fun! -- |\/\/\/| /------------------------------------------------------------------\ | | | Markus GRITSCH | phone: +43 / 1 / 58801-36015 | | | | Institute for Microelectronics | cellular: +43 / 676 / 4973431 | | (o)(o) | Technical University of Vienna | fax: +43 / 1 / 58801-36099 | C _) | Gusshausstrasse 27-29 / E360 | email: gritsch@iue.tuwien.ac.at | | ,___| | A-1040 Vienna / AUSTRIA | SMS: 436764973431@max.mail.at | | / \------------------------------------------------------------------/ /____\ / \ "Computers let you make more mistakes faster than any other invention in human history, with the possible exception of handguns and tequila." Mitch Radcliffe
I'd like to present you KineticsKit - A Physics-Based General 3D Mass-Spring System. It's purely written in Python and has several example programs included. Just give it a try - you will like it.
Markus, If you create a webpage for it, I'll link it into the edu-sig webpage under "Software". This offer applies to everyone who has educational software written in Python. I apologize for not having kept up-to-date with past announcements in this list; if you announced something previously that you think would be interesting for others, please send me an email with a URL and a one-sentence blurb similar to the ones that are already there in the "Software" section at the edu-sig webpage: http://www.python.org/sigs/edu-sig/ --Guido van Rossum (home page: http://www.python.org/~guido/)
Arthur -- I hope you'll send a sentence re PyGeo, as the link on the page mentioned is broken (not updated since you moved). Kirby
Python. I apologize for not having kept up-to-date with past announcements in this list; if you announced something previously that you think would be interesting for others, please send me an email with a URL and a one-sentence blurb similar to the ones that are already there in the "Software" section at the edu-sig webpage:
http://www.python.org/sigs/edu-sig/
--Guido van Rossum (home page: http://www.python.org/~guido/)
Here's something from a math teacher who hangs out on math-teach (where I've been cross-posting a few of my "math through programming" essays). He wonders if there's a way to get "long floats". I don't think there is, except maybe there's an add-on module I don't know about. Any clues? Kirby ============================================================== Date: Tue, 09 Jan 2001 20:49:15 -0800 From: ...@stanfordalumni.org> Subject: Re: Math through Programming (CP4E) To: urner@alumni.princeton.edu Thanks to your posts, I started playing with Python a bit. The first thing I wanted to explore was something a student pointed out: on their calculator, there are patterns around the last few digits of (1 / sqrt(5)) * ((1 + sqrt(5))/2)^n, and they wondered if it was just rounding error or something interesting going on or a coincidence or what. I figured getting some further decimal places would be easy in Python, but while long integers are there for the having, I couldn't find any way to get additional precision in my floats. I read the manual a bit and was still lost. It wasn't hard to put together the loop to do the nth powers, and it only took me a little while to realize that I needed to type math.sqrt instead of sqrt (or define my own function to be math.sqrt). I still forget to put the : at the end of the line when I need to, and haven't quite gotten used to the interpreter yet, so I end up retyping stuff more than I should. I'm running it on a mac; probably if I ran it under linux instead I'd be happier living in my emacs window. Anyway, if you know anything about python packages that do floating point with 100 digits of precision, that would be nifty. In any case, thanks for writing something on math-teach that isn't just still more arguing between GG and MPG and their ilk. Actual math content! Wow! --XXXXX
On Wed, 10 Jan 2001, Kirby Urner wrote:
Here's something from a math teacher who hangs out on math-teach (where I've been cross-posting a few of my "math through programming" essays).
He wonders if there's a way to get "long floats". I don't think there is, except maybe there's an add-on module I don't know about. Any clues?
According to the documentation at: http://python.org/doc/current/lib/module-mpz.html there should be a rational numbers module in one of the Demos directories (Demo/classes/Rat.py). Here's an interpreter session that plays a little bit with it. ###
from Rat import rat rat(5,3) Rat(5,3) str(rat(1,3)) '(1/3)' 1000-.0001 999.99990000000003 rat(1000) - rat(.0001) 999.99990000000003 # whoops! rat(1000) - rat(1, 1000) Rat(999999,1000) # ah, that's better. ###
Hope this helps!
[Kirby Urner]
Here's something from a math teacher who hangs out on math-teach (where I've been cross-posting a few of my "math through programming" essays).
He wonders if there's a way to get "long floats". I don't think there is, except maybe there's an add-on module I don't know about. Any clues?
The only language I know of with native support for adjustable (by the user) precision floats is REXX. There are BigFloat libraries available at least for Perl and Ruby, and IIRC also Java. Some specialized numeric environments (e.g., Macsyma) also support them. As far as I know, nobody ever bothered to write one for Python. Python has something far more strange and wonderful, though: ftp://ftp.python.org/pub/python/contrib-09-Dec-1999/ DataStructures/real-accurate.pyar (I broke that into two lines, else my mailer would have broken it at an uglier place; paste them together without the leading blanks on the second line) That's an unusual implementation of the constructive reals. Arbitrary-precision floats are buried in there, but hard to get at as such. I'll just quote the start of the readme file: Real.README - documentation for real.py. Goes with version 1.11. Written by Jurjen N.E. Bos (jurjen@q2c.nl) Introduction ============ real.py is a library that introduces a new class, called Real, of abitrarily precise numbers, allowing computations with "infinite" precision. Each number keeps an approximation of the accuracy of the number. Routines are available to compute numbers with more and more digits. real.py allows you to: - compute with very high precision, e.g. 1000 digits - compute with very large or small numbers, up to about 10**500000000 - compute without unexpected precision loss: all digits guaranteed - compute with infinite precision, printing digits on the fly - printing floating point numbers to their exact accuracy Just to whet your appetite:
from real import pa,exp,pi,sqrt pa('exp(pi()*sqrt(163))') 262537412640768743.99999999999925007259719818568887935385633733699086270 753741037821064791011860731295118134618606450419308388794975386404490572 871447719681485232243203911647829148864228272013117831706501045222687801 444841770346969463355707681723887681000923706539519386506362757657888558 223948114276912100830886651107284710623465811298183012459132836100064982 ...
and so on. it's-especially-fun-if-you-have-infinite-memory<wink>-ly y'rs - tim
At 04:18 AM 01/12/2001 -0500, Tim Peters wrote:
The only language I know of with native support for adjustable (by the user) precision floats is REXX. There are BigFloat libraries available at least for Perl and Ruby, and IIRC also Java. Some specialized numeric environments (e.g., Macsyma) also support them.
As far as I know, nobody ever bothered to write one for Python. Python has something far more strange and wonderful, though:
ftp://ftp.python.org/pub/python/contrib-09-Dec-1999/ DataStructures/real-accurate.pyar
Thank you Tim! Once again, your input is right on the money. I've been having a fun time computing things to the nth digit (plus/minus). Such a powerful little package! O frabjous day! Callooh! Callay!" He chortled in his joy. -- Lewis Carroll Kirby PS: appended, my letter to the math teacher, who is also turned on about Python at this point, probably chortling as well: On Fri, 12 Jan 2001 21:11:07 -0800, you wrote:
Here's some feedback re the "long float" question from Tim Peters. He's a Python god and, even better, consistently helpful and forthcoming with hard-to-find information re Python and programming in general.
Nifty! I'd seen big floats in C++ but not in any of the other languages. I'll give that library a whirl. Looks like it has plenty of decimal places for me!
Thanks, --[XXXXXXXX]
Yes, I've been playing with it too (see appended value of PHI = (1+sqrt(5))/2 I'm not familiar with the .pyar extension. I renamed it to .py after downloading, and then, since hyphenated file names are problematic, I had to use a weird syntax for import, shown below.
__import__("real-accurate") # after changing extension to .py real.README : written. real.py : written. <module 'real-accurate' from 'g:\python20\lib\real-accurate.py'> import real from real import * pi() 3.141592653589793238462643383279503+-2 sqrt(5) 2.23606797749978969640917366873127+-2 (1+sqrt(5))/2 1.61803398874989484820458683436564+-1 pow((1+sqrt(5))/2,2) 2.61803398874989484820458683436564+-5 for i in range(10): print pow( (1+sqrt(5))/2,i )
1 1.61803398874989484820458683436564+-1 2.61803398874989484820458683436564+-5 4.2360679774997896964091736687312+-4 6.854101966249684544613760503097+-2 11.09016994374947424102293417183+-2 17.94427190999915878563669467491+-3 29.03444185374863302665962884674+-3 46.97871376374779181229632352168+-3 76.0131556174964248389559523685+-3
for i in range(10): print pow( (sqrt(5)*(1+sqrt(5))/2),i )
1 3.6180339887498948482045868343656+-1 13.090169943749474241022934171827+-2 47.36067977499789696409173668730+-3 171.3525491562421136153440125775+-5 619.95934690622108325626137945+-1 2243.03398874989484820458683436+-3 8115.3732092183688247416272746+-1 29361.6961023423698826852022010+-2 106231.614465620005289717874632+-2 And here's PHI (only took a few seconds):
rep('(1+sqrt(5))/2')
<< SNIP -- iterations of lesser precision >> 1.61803398874989484820458683436563811772030917980576286213 5448622705260462818902449707207204189391137484754088075386 8917521266338622235369317931800607667263544333890865959395 8290563832266131992829026788067520876689250171169620703222 1043216269548626296313614438149758701220340805887954454749 2461856953648644492410443207713449470495658467885098743394 4221254487706647809158846074998871240076521705751797883416 6256249407589069704000281210427621771117778053153171410117 0466659914669798731761356006708748071013179523689427521948 4353056783002287856997829778347845878228911097625003026961 5617002504643382437764861028383126833037242926752631165339 2473167111211588186385133162038400522216579128667529465490 6811317159934323597349498509040947621322298101726107059611 6456299098162905552085247903524060201727997471753427775927 7862561943208275051312181562855122248093947123414517022373 5805772786160086883829523045926478780178899219902707769038 9532196819861514378031499741106926088674296226757560523172 7775203536139362107673893764556060605921658946675955190040 0555908950229530942312482355212212415444006470340565734797 6639723949499465845788730396230903750339938562102423690251 3868041457799569812244574717803417312645322041639723213404 4449487302315417676893752103068737880344170093954409627955 8986787232095124268935573097045095956844017555198819218020 6405290551893494759260073485228210108819464454422231889131 9294689622002301443770269923007803085261180754519288770502 1096842493627135925187607778846658361502389134933331223105 3392321362431926372891067050339928226526355620902979864247 2759772565508615487543574826471814145127000602389016207773 2244994353088999095016803281121943204819643876758633147985 7191139781539780747615077221175082694586393204565209896985 5567814106968372884058746103378105444390943683583581381131 1689938555769754841491445341509129540700501947754861630754 2264172939468036731980586183391832859913039607201445595044 9779212076124785645916160837059498786006970189409886400764 4361709334172709191433650137157660114803814306262380514321 1734815100559013456101180079050638142152709308588092875703 4505078081454588199063361298279814117453392731208092897279 2221329806429468782427487401745055406778757083237310975915 1177629784432847479081765180977872684161176325038612112914 3683437670235037111633072586988325871033632223810980901211 0198991768414917512331340152733843837234500934786049792945 9915822012581045982309255287212413704361491020547185549611 8087642657651106054588147560443178479858453973128630162544 8761148520217064404111660766950597757832570395110878230827 1064789390211156910392768384538633332156582965977310343603 2322545743637204124406408882673758433953679593123221343732 0995749889469956564736007295999839128810319742631251797141 4320123112795518947781726914158911779919564812558001845506 5632952859859100090862180297756378925999164994642819302229 3552346674759326951654214021091363018194722707890122087287 3617073486499981562554728113734798716569527489008144384053 2748378137824669174442296349147081570073525457070897726754 6934382261954686153312095335792380146092735102101191902183 6067509730895752895774681422954339438549315533963038072916 9175846101460995055064803679304147236572039860073550760902 3173125016132048435836481770484818109916024425232716721901 8933459637860878752870173935930301335901123710239171265904 7026349402830766876743638651327106280323174069317334482343 5645318505813531085497333507599667787124490583636754132890 8624063245639535721252426117027802865604323494283730172557 4405837278267996031739364013287627701243679831144643694767 0531272492410471670013824783128656506493434180390041017805 3395058772458665575522939158239708417729833728231152569260 9299594224000056062667867435792397245408481765197343626526 8944888552720274778747335983536727761407591712051326934483 7529916499809360246178442675727767900191919070380522046123 2482391326104327191684512306023627893545432461769975753689 0417636502547851382463146583363833760235778992672988632161 8583959036399818384582764491245980937043055559613797343261 3483049494968681089535696348281781288625364608420339465381 9441945714266682371839491832370908574850266568039897440662 1053603064002608171126659954199368731609457228881092077882 2772036366844815325617284117690979266665522384688311371852 9919216319052015686312228207155998764684235520592853717578 0765605036773130975191223973887224682580571597445740484298 7807352215984266766257807706201943040054255015831250301753 409411719101929890384+-1 Seems to be a powerful package. Fun, too (guess I must be a nerd, huh -- actually I prefer "geek" as an honorific). Kirby
Kirby, I'm delighted you're having fun with Jurjen's Real class! It is indeed a hoot.
... I'm not familiar with the .pyar extension. I renamed it to .py after downloading, and then, since hyphenated file names are problematic, I had to use a weird syntax for import, shown below.
If you read the top of the downloaded file, it says: #! /usr/local/bin/python # This is a Python archive; run through your Python to convert. # Supports bit error detection using MD5 (if available). That is, the downloaded file is a Python program that, when *executed*, *creates* the files real.README and real.py. So you should do something like python real-accurate.py from a command line to unpack the contents. As part of unpacking, it computes MD5 checksums to ensure that the download wasn't corrupted. Jurjen had a unique approach to things. pyar files never caught on.
__import__("real-accurate") # after changing extension to .py real.README : written. real.py : written. <module 'real-accurate' from 'g:\python20\lib\real-accurate.py'>
That's another odd way to execute the file. From now on you can *start* with the next line:
import real from real import * pi() 3.141592653589793238462643383279503+-2 sqrt(5) 2.23606797749978969640917366873127+-2 (1+sqrt(5))/2 1.61803398874989484820458683436564+-1 pow((1+sqrt(5))/2,2) 2.61803398874989484820458683436564+-5 ...
I'm not sure this is documented (it's been a while): the module attribute "default" controls how many digits are used by default. So, for example,
import real real.pi() 3.141592653589793238462643383279503+-2 real.default = 10 real.pi() 3.14159265358+-2 real.default = 50 real.pi() 3.141592653589793238462643383279502884197169399375105+-2
... Seems to be a powerful package.
Very, yes. It also has surprises; for example,
rep('pi() - pi()') .000000000000000000000000000000000+-4 .00000000000000000000000000000000000000000000+-3 0.+-4e-59 0.+-3e-79 0.+-4e-105 0.+-3e-140 0.+-3e-187 0.+-3e-249 0.+-4e-332 0.+-4e-443 0.+-3e-591 0.+-4e-788 0.+-3e-1051 0.+-3e-1401 0.+-3e-1868 ...
which isn't surprising, but
pa('pi() - pi()')
will never print anything. This will often happen when, for example, the exact result is an exact integer (like 0!): the error bounds keep getting tighter as more precision is used, but no finite amount of precision suffices to distinguish 4.00000000000000000000000000000000000000000000000 ... from 3.99999999999999999999999999999999999999999999999 ... So pa gets stuck, never sure what the first digit is. And it's a provable general result that *no* implementation of the constructive rules can always be sure of the first result digit. There are some very pretty results of that nature; as another example, comparison of the constructive reals is also uncomputable in general (for much the same reason: is 3.999999... < 4? it depends on whether that sequence of trailing 9s ever ends! if it does, we'll eventually discover that, but if it doesn't, we'll keep computing forever).
Fun, too (guess I must be a nerd, huh -- actually I prefer "geek" as an honorific).
Then I'm a nerd too -- well, I guess I was anyway <wink>.
participants (5)
-
Daniel Yoo
-
Guido van Rossum
-
Kirby Urner
-
Markus Gritsch
-
Tim Peters