!
! This file is part of SACAMOS, State of the Art CAble MOdels for Spice. 
! It was developed by the University of Nottingham and the Netherlands Aerospace 
! Centre (NLR) for ESA under contract number 4000112765/14/NL/HK.
! 
! Copyright (C) 2016-2018 University of Nottingham
! 
! SACAMOS is free software: you can redistribute it and/or modify it under the 
! terms of the GNU General Public License as published by the Free Software 
! Foundation, either version 3 of the License, or (at your option) any later 
! version.
! 
! SACAMOS is distributed in the hope that it will be useful, but 
! WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
! or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
! for more details.
! 
! A copy of the GNU General Public License version 3 can be found in the 
! file GNU_GPL_v3 in the root or at <http://www.gnu.org/licenses/>.
! 
! SACAMOS uses the EISPACK library (in /SRC/EISPACK). EISPACK is subject to 
! the GNU Lesser General Public License. A copy of the GNU Lesser General Public 
! License version can be found in the file GNU_LGPL in the root of EISPACK 
! (/SRC/EISPACK ) or at <http://www.gnu.org/licenses/>.
! 
! The University of Nottingham can be contacted at: ggiemr@nottingham.ac.uk
!
!
! FILE CONTENTS:
!SUBROUTINE set_conductor_positions_for_Einc
!
!
! NAME
!     SUBROUTINE set_conductor_positions_for_Einc
!
! DESCRIPTION
!  The x and y coordinates of each individual conductor
!  are set in the bundle structure for subsequenct use in calculation of the incident field
!  excitation source terms on each conductor.
!     
! COMMENTS
!     
!
! HISTORY
!    started 2/12/2015 CJS
!    9/5/2016 CJS   plot bundle cross section to jpeg file as well as the screen
!    14/6/2016 CJS  Create the arrays of x and y coordinates of each conductor which are used in the incident field excitation
!    5/9/2016 CJS   include additional shielded cable types with surface impedance loss models
!    20/4/2017 CJS Separate the set_conductor_positions_for_Einc process from plot_bundle_cross_section
!    16/3/2018 CJS add y offset for ML_flex_cable
!

SUBROUTINE set_conductor_positions_for_Einc(bundle)

USE type_specifications
USE general_module
USE constants
USE cable_module
USE cable_bundle_module
USE PUL_parameter_module
USE filter_module
USE maths

  IMPLICIT NONE

! variables passed to subroutine

  type(bundle_specification_type),intent(INOUT)    :: bundle
  
! local variables
  
  integer  :: cable
  real(dp) :: xmin,xmax,ymin,ymax   ! the extent of the plot area
  real(dp) :: xc,yc                 ! the centre of the plot area
  real(dp) :: length                ! edge length of the plot area
  
  integer  :: conductor             ! counter for conductors
  integer  :: i                     ! loop counter
  
  real(dp) ::  FC_x0,FC_y0,theta    ! flex cable conductor offset and rotation angle

! START
  
! allocate conductor based position information and reset the conductor count

  write(*,*)'ALLOCATING cx, cy,',bundle%tot_n_conductors

  ALLOCATE( bundle%conductor_x_offset(1:bundle%tot_n_conductors) )
  ALLOCATE( bundle%conductor_y_offset(1:bundle%tot_n_conductors) )
  bundle%conductor_x_offset(1:bundle%tot_n_conductors)=0d0
  bundle%conductor_y_offset(1:bundle%tot_n_conductors)=0d0
  
! Set the conductor position for incident field excitation to the centre of the cable which it belongs to
! This naturally eliminates incident field coupling onto any modes within a cable

  conductor=0

! loop over the cables in the bundle  
  do cable=1,bundle%n_cables
    
! loop over the conductors in the cable
    if ((bundle%cable(cable)%cable_type.EQ.cable_geometry_type_flex_cable).OR.    &
        (bundle%cable(cable)%cable_type.EQ.cable_geometry_type_ML_flex_cable) ) then
            
! We include a fix here for flex cables which have to have their conductor offsets calculated here
! work out the centre of this conductor before rotation

      theta=bundle%cable_angle(cable)
      
      do i=1,bundle%cable(cable)%tot_n_conductors
        FC_x0=bundle%cable(cable)%external_model(i)%conductor_ox
        FC_y0=bundle%cable(cable)%external_model(i)%conductor_oy
! work out the centre of this conductor when the flex cable is rotated and offset
        conductor=conductor+1
        bundle%conductor_x_offset(conductor)=FC_x0*cos(theta)-FC_y0*sin(theta)+bundle%cable_x_offset(cable)
        bundle%conductor_y_offset(conductor)=FC_x0*sin(theta)+FC_y0*cos(theta)+bundle%cable_y_offset(cable)
      end do
      
    else ! not a flex cable

      do i=1,bundle%cable(cable)%tot_n_conductors
        conductor=conductor+1
        bundle%conductor_x_offset(conductor)=bundle%cable_x_offset(cable)
        bundle%conductor_y_offset(conductor)=bundle%cable_y_offset(cable)
      end do
      
    end if
      
  end do

END SUBROUTINE set_conductor_positions_for_Einc

