Groovy coding a pile of dirt

Today I was wondering about the weight of a pile of builders raw material, like sand or cement. Specifically what allows us to calculate weight from a mound sat in front of us!

I’ve been using dynamic scripting for a long time, like Python, Perl, PHP and TCL. But I thought I’d do this little experiment with Groovy. I’ve written FORTRAN in the past for scientific and statistical studies, but the language itself is a dog. The value, the formulas look like the math when embedded in ones code.

Will Groovy look good? Here’s my code and I think the Math looks just fine. Quick, clear and no fuss, just what a useful experimental language should be. Here’s the code

// Compute the weight of a pile of build material
//
// NB pile is conical with a natural degree of repose (specific to material)
//
// The formula I use
//     W = pi h³D / 3 tan²(theta)

// or via Mathematica
//     W = 1/3 pi (h^3 D) tan^2(theta)

enum Material { CEMENT, DRYSAND, WETSAND, CLEANGRAVEL, SANDYGRAVEL }

class ConicalPile {
  def h         // height
  def D         // density of Material (pounds per cubic foot)
  def theta     // angle of repose in degrees - this is a known constant(range...)
  def float W   // calculated weight

  ConicalPile(material, height) {

    switch (material) {
      case material.CEMENT:
        D = 131 // equiv to 14.1 kg/m**3
        theta = 45
        break
     case material.DRYSAND: // dry or moist
        D = 90
        theta = 35
        break
     case material.WETSAND:
        D = 118
        theta = 25
        break
     case material.CLEANGRAVEL:
        D = 118
        theta = 37.5 // 35 to 40
        break
     case material.SANDYGRAVEL:
        D = 118
        theta = 27.5 // 25 to 30°
        break
     default:
        println "UNKNOWN material "
        break
    }

    h = height
    W = weight()
  }

  private float weight() {
     // ERMMM the formula says tan, but I use cos
    def radians=theta*(Math.PI/180)
    def cT = Math.cos(radians)
    // println "Math.cos("+theta+") == " + Math.cos(radians) // 45deg correct at .707

    // the recipe
    W = (Math.PI * h**3 * D) / (3 * (cT**2))
  }

  float getWeightPounds () {
    return W
  }

  float getWeightTons () {
    return W / 2000
  }

  void display() {
    println "Weight = " + String.format("%.0f", getWeightPounds()) + " pounds"
    println "Weight = " + String.format("%.3f", getWeightTons()) + " tons"
    println ""
  }

}

try {

  def ht = 10; // 10 ft high pile of builders material

  new ConicalPile(Material.CEMENT, ht).display();
  new ConicalPile(Material.DRYSAND, ht).display();

  return 0

} catch (Exception ex) {
  print "ouch"
}

Well a 10 ft high pile of cement is

Weight = 274366 pounds
Weight = 137.183 tons

and if dry sand

Weight = 140457 pounds
Weight = 70.228 tons

or thereabouts ;-)

[Ed: see the follow up for a better Groovier version]

One Response to Groovy coding a pile of dirt

  1. Pingback: Groovy Piles went mobile « LEXECORP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s