!
! 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 write_spice_modal_decomposition_equivalent_circuit
!
! NAME
!     write_spice_modal_decomposition_equivalent_circuit
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     Create the node numbering for the modal transmission lines and 
!     write the modal decomposition circuit as shown in Theory_Manual_Figure 3.8
!
!     INPUTS REQUIRED
!     1. The conductor based node numbers at the terminations of the transmission line
!     2. The modal voltage transformation matrix and the inverse current transformation matrix 
!     3. The first free node available
!     4. The reference node number
!
!     OUTPUTS
!     1. The components required to implement the model are written to
!        the subcircuit file
!     2. The nodes created for the individual modes are created in the subroutine and
!        are returned to the calling process
!     
! COMMENTS
!     For the stage 2 developments the propagation is frequency independent
!     Ulitmately frequency dependent models will be included here using
!     the s-domain transfer function.
!     
!
! HISTORY
!
!     STAGE 2 developments started 2/2/2016
!     9/5/2017 CJS Document software with reference to Theory_Manual 
!
  SUBROUTINE write_spice_modal_decomposition_equivalent_circuit(vref_node,next_free_node,domain,end,n_conductors,dim,   &
                                                                conductor_nodes,real_TV,real_TII,modal_nodes)
USE type_specifications
USE general_module
USE spice_cable_bundle_module

IMPLICIT NONE

! variables passed to the subroutine

  integer,intent(IN)     :: vref_node                       ! sub-circuit reference node number
  integer,intent(INOUT)  :: next_free_node                  ! next free spice subcircuit node number 
  integer,intent(IN)     :: domain                          ! domain number
  integer,intent(IN)     :: end                             ! end number of transmission line (1 or 2)
  integer,intent(IN)     :: n_conductors                    ! total number of conductors
  integer ,intent(IN)    :: dim                             ! dimension of modal decomposition matrices
  real(dp),intent(IN)    :: real_TII(1:dim,1:dim)           ! Inverse of Current modal decomposition matrix
  real(dp),intent(IN)    :: real_TV(1:dim,1:dim)            ! Voltage modal decomposition matrix
  integer,intent(IN)     :: conductor_nodes(1:n_conductors) ! known node numbers for connecting the domain decomposition circuits
  integer,intent(OUT)    :: modal_nodes(1:n_conductors)     ! new node numbers for individual mode based voltages and currents. These are generated here
  
! local variables

! variables for modal decomposition

  integer :: n_modes                        ! number of modes in this domain
  integer :: internal_nodes(1:dim,1:dim+1)  ! array of internal node numbers in the model decomposition sub-circuit

  character(LEN=spice_name_length) :: name_head         ! base of the name for modal decompoition components
   
  character(LEN=spice_name_length) :: Vmeas_name(1:dim)   ! name for null voltage sources used for current sensing
  character(LEN=spice_name_length) :: E_name(1:dim,1:dim) ! name for voltage controlled voltage sources
  character(LEN=spice_name_length) :: F_name(1:dim,1:dim) ! name for current controlled current sources

! loop variables
  integer :: conductor
  integer :: mode

! START
 
  n_modes=n_conductors-1
! checks on dimensions
  if (n_modes.NE.dim) then
    run_status='ERROR: write_spice_modal_decomposition, discrepancy in dimensions, dim.NE.n_modes'
    if (verbose) write(*,*)'ERROR: domain=',domain,' n_conductors=',n_conductors,' n_modes=',n_modes,' dim=',dim
    CALL write_program_status()
    STOP 1
  end if
  
! set the modal node numbers. The last modal node is set to the reference node number

  do mode=1,dim
    modal_nodes(mode)=next_free_node
    next_free_node=next_free_node+1
  end do
  modal_nodes(n_conductors)=vref_node
  
! set the internal node numbers, (n_conductors-1) for each mode (n_modes=n_conductors-1)
  
  do conductor=1,dim
    do mode=1,dim
      internal_nodes(conductor,mode)=next_free_node
      next_free_node=next_free_node+1
    end do
    internal_nodes(conductor,n_modes+1)=vref_node
  end do
    
! create the component names

! Zero value voltage source used for current sensing
  name_head='Vmeas_mode_decomp'
  do conductor=1,dim
    CALL build_name_with_domain_conductor_and_end(name_head,domain,conductor,end,Vmeas_name(conductor))
  end do  

! Voltage controlled voltage source on domain conductor side of circuit
  name_head='E_mode_decomp'
  do conductor=1,dim
    do mode=1,dim
      CALL build_name_with_domain_conductor_mode_and_end(name_head,domain,conductor,mode,end,E_name(conductor,mode))
    end do  
  end do  

! Current controlled current source on mode based side of circuit
  name_head='F_mode_decomp'
  do conductor=1,dim
    do mode=1,dim
      CALL build_name_with_domain_conductor_mode_and_end(name_head,domain,conductor,mode,end,F_name(conductor,mode))
    end do  
  end do  

! write the spice circuit elements along with comments

 CALL write_spice_comment('Modal decomposition Voltage sources used for current sensing')
  
  do conductor=1,dim
    write(spice_model_file_unit,'(A30,2I6)')Vmeas_name(conductor),conductor_nodes(conductor),internal_nodes(conductor,1)
  end do  
  
  CALL write_spice_comment('Modal decomposition Voltage controlled voltage sources (mode to conductor)')
  
  do conductor=1,dim
    do mode=1,dim
      write(spice_model_file_unit,'(A30,4I6,ES18.8)')E_name(conductor,mode), &
                                                    internal_nodes(conductor,mode),internal_nodes(conductor,mode+1), &
                                                    modal_nodes(mode),vref_node,real_TV(conductor,mode)
    end do
  end do  
  
  CALL write_spice_comment('Modal decomposition Current controlled current sources (conductor to mode)')
  
  do mode=1,dim
    do conductor=1,dim
      write(spice_model_file_unit,'(A30,2I6,A,A30,ES18.8)')F_name(conductor,mode), &
                           vref_node,modal_nodes(mode),' ',Vmeas_name(conductor),real_TII(mode,conductor)
    end do
  end do  

  END SUBROUTINE write_spice_modal_decomposition_equivalent_circuit
