Monday, October 10, 2011

Bending materials

Have you ever wondered how to calculate the precise amount of weight some materials can handle? Let's suppose you want to build a old fashioned furniture for that audio system of yours using some nice wood you have in home and need to know if the wood will hold the audio weight. This can be achieved using formulas regarding strength of materials, more precisely the bending property.

1. Bending tensile strength (i.e. Yield strength or rupture tensile): The first thing we need in order to continue with our calculations is to discover what is the bending tensile strength of the material we are using. This value represent the edge where the material will break if that amount of pressure is applied to it. These values can be obtained from the suppliers (commercial materials) or a search in the net also can help. These values are not always expressed in the same unity, being the most common units: psi (pound per square inch), Map (mega pascal), N/mm2 (Newton per square millimeter), kgf/cm2 (kilogram-force per square centimeter), etc. None of them are better than other, they are just a way of see the pressure the material can hold. We are going to work with kgf/cm2 as this is one of the most used. If you find the bending tensile strength in some other unity you can use this site to convert it to kgf/cm2.

2. Choosing the material:  The material we are going to use for the tests is the very well known Plexiglas (PMMA), some more info about this material can be obtained here. The Plexiglas tensile strength is around 11000 psi, thus this is about 773.4 kgf/cm2.

3. Defining the shape: Having the first property we can now discover how to calculate the weight some material can handle for a given shape. There are formulas for most of the shapes we can find over the market, for the sake of simplicity we are going to do calculations using the rectangular shape (the most common one). The properties used by the formulas depend on the way the shape is being hold, more precisely which side are supported by a base (foundation pillars). Using the example from the picture below we can clarify this.

From the picture above we have the following variables:

b is the side of the rectangle hold by the foundation pillars.
h is the height of the rectangle.
L is the side of the rectangle from pillar to pillar
P is the force applied over the shape.

As we are working with kgf/cm2 all measurement must be made using centimeters, and P must be calculated using kilogram-force (kgf). Kgf is just the weight of the material in kilograms.

4. Formulas: There are a couple of formulas that we are going to use to calculate the material. Below I present some of the formulas related to the rectangular shape. Notice the shape is formed by the b and h sides, also known as section.

Tr is the bending tensile strength in kgf/cm2.
F is a security factor (see section below).
Tf is the acceptable bending tensile strength calculated based on the security factor.
Mf is the maximum flexure (bending) moment in kgf
P is the force applied to the rectangular shape in kgf.
L1 is the distance from pillar 1 to the point where P is applied (in centimeters).
L2 is the distance from pillar 2 to the point where P is applied (in centimeters).
L is the distance from pillar to pillar.
I is the inertia momentum (no external forces being applied to the material).
y is a imaginary line that divides the height of the shape in 2.
W is the resistance module of the material (in cube centimeters)
b is the side of the shape hold by the pillars (in centimeters).
h is the height of the shape (in centimeters).

5 - Security Factor: This is a number made by certain considerations about the elasticity and discharge of the material and also based on the way (effort) the weight is going to be applied over the shape. The effort for the security factor is divided into 4 groups:

  • Static - The weight is applied over the material statically —this is kept in the same place all the time.
  • Intermittent - The weight is applied and released from the material periodically.
  • Alternate - The weight is applied periodically in both directions (up and down).
  • Abrupt - The weight is released over the material abruptly and periodically.
For each group there is a security factor to be used for certain types of materials. If you don't know which type to use try to find the material that has the closest Tr of the material you are using.

Cast iron6101520
Soft Iron56812
Hard Iron46812
Security Factor

6 - Plugging the numbers: Now, suppose we have a piece of Plexiglas with these dimensions: b is 10 cm, L is 20 cm and h is 2 cm. What is the maximum weight in kilos this shape can hold? Remember we already know that the bending tensile strength (Tr) of the Plexiglas is about 773.4 kgf/cm2. Let's discover what is the appropriate flexure tensile (Tf) using the security factor. I'm going to use 8 for the security factor as the bending tensile strength from Plexiglas is very similar to wood and our audio system will sit statically over the shape.

P is going to be applied in the center of the shape (equilibrium point), in this way the weight is going to be distributed among the whole area. Now we are going to need a formula that relates the force applied with the shape we have. In order to do that we are going to need to calculate the resistance module of the material first.

Now we can relate Tf, Mf and W using the following formula:

So we discovered that our piece of Plexiglas can handle 129.204 kgf, or just simplifying, 129 kilograms. Now, it's just a matter of rearrange the formulas in order to solve for any variable you want.

7 - Other Shapes: Below I present some other shapes (section) formulas —if some of the variable is missing this means that the formula is the same as the rectangle shape. if you have trouble finding the one you want, just drop me a message and I can try to find some more information regarding it.

Solid circumference, where d is the diameter.

Tubular circumference, where d is the internal diameter and D is the external diameter.

Solid square, where a is the side.

Tubular square, where a is the external side and b is the internal side.

Have fun!

Friday, October 7, 2011

Calling functions dynamically in C

This is not news that someone can loads a function dynamically from a library and use it regularly. But how
about dynamically choosing the function to call and send the number of arguments based on this decision? One can say that this can be achieved using a BIG switch, but, using a switch or ifs we'll always have to change the code when new functions are included, besides that, it will not be fun! :)

This is simpler than it looks, all we have to do is to input some assembler into the code and et-voilà! We just need to pay attention at the calling convention being used. In the _cdecl calling convention the caller must cleanup the stack and as opposite of that, the _stdcall calling convention, the callee (the target function) will do the job automatically. For more details on calling conventions see this article in Wikipedia.

See below the example for the x86 processor in Windows 32 bits assembler. Notice 64 bits numbers are sent in 2 registers, the most significant byte are pushed first on the stack.

#include <stdio.h>

 * This function will be called dynamically (This will return my IQ coefficient! :-)
__int64 _cdecl test_stack(int first,int second,char *third,__int64 last)

* example on how to execute a function with dynamic arguments passing
* (example valid for x86 in Windows 32 bits for _stdcall and _cdecl calling conventions)
* see for more detais
int main(int argc,char *argv[])
#define MSB_LL(ll) ((long) (ll >> 32))
#define LSB_LL(ll) ((long) ll)
    int            result,first=4,sec=5;char *third="6";
    void          *f=test_stack;
    __int64    ll=123;
    long          hi=MSB_LL(ll);
    long          lo=LSB_LL(ll);

    /* caller must know the type (and order) of the arguments before calling the function */
    _asm {
        push hi        /* last argument is __int64 - push most significant part first */
        push lo        /* now send the least significant part of int64 */
        push third    /* third argument in the function prototype */
        push sec       /* second argument */
        push first     /* first argument */
        call f             /* call function */
        /* on _stdcall callee must cleanup the stack */
        /* on _cdecl caller must cleanup the stack */
        add esp, 20    /* Stack cleaning  --> add esp, sizeof(long) * arguments */
        mov hi,edx     /* get result of hi order (only used for 64 bits returns) */
        mov lo,eax     /* get result of low order or integer response */
    ll=((__int64) hi << 32 | lo);

Now, suppose that you receive the function pointer as a parameter and you also receive the arguments to be sent to the function in a va_list fashion? Powerful, don't you think?

Have fun!