/**
File: Demo.Arm2D.any

Copyright (c) 1999-2011, AnyBody Technology A/S

All rights reserved.



This is a demonstration of a simple arm model comprising 
two degrees of freedom and a selected number of muscles.
Features in this model are

- definition of muscles
- application of muscle recruitment analysis in 
  the AnyBodyStudy
- redundancy of the muscle configuration


The file has the following sections:
- The Arm Model
  - Reference frames
  - Segments
  - Joints
  - Drivers
  - Loads
  - Muscles
- "The body study"

Notice that all these section could be separate files
that are assemble in a single main file by #include 
statements. All include statements must be inside the 
"Main = {};" statement in this main file.

*/
Main = {

AnyFolder InputVariables = {
  AnyVar MassArm ??= DesignVar(0.4);  
};
  
 
// =======================================================
// The Arm Model
// =======================================================
AnyFolder ArmModel = {

  
  // -----------------------------------------------------
  // Basic Reference Frames
  // -----------------------------------------------------
  AnyFixedRefFrame GlobalRef = {
    //AnyDrawRefFrame DrwGlobalRef = {};

    AnyRefNode DeltodeusA = { sRel = {0.05,0,0}; };
    AnyRefNode DeltodeusB = { sRel = {-0.05,0,0}; };
    AnyRefNode BicepsLong = { sRel = {0.1,0,0}; };
    AnyRefNode TricepsLong = { sRel = {-0.1,0,0}; };
    
  };


  // -----------------------------------------------------
  // Segments
  // -----------------------------------------------------
  AnyFolder Segs = {

    //---------------------------------
    AnySeg UpperArm = {
      r0 = {0,0,-0.2};
      Axes0 = {{0,0,1},{0,1,0},{-1,0,0}};
      Mass = 2.0;
      Jii = {0.005,0.01,0.01};
      AnyRefNode ShoulderNode = {
        sRel = {-0.2,0,0};
      };
      AnyRefNode ElbowNode = {
        sRel = {0.2,0,0};
      };
      AnyRefNode DeltodeusA = { sRel = {-0.1,0,0}; };
      AnyRefNode DeltodeusB = { sRel = {-0.1,0,0}; };
      AnyRefNode Brachialis = { sRel = {0.1,0,0}; };
      AnyRefNode BicepsShort = { sRel = {-0.1,0,0}; };
      AnyRefNode Brachioradialis = { sRel = {0.05,0,0}; };
      AnyRefNode TricepsShort = { sRel = {-0.1,0,0}; };

      AnyDrawSeg DrwSeg= {};
    };

    //---------------------------------
    AnySeg LowerArm = {
      r0 = {0.2,0,-0.4};
      Mass = Main.InputVariables.MassArm;
      Jii = {0.005,0.01,0.01};
      AnyRefNode ElbowNode = {
        sRel = {-0.2,0,0};
      };
      AnyRefNode HandNode = {
        sRel = {0.2,0,0};
        //AnyDrawRefFrame DrwNode = {};
      };
      AnyRefNode Brachialis = { sRel = {-0.1,0,0}; };
      AnyRefNode Brachioradialis = { sRel = {0.0,0,0}; };
      AnyRefNode Biceps = { sRel = {-0.15,0,0}; };
      AnyRefNode Triceps = { sRel = {-0.25,0,-0.05}; };

      AnyDrawSeg DrwSeg= {};
    };

  };

  // -----------------------------------------------------
  // Joints
  // -----------------------------------------------------
  AnyFolder Jnts = {

    //---------------------------------
    AnyRevoluteJoint Shoulder = {
      Axis = y;
      AnyFixedRefFrame& Ground = Main.ArmModel.GlobalRef;
      AnyRefNode &UpperArmNode = ..Segs.UpperArm.ShoulderNode;
    };

    //---------------------------------
    AnyRevoluteJoint Elbow = {
      Axis = y;
      AnyRefNode &UpperArmNode = ..Segs.UpperArm.ElbowNode;
      AnyRefNode &LowerArmNode = ..Segs.LowerArm.ElbowNode;
    };

  };

  // -----------------------------------------------------
  // Motion Drivers
  //   Notice that the reaction force's type is set to zero 
  //   This indicates that the reactions is inactive, 
  //   i.e. it does not carry any load
  // -----------------------------------------------------
  AnyFolder Drivers = {

    //---------------------------------
    AnyKinEqSimpleDriver ShoulderMotion = {
      AnyJoint &Jnt = ..Jnts.Shoulder;
      DriverPos = {1.5};
      DriverVel = {-0.7};
      Reaction.Type = {Off};
    };
//    //---------------------------------
//    AnyKinEqSimpleDriver ShoulderMotion1 = {
//      AnyJoint &Jnt = ..Jnts.Shoulder;
//      DriverPos = {1.6};
//      DriverVel = {-0.7};
//      Reaction.Type = {Off};
//    };

    //---------------------------------
    AnyKinEqSimpleDriver ElbowMotion = {
      AnyJoint &Jnt = ..Jnts.Elbow;
      DriverPos = {-1.5};
      DriverVel = {-0.7};
      Reaction.Type = {Off};
    };

  };

  // -----------------------------------------------------
  // Applied Loads
  //   Notice that gravity is included automatically
  //   We apply two loads (they may be uncommented in the 
  //   file):
  //   - A force (point load, 3D vector) at the hand
  //   - A load to the elbow joint. Since the elbow 
  //     joint is revolute joint measuring angle, the load 
  //     applied is equivalent to a torque.
  // -----------------------------------------------------
  AnyFolder Loads = {

    //---------------------------------
    AnyForce3D HandLoad = {
      AnyRefNode &HandNode = ..Segs.LowerArm.HandNode;
      F = {0,0,-100};  // Global force in Newton
    };

    //---------------------------------
    AnyForce ElbowTorque = {
      AnyJoint& Jnt = ..Jnts.Elbow;
      F = {10};  // Force, equivalent to torque in Newton
    };


  };


  // -----------------------------------------------------
  // Muscles
  // -----------------------------------------------------
  AnyFolder Muscles = {

    //---------------------------------
    // We define one simple muscle model, which we will use
    // use for all muscles
    AnyMuscleModel MusMdl = {
      F0 = 300;
    };

    //---------------------------------
    AnyViaPointMuscle DeltodeusA = {
      AnyMuscleModel &MusMdl = .MusMdl;
      AnyRefNode &Org = Main.ArmModel.GlobalRef.DeltodeusA;
      AnyRefNode &Ins = ..Segs.UpperArm.DeltodeusA;
      AnyDrawMuscle DrwMus = {
        RGB = {149/256,51/256,55/256};
        Bulging =1.0;
        ColorScale =1.0;
        RGBColorScale = {0.957031, 0.785156, 0.785156};
        MaxStress = 50000; //N/m^2 //This number is for graphics only!
      };
    };

    //---------------------------------
    AnyViaPointMuscle DeltodeusB = {
      AnyMuscleModel &MusMdl = .MusMdl;
      AnyRefNode &Org = Main.ArmModel.GlobalRef.DeltodeusB;
      AnyRefNode &Ins = ..Segs.UpperArm.DeltodeusB;
      AnyDrawMuscle DrwMus = {
        RGB = {149/256,51/256,55/256};
        Bulging =1.0;
        ColorScale =1.0;
        RGBColorScale = {0.957031, 0.785156, 0.785156};
        MaxStress = 50000; //N/m^2 //This number is for graphics only!
      };
    };


    //---------------------------------
    AnyViaPointMuscle Brachialis = {
      AnyMuscleModel &MusMdl = .MusMdl;
      AnyRefNode &Org = ..Segs.UpperArm.Brachialis;
      AnyRefNode &Ins = ..Segs.LowerArm.Brachialis;
      AnyDrawMuscle DrwMus = {
        RGB = {149/256,51/256,55/256};
        Bulging =1.0;
        ColorScale =1.0;
        RGBColorScale = {0.957031, 0.785156, 0.785156};
        MaxStress = 50000; //N/m^2 //This number is for graphics only!
      };
    };

    //---------------------------------
    AnyViaPointMuscle Brachioralis = {
      AnyMuscleModel &MusMdl = .MusMdl;
      AnyRefNode &Org = ..Segs.UpperArm.Brachioradialis;
      AnyRefNode &Ins = ..Segs.LowerArm.Brachioradialis;
      AnyDrawMuscle DrwMus = {
        RGB = {149/256,51/256,55/256};
        Bulging =1.0;
        ColorScale =1.0;
        RGBColorScale = {0.957031, 0.785156, 0.785156};
        MaxStress = 50000; //N/m^2 //This number is for graphics only!
      };
    };

    //---------------------------------
    AnyViaPointMuscle BicepsShort = {
      AnyMuscleModel &MusMdl = .MusMdl;
      AnyRefNode &Org = ..Segs.UpperArm.BicepsShort;
      AnyRefNode &Ins = ..Segs.LowerArm.Biceps;
      AnyDrawMuscle DrwMus = {
        RGB = {149/256,51/256,55/256};
        Bulging =1.0;
        ColorScale =1.0;
        RGBColorScale = {0.957031, 0.785156, 0.785156};
        MaxStress = 50000; //N/m^2 //This number is for graphics only!
      };
    };

    //---------------------------------
    AnyViaPointMuscle TricepsShort = {
      AnyMuscleModel &MusMdl = .MusMdl;
      AnyRefNode &Org = ..Segs.UpperArm.TricepsShort;
      AnyRefNode &Ins = ..Segs.LowerArm.Triceps;
      AnyDrawMuscle DrwMus = {
        RGB = {149/256,51/256,55/256};
        Bulging =1.0;
        ColorScale =1.0;
        RGBColorScale = {0.957031, 0.785156, 0.785156};
        MaxStress = 50000; //N/m^2 //This number is for graphics only!
      };
    };

    //---------------------------------
    AnyViaPointMuscle BicepsLong = {
      AnyMuscleModel &MusMdl = .MusMdl;
      AnyRefNode &Org = Main.ArmModel.GlobalRef.BicepsLong;
      AnyRefNode &Ins = ..Segs.LowerArm.Biceps;
      AnyDrawMuscle DrwMus = {
        RGB = {149/256,51/256,55/256};
        Bulging =1.0;
        ColorScale =1.0;
        RGBColorScale = {0.957031, 0.785156, 0.785156};
        MaxStress = 50000; //N/m^2 //This number is for graphics only!
      };
    };

    //---------------------------------
    AnyViaPointMuscle TricepsLong = {
      AnyMuscleModel &MusMdl = .MusMdl;
      AnyRefNode &Org = Main.ArmModel.GlobalRef.TricepsLong;
      AnyRefNode &Ins = ..Segs.LowerArm.Triceps;
      AnyDrawMuscle DrwMus = {
        RGB = {149/256,51/256,55/256};
        Bulging =1.0;
        ColorScale =1.0;
        RGBColorScale = {0.957031, 0.785156, 0.785156};
        MaxStress = 50000; //N/m^2 //This number is for graphics only!
      };
    };

  };

}; // End of ArmModel

  
// =======================================================
// "The body study"
// =======================================================
AnyBodyStudy Study = {
  AnyFolder& Model = Main.ArmModel;
  Gravity = {0,0,-9.81};
  nStep = 40;
  
  AnyOutputFile FileOutput = {
      // Write output to a csv file with the number of steps in filename
      FileName = "Output/TestOutput_"+ANYBODY_NAME_MAINFILEDIR+ ".csv";
      Header =   {
      TitleSectionOnOff = Off;
      ConstSectionOnOff = Off;
      VarSectionOnOff = Off;
      ColumnNamesOnOff = On;
      };
      AnyFloat MaxMusAct = .MaxMuscleActivity;
      AnyFloat ElbowAngle = .Model.Drivers.ElbowMotion.Pos;
      AnyFloat BicepsLong = .Model.Muscles.BicepsLong.Activity;
  };
  
  
};



}; // End of file