!
! 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 flex_cable_set_parameters
! SUBROUTINE flex_cable_set_internal_domain_information
! SUBROUTINE flex_cable_plot
!
! NAME
!     flex_cable_set_parameters
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     Set the overall parameters for a flex_cable cable
!
! COMMENTS
!      
!
! HISTORY
!
!     started 26/9/2016 CJS 
!     16/11/2017 CJS Include network synthesis process to replace s-domain transfer functions
!
!
SUBROUTINE flex_cable_set_parameters(cable)

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  type(cable_specification_type),intent(INOUT)    :: cable

! local variables

! START

  cable%cable_type=cable_geometry_type_flex_cable
  cable%tot_n_conductors=0        ! this is set in the cable specification file
  cable%tot_n_domains=1
  cable%n_external_conductors=0   ! this is set in the cable specification file
  cable%n_internal_conductors=0  
  cable%n_internal_domains=0
  cable%n_parameters=6
  cable%n_dielectric_filters=1
  cable%n_transfer_impedance_models=0
  
END SUBROUTINE flex_cable_set_parameters
!
! NAME
!     flex_cable_set_internal_domain_information
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     Set the overall parameters for a flex_cable cable
!
! COMMENTS
!     Set the dimension of the domain transformation matrices to include an external reference conductor for the cable 
!      
!
! HISTORY
!
!     started 13/4/2016 CJS 
!     8/5/2017         CJS: Include references to Theory_Manual
!
!
SUBROUTINE flex_cable_set_internal_domain_information(cable)

USE type_specifications
USE general_module
USE constants

IMPLICIT NONE

! variables passed to subroutine

  type(cable_specification_type),intent(INOUT)    :: cable

! local variables

  integer :: nc
  integer :: dim
  integer :: i,row,col
  
  real(dp) :: full_width,flex_cable_xmin
  real(dp) :: wd,hd,x0
  
! variables for cable parameter checks 
  logical :: cable_spec_error
  real(dp) :: w
  real(dp) :: h
  real(dp) :: s
  real(dp) :: dox
  real(dp) :: doy
  real(dp) :: sigma
  
  type(Sfilter) :: epsr
  
  character(LEN=error_message_length) :: message 
  
  character(LEN=2) :: conductor_string

! START

! Check the cable parameters
  nc=cable%tot_n_conductors 

  w=cable%parameters(1)   ! wire width
  h=cable%parameters(2)   ! wire height
  s=cable%parameters(3)   ! wire separation
  dox=cable%parameters(4)   ! dielectric offset in x
  doy=cable%parameters(5)   ! dielectric offset in y
  sigma=cable%parameters(6)   ! conductivity
  
  epsr=cable%dielectric_filter(1)
  
  cable_spec_error=.FALSE.    ! assume no errors initially
  message=''
  CALL flex_cable_check(nc,w,h,s,dox,doy,cable_spec_error,cable%cable_name,message)
  CALL dielectric_check(epsr,cable_spec_error,cable%cable_name,message)
   
  if (cable_spec_error) then       
    run_status='ERROR in cable_model_builder, error on parameters for cable:'//trim(cable%cable_name)//'. '//trim(message)
    CALL write_program_status()
    STOP 1
  end if

! set the information related ot the number of conductors

  cable%n_external_conductors=nc
    
! Set the domain decomposition matrices ! Theory_Manual_Eqn 6.17, 6.18

! The dimension of the domain transformation matrices is the number of conductors+1
  dim=nc+1

  cable%MI%dim=dim
  ALLOCATE(cable%MI%mat(dim,dim))
  cable%MI%mat(:,:)=0d0
  
  cable%MV%dim=dim
  ALLOCATE(cable%MV%mat(dim,dim))
  cable%MV%mat(:,:)=0d0

  do i=1,nc
    row=i
    col=row
    cable%MI%mat(row,col)=1d0
  end do
  do i=1,dim
    row=dim
    col=i
    cable%MI%mat(row,col)=1d0
  end do

  do i=1,nc
    row=i
    col=row
    cable%MV%mat(row,col)=1d0
    cable%MV%mat(row,dim)=-1d0
  end do
  cable%MV%mat(dim,dim)=1d0
  
! Set the local reference conductor numbering  
  ALLOCATE( cable%local_reference_conductor(nc) )
  cable%local_reference_conductor(1:nc)=0              ! external domain conductor, reference not known

