Spring Mass

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. We receive a few cents from each purchase.   Thanks.


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

 

Problem Description

This is another in the "Physical World" (or maybe "Physics World"?) series of programs.  The Bouncing Ball and Cannon programs started it.  This one begins  an exploration of Spring-Mass systems by modeling the actions of a coiled spring with a weight hanging from it when we give it a push or stretch and release it.  The motion of the "system" is animated as the spring bounces.   

Background & Techniques

I'm interested in programs that  model the real world.  One goal is to develop a model Physics World where we can assemble machines and watch them run.   Remember "The Incredible Machine"  from DOS days?  

That's  still a long way off, but we'll definitely need spring-mass systems as a component of that world.  The current version, a single spring with a dangling weight, is about 300 lines long.  But the heart of the program, the Animate routine, has only 30 or so lines of Delphi code, so why not start now.  

First the physics:  Thanks to Mr. Newton, we know that if we suspend a  weight from a spring, it will just hang there (1st law).  We'll also be needing his 2nd law (acceleration is proportional to the force applied and inversely proportional to its mass, A=F/M).  

To get our system moving, we'll consider two initial conditions: The initial stretch, X0,  applied to the spring and/or the initial push or force, F0,  applied to the spring.   

Once we get our spring-mass in motion,  we need to calculate the  displacement as function of time.  X=f(t).  To do this we'll need to worry about how fast the mass is moving, velocity, which in turn depends on how fast the velocity is changing over time,  acceleration.    In math terms, this involves a differential equation.  For our purposes, we won't need to worry about derivatives or integration (even though we will be implicitly using both). The up and down motion of our weight is controlled by four primary factors:  Gravity, Mass, Spring Constant and Damping.  

bulletGravity pulls downward with a force proportional to the Mass.  As an equation F= MG.
bulletThe spring itself applies a force proportional how far it is stretched - stretch it twice as far, and it pulls twice as hard trying to get back to equilibrium.  Notice that this force always opposes the displacement.   In other words -F/X=K or F=-KXK is called the spring constant since it is a property of the particular spring used. 
bulletThere are other forces trying to stop the spring as it moves.  The friction of air resists the movement, there is internal friction between the molecules of the spring and there may be an external damper resisting the movement.  All of these can be combined into a constant called the damping factor, C.  The force also opposes the direction of movement  and is proportional to the velocity of the moving mass.   So -F/V=C or F=-CV.   Notice that when the system is at rest (V=0), there is no damping force.  

These six parameters give us enough information to set our mass in motion and predict how it will move.  In practice, the animation loop assumes one time unit increment each time through.  With some simplifying assumptions,  the new distance X increases by the velocity, V (X=X+V).  The velocity changes by the acceleration value A (V=V+A).  And finally the acceleration depends on the forces acting on the mass as described above.  Here's where we need  Newton's 2nd law,   A=F/M.   All of our forces are acting in the same direction (vertical), so we can just add them up:  A= (MG-KX-CV)/M  or A=G-(KX+CV)/M.  When you examine the code, you'll see these three equations for X, V, and A as the heart of the loop.  Everything else is detail.

There's another aspect of the spring constant that we have not discussed.  Some springs, for example rubber bands or screen door springs, that can only apply force by pulling.  The above equations would hold for an automobile spring, but not for one of these floppy springs.  We'll leave the addition of this kind of spring for the next version.

The code defines a TSpring object as a descendent of TPanel.  A Timage contained in TSpring supplies the drawing canvas.  Methods are provided to draw the spring & mass image of a given length and to erase it.  The Animate method contains the loop discussed above to calculates a new length and erase the old and draws the new image at each step.   Most of the remainder of the code gets parameters from edit fields and ensures that they are valid numbers. 

Addendum 6/27/2003:   Viewer Don Rowlett reported that the spring animation loop does not always end and suggested some additional error checking of the input parameters.  That gave me the excuse to revisit the program with the result posted today.   The loop resulted because I was trying to stop when velocity and acceleration were both small and the weight was at displacement 0 (un-stretched spring).  Of course, with a weight attached, the spring does not come to rest at 0 but  is stretched by G*M/K.  Defining a new property, XEnd, to reflect the resting position and testing against that value corrected the problem.   While at it, several other changes were made including correcting the damping problem  (Damping is now entered and a value between 0 and 1 and adjusted internally to range from no damping to fully damped).  And the spring type may be defined as a compression type, acting in both directions, or "rubber band", acting to pull on the weight only.  Also the weight end of the spring may be defined as initially constrained or unconstrained.   

Addendum February 12, 2007:      It has been several years since I have looked at the Spring mass program, but received an email recently from a college instructor who would like to use it in a Differential Equations class to provide visualizations.  I made a few enhancements and fixed a few bugs based on his suggestions. 

bulletAnimated spring display is now automatically scaled.
bullet"Initial Force" parameter has been replaced by "Initial Velocity".
bulletResolution and display speed can now be controlled by the user.
bulletDetailed Time, Acceleration, Velocity and Position data can be displayed. 

SpringMass2 was posted today for his review.    (So Springmass2.1 may not be far behind :>) 

Running/Exploring the Program

bulletDownload source
bulletDownload executable

Suggestions for Further Explorations

Got too much spare time? These should solve your problem. 

Fixed 6/27/03: There is a bug revealed if you start with the default initial conditions but increase the damping to around 20.  I've figured out what is happening, but haven't fixed it yet.  Can you?
Added 6/27/03: Handle "floppy" springs that only apply a restoring force while in tension.  Probably add a new "Springtype" parameter, and maybe change the spring image to a straight line to represent a rubber band for the new type.
Add units scaling so that inputs and outputs can be specified in metric or English form.
Add some sort of charting capability to display any of the 4 variables (displacement, velocity, acceleration,  time) against another. 
Make spring images resizable.
Handle multiple springs, with  serial or parallel connections  between them.  
And the ultimate goal: Springs, Masses, and Dampers as separate object types, arbitrarily connected into a single system.  A Charts object type attachable to any point in the system to display outputs.
  
Created: December 12, 2000

Modified: February 18, 2016

 
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2017, Gary Darby    All rights reserved.