#!/usr/bin/env perl

# Make a simple airplane FE model.  This script contains the geometry
# description then sends it to gmsh for meshing.
# 
# Albert Danial May 9 2005

use warnings;
use strict;

my @lines = <DATA>;

my $base = "plane";
my $file = "$base.geo";
open  GEO, ">$file" or die "Cannot write to $file:  $!\n";
print GEO @lines;
close GEO;

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

__END__
cockpit_radius  = 5;
wing_len        = 35;
body_len        = 60;
Hstab_len       = 14;
wing_root_LE    = 17;
wing_root_TE    = wing_root_LE + 15;
wing_tip_LE     = 25;
wing_tip_TE     = wing_tip_LE  +  8;
emp_len         = 10;
emp_Y_low       = 2*cockpit_radius - 5;
emp_Y_hi        = 2*cockpit_radius;
tail_Xoff_LE    =  5;
tail_Xoff_TE    = tail_Xoff_LE + 8;
tail_height     = 15;
Hstab_X_off     = 4;
CcL             = 1.0; // cockpit characteristic length
BcL             = 2.0; // body characteristic length
                                                                                
// cockpit
Point(1) = { 0, 0, 0, CcL};
Point(2) = { 0, cockpit_radius, 0, CcL};
Extrude Point { 1, { 0,0,1}, {0,cockpit_radius,0} , -Pi/2 };  // becomes Curve 1
Extrude Point { 3, { 0,0,1}, {0,cockpit_radius,0} , -Pi/2 };  // becomes Curve 2
Line(10) = { 1, 4 };
Line Loop(11) = {1,2,-10};        // cockpit boundary
Plane Surface(12) = {11};         // cockpit surface
                                                                                
// body
Point(101) = {body_len,             0, 0, BcL};
Point(103) = {body_len, 2*cockpit_radius, 0, BcL};
// wing segment
Point(121) = {wing_root_LE,  0, 0, BcL};
Point(131) = {wing_root_TE,  0, 0, BcL};
Line(1001) = {1, 121};
Line(1002) = {121, 131};   // wing root segment
Line(1004) = {131, 101};
Line(1005) = {101, 103};
Line(1006) = {103,   4};
Line Loop(1007) = {1001,1002,1004,1005,1006,-10};  // body boundary
Plane Surface(1008) = {1007};                      // body surface

// left wing
Point(201) = {wing_tip_LE, 0, -wing_len, BcL};  // outboard leading  edge tip
Point(202) = {wing_tip_TE, 0, -wing_len, BcL};  // outboard trailing edge tip
Line(2001) = {121, 201};                        // leading  edge
Line(2002) = {201, 202};                        //
Line(2003) = {202, 131};                        // trailing edge
Line Loop(2007) = {2001, 2002, 2003, -1002};    // Lwing boundary
Plane Surface(2008) = {2007};                   // Lwing surface
                                                                                
// right wing
Point(301) = {wing_tip_LE, 0,  wing_len, BcL};  // outboard leading  edge tip
Point(302) = {wing_tip_TE, 0,  wing_len, BcL};  // outboard trailing edge tip
Line(3001) = {121, 301};                        // leading  edge
Line(3002) = {301, 302};                        //
Line(3003) = {302, 131};                        // trailing edge
Line Loop(3007) = {3001, 3002, 3003, -1002};    // Rwing boundary
Plane Surface(3008) = {3007};                   // Rwing surface
                                                                                
// empennage base
Point(401) = {body_len + emp_len, emp_Y_low, 0, BcL};
Point(402) = {body_len + emp_len, emp_Y_hi , 0, BcL};
Line(4001) = {101, 401};
Line(4002) = {401, 402};
Line(4003) = {402, 103};
Line Loop(4004) = {4001,4002,4003,-1005};
Plane Surface(4005) = {4004};
                                                                                
// vertical stabilizer
Point(501) = {body_len + tail_Xoff_LE, 2*cockpit_radius + tail_height, 0, BcL};
Point(502) = {body_len + tail_Xoff_TE, 2*cockpit_radius + tail_height, 0, BcL};
Line(5001) = {103, 501};
Line(5002) = {501, 502};
Line(5003) = {502, 402};
Line Loop(5004) = {5003,4003,5001,5002};
Plane Surface(5005) = {5004};
                                                                                
// left horizontal stabilizer
Point(601) ={body_len+tail_Xoff_LE+Hstab_X_off, 2*cockpit_radius+tail_height,  Hstab_len, BcL};
Point(602) ={body_len+tail_Xoff_TE+Hstab_X_off, 2*cockpit_radius+tail_height,  Hstab_len, BcL};
Line(6001) = {501, 601};
Line(6002) = {601, 602};
Line(6003) = {602, 502};
Line Loop(6004) = {6001,6002,6003,-5002};
Plane Surface(6005) = {6004};
                                                                                
// right horizontal stabilizer
Point(701) ={body_len+tail_Xoff_LE+Hstab_X_off, 2*cockpit_radius+tail_height, -Hstab_len, BcL};
Point(702) ={body_len+tail_Xoff_TE+Hstab_X_off, 2*cockpit_radius+tail_height, -Hstab_len, BcL};
Line(7001) = {501, 701};
Line(7002) = {701, 702};
Line(7003) = {702, 502};
Line Loop(7004) = {7003,-5002,7001,7002};
Plane Surface(7005) = {7004};