! Set the local domain information: include a reference conductor in the count
  ALLOCATE( cable%local_domain_n_conductors(1:cable%tot_n_domains) )
  cable%local_domain_n_conductors(1)=nc+1     ! external domain 
   
! set the conductor impedance model for the conductors

  do i=1,cable%n_external_conductors

    cable%conductor_impedance(i)%impedance_model_type=impedance_model_type_rectangular_with_conductivity
    cable%conductor_impedance(i)%width=cable%parameters(1)
    cable%conductor_impedance(i)%height=cable%parameters(2)
    cable%conductor_impedance(i)%conductivity=cable%parameters(6)

  end do
 
! Set the external domain conductor and dielectric information

  full_width=w*cable%tot_n_conductors+s*(cable%tot_n_conductors-1)
  flex_cable_xmin=-full_width/2d0
  
  ALLOCATE( cable%external_model(cable%n_external_conductors) )
  do i=1,cable%n_external_conductors
    CALL reset_external_conductor_model(cable%external_model(i))
    cable%external_model(i)%conductor_type=rectangle
    cable%external_model(i)%conductor_width=w
    cable%external_model(i)%conductor_width2=w
    cable%external_model(i)%conductor_height=h

! work out the offset of the ith conductor
    x0=flex_cable_xmin+(w+s)*(i-1)+w/2d0
    cable%external_model(i)%conductor_ox=x0
    
  end do
  
! add a dielectric region to the first conductor which encloses the whole cable

! write the dielectric which is offset from the conductors    
  
  wd=full_width+dox*2d0
  hd=h+2d0*doy
  
! The dielectric is centred at the cable centre 
  x0=0d0
  
  cable%external_model(1)%dielectric_width=wd
  cable%external_model(1)%dielectric_height=hd
  cable%external_model(1)%dielectric_ox=x0
  cable%external_model(1)%dielectric_epsr=epsr

  CALL deallocate_Sfilter(epsr)
  
  ALLOCATE( cable%conductor_label(1:cable%tot_n_conductors) )
  do i=1,cable%tot_n_conductors
    write(conductor_string,'(I2)')i
    cable%conductor_label(i)='Cable name: '//trim(cable%cable_name)//   &
    '. type: '//trim(cable%cable_type_string)//'. conductor '//conductor_string//' : original_flex_cable conductor'
  end do
    
END SUBROUTINE flex_cable_set_internal_domain_information
!
! NAME
!     flex_cable_plot
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     plot flex_cable cable
!
! COMMENTS
!      
!
! HISTORY
!
!     started 23/9/2016 CJS 
!
!
SUBROUTINE flex_cable_plot(cable,x_offset,y_offset,theta,xmin,xmax,ymin,ymax)

USE type_specifications
USE general_module

IMPLICIT NONE

! variables passed to subroutine

  type(cable_specification_type),intent(IN)    :: cable
  
  real(dp),intent(IN) :: x_offset,y_offset,theta
  real(dp),intent(INOUT) ::  xmin,xmax,ymin,ymax
  
! local variables

  integer nc

  real(dp) :: full_width,flex_cable_xmin
  real(dp) :: x0,y0,x,y,w,h,s,wd,hd
  
  integer i

! START

! plot flex_cable conductor

  nc=cable%tot_n_conductors

  w=cable%parameters(1)   ! wire width
  h=cable%parameters(2)   ! wire height
  s=cable%parameters(3)   ! wire separation

  full_width=w*nc+s*(nc-1)
  flex_cable_xmin=-full_width/2d0

! loop over the individual conductors
  do i=1,nc

! work out the centre of this conductor before rotation
    x0=flex_cable_xmin+(w+s)*(i-1)+w/2d0
    y0=0.0
    
! work out the centre of this conductor when the flex cable is rotated and offset
    x=x_offset+x0*cos(theta)-y0*sin(theta)
    y=y_offset+x0*sin(theta)+y0*cos(theta)
    
! write the conductor    
    CALL write_rectangle(x,y,w,h,theta,conductor_geometry_file_unit,xmin,xmax,ymin,ymax)
   
  end do ! next conductor 

! write the dielectric which is offset from the conductors    
  
  wd=cable%external_model(1)%dielectric_width
  hd=cable%external_model(1)%dielectric_height
  CALL write_rectangle(x_offset,y_offset,wd,hd,theta,dielectric_geometry_file_unit,xmin,xmax,ymin,ymax)
   
  RETURN
  
END SUBROUTINE flex_cable_plot




