Showing posts with label Physics. Show all posts
Showing posts with label Physics. Show all posts

Wednesday, April 6, 2011

Vectors, Projectiles, and More: Doing Physics in .net Part III

Having just started getting into C#, I can already say that it’s a pleasure to work with – especially when used with its tightly coupled IDE, Visual Studio. Of course, being a physics/astronomy/computer science geek, I got straight to work doing some projectile physics in C# to see how it turned out. To get myself started on this blog, I’m going to write a couple of posts about doing physics in .net:

In part 2 I started out an F# physics library containing the units we want to use, and some unit-safe math functions called Units.fs (note the edit added to part 2 regarding the sqrt function – you don't need to write one, F# has its own built-in, unit-safe sqrt). This will be used by the rest of the library and should remain the first source file in your project (in Visual Studio, the order the files appear in is the order they are compiled in. Now we want to start fleshing out our library and making it into something useful. I'll start by moving the whole projectile class into the F# library.

But wait. I'm pretty much just writing this whole thing over again in F#, right? The point of this is to interoperate between the two languages, after all. Really, this is the proper thing to do – we're going to write the projectile physics as a class in F#. The class can be extended by C# or other .net languages if you want to add qualitative properties to the projectile (such as, making a red ball that you want to send flying through the air – you just attach the projectile physics to your otherwise normal red ball object).

So here's our C# class, translated into F#:

//Projectiles.fs
#light

namespace FsPhysicsDemo

module Projectiles =
    [<Literal>]
    let g = -9.808<m/s^2>

open Projectiles
open UnitSafeFunctions

type Projectile =
    val mutable xi : float<m>
    val mutable yi : float<m>
    val mutable vxi : float<m/s>
    val mutable vyi : float<m/s>

    new() = {
        xi = 0.0<m>; yi = 0.0<m>;
        vxi = 0.0<m/s>; vyi = 0.0<m/s>;
    }
    
    member this.xf (t : float<s>) =
        this.xi + this.vxi * t
    member this.yf (t : float<s>) =
        this.yi + this.vyi * t + g * sqr t

Here we've moved our g constant out of the class, and into a module. As for the Projectile class, you see that we opened the Projectiles and UnitSafeFunctions modules just above them. This is because we use the g constant, and our unit-safe square function in the last member function in the class. We don't have to open anything in order to use the units however, since those weren't defined in a module, but rather just in the namespace (our Projectile class is also defined at namespace level – types can be in a namespace).

That's not bad, now we have our C# class, but a little more condensed and unit-aware. We're not really harnessing much power from functional programming right now though—but what more can we do? Well, those x and y values all belong together. In C# you'd probably use the Point class in System.Drawing, or make your own class. In a functional programming language (or one that supports them nicely, such as Python) you could use a Tuple:

Wednesday, February 23, 2011

Physics Library: Doing Physics in .net Part II

Having just started getting into C#, I can already say that it’s a pleasure to work with – especially when used with its tightly coupled IDE, Visual Studio. Of course, being a physics/astronomy/computer science geek, I got straight to work doing some projectile physics in C# to see how it turned out. To get myself started on this blog, I’m going to write a couple of posts about doing physics in .net:

In part 1 I went over the benefits of F#’s units of measure over doing dimensionless math with C# (and most other languages). But if your project isn’t already in F#, it’s a little hard to benefit from it. Luckily, it’s easy to work between languages in .net managed code. You can build the F# library using the standalone installer and Visual Studio 2010 Shell. This is the F# code I left off with:

#light

namespace FsPhysicsDemo

module Physics =
    [<Measure>] type m
    [<Measure>] type s

    let xf (xi : float<m>) (v : float<m/s>) (a : float<m/s^2>) (t : float<s>) =
        xi + v * t + 0.5 * a * t * t

    let v (a : float<m/s^2>) (t : float<s>) =
        a * t

    let deltaV (vi : float<m/s>) (vf : float<m/s>) =
        vf - vi

This works fine as a small library, but it’s a little annoying to work with. When the math was just done in C#, we had the benefit of it being object oriented—the parameters of the equation were all stored in the object—whereas with this code each parameter needs to be passed to a function. What makes it even worse is that we already have an object oriented projectile in C#, and then we go ahead and throw away the strengths of that setup:

Sunday, February 20, 2011

Units of Measure: Doing Physics in .net Part I

Having just started getting into C#, I can already say that it’s a pleasure to work with – especially when used with its tightly coupled IDE, Visual Studio. Of course, being a physics/astronomy/computer science geek, I got straight to work doing some projectile physics in C# to see how it turned out. To get myself started on this blog, I’m going to write a couple of posts about doing physics in .net.

For part one, I’ll do a brief intro into F#’s units of measure.

So, when I got started on this C# projectile stuff, it started looking a little like this:

namespace CsPhysicsDemo
{
    class Projectile
    {
        public const double g = -9.80;
        public double xi;
        public double yi;
        public double vxi;
        public double vyi;

        public double xf(double t)
        {
            return xi + vxi * t;
        }

        public double yf(double t)
        {
            return yi + vyi * t + 0.5 * Projectile.g * t;
        }
    }
}

Nice. The variables stand for “x initial,” “velocity x initial,” etc. This compiles and works fine – but guess what. There’s a mistake in there. Can you spot it? It’s not easy. the yf() function is supposed to return the vertical position of the projectile at a given time – instead it returns its velocity, plus its initial displacement. Which, of course, doesn’t make sense. The problem here isn’t just that we forgot to square t, but that – despite the strongly typed nature of C# – there’s no type checking for what kind of double we’ve created in this math operation.