#!/usr/bin/env perl
#!/usr/bin/perl

# Ted Rose's Two Headed Fly Swatter for demonstrating superelements
# and component mode synthesis.  This script creates a gmsh geometry 
# file describing the model then sends it to gmsh for 2D meshing.
# 
# Albert Danial Sept 15 2004

use warnings;
use strict;
my $Zcoord    = 0.0;
my $mesh_size = 0.2;
my $W = 5;       my $w = $W/2;
my $H = 5;       my $h = $H/2;
my $T = 0.75;    my $t = $T/2;
my @A = (   10, 17);
my @B = (   18, 17);
my @C = ($A[0], 12);
my @D = ($B[0], 12);
my @E = (($C[0] + $D[0])/2, 12);
my @F = ($E[0], 0);

my @p = (
          [ $A[0] - $w,      $A[1] + $h ],      # left head
          [ $A[0] + $w,      $A[1] + $h ],
          [ $A[0] - $w,      $A[1] - $h ],
          [ $A[0] - $t,      $A[1] - $h ],
          [ $A[0] + $t,      $A[1] - $h ],
          [ $A[0] + $w,      $A[1] - $h ],

          [ $B[0] - $w,      $B[1] + $h ],      # right head
          [ $B[0] + $w,      $B[1] + $h ],
          [ $B[0] - $w,      $B[1] - $h ],
          [ $B[0] - $t,      $B[1] - $h ],
          [ $B[0] + $t,      $B[1] - $h ],
          [ $B[0] + $w,      $B[1] - $h ],

          [ $C[0] - $t,      $C[1] - $t ],      # left stem
          [ $C[0] + $t,      $C[1] + $t ],

          [ $D[0] - $t,      $D[1] + $t ],      # right stem
          [ $D[0] + $t,      $D[1] - $t ],

          [ $E[0] - $t,      $E[1] - $t ],      # center
          [ $E[0] + $t,      $E[1] - $t ],
          [ $F[0] - $t,      $F[1]      ],
          [ $F[0] + $t,      $F[1]      ],
        );

my $base = "thfs";
my $file = "$base.geo";
open  GEO, ">$file" or die "Cannot write to $file:  $!\n";
my $nP = 0;
foreach (@p) {
    ++$nP;
    printf GEO "Point(%2d) = {% 12.4f, % 12.4f, % 12.4f, % 12.4f};\n",
                         $nP, $_->[0], $_->[1], $Zcoord, $mesh_size;
}

my @endpoints = ( 1, 2, 6, 5, 14, 15, 10, 9, 7, 8, 12,
                  11, 16, 18, 20, 19, 17, 13, 4, 3, 1
                );

for (my $i = 0; $i < scalar(@endpoints) - 1; $i++) {
    printf GEO "Line(%3d) = {%d,%d};\n", 
           $i+100, $endpoints[$i], $endpoints[$i+1];
}

printf GEO "Line Loop(1000) = {" . join(",", 100..119) . "};\n";
printf GEO "Plane Surface (2000) = {1000};\n";

close GEO;
printf "Wrote geometry file '$base.geo'\n";

system "gmsh -v 0 -2 $file";   # mesh 2D elements; disable verbose info
if (-r "$base.msh") {
    printf "Wrote mesh file     '$base.msh'\n";
}
