!
! 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_domain_decomposition_equivalent_circuit
!
! NAME
!     write_spice_domain_decomposition_equivalent_circuit
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     Create the node numbering for the modal transmission lines and 
!     write the modal decomposition matrix process
!     The circuit diagram is shown in the Theory_Manual_Figure 3.5
!
!     INPUTS REQUIRED
!     1. The conductor based node numbers at the terminations of the bundle
!     2. The inverse domain voltage transformation matrix (MVI) and the domain current transformation matrix (MI)
!     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 domains are created in the subroutine and
!        are returned to the calling process
!     
! COMMENTS
!     The naming on the domain side could be made clearer - domain and conductor within domain not just a domain_conductor maybe...
!     This will need lists relating external_conductor to domain and external_conductor to conductor_in_domain
!
! HISTORY
!
!     STAGE 3 developments started 24/3/2016
!     9/5/2017 CJS Document software with reference to Theory_Manual 
!
SUBROUTINE write_spice_domain_decomposition_equivalent_circuit(vref_node,next_free_node,end,n_conductors,dim,   &
                                                        conductor_nodes,real_MVI,real_MI,domain_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)     :: end                             ! end number of transmission line (1 or 2)
  integer,intent(IN)     :: n_conductors                    ! total number of conductors
  integer ,intent(IN)    :: dim                             ! dimension of domain decomposition matrices
  real(dp),intent(IN)    :: real_MI(1:dim,1:dim)            ! Current domain decomposition matrix
  real(dp),intent(IN)    :: real_MVI(1:dim,1:dim)           ! Inverse of Voltage domain decomposition matrix
  
  integer,intent(IN)     :: conductor_nodes(1:n_conductors) ! known node numbers for connecting the domain decomposition circuits
  integer,intent(OUT)    :: domain_nodes(1:n_conductors)    ! new node numbers for domain based voltages and currents. These are generated here

! local variables

! variables for domain decomposition

  integer :: domain                           ! domain number
  integer :: internal_nodes(1:dim,1:dim+1)    ! array of internal nodes required

  character(LEN=spice_name_length) :: name_head  ! head of component name
  
  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

  integer :: conductor   ! conductor loop variable
  integer :: d_c         ! domain_conductor loop variable

! START
 
! checks on dimensions

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

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

! Zero value voltage source used for current sensing
  name_head='Vmeas_domain_decomp'
  do conductor=1,dim
    CALL build_name_with_conductor_and_end(name_head,conductor,end,Vmeas_name(conductor))
  end do  
  
! Voltage controlled voltage source on conductor side of circuit
! See comment regarding naming...
  name_head='E_domain_decomp'
  do conductor=1,dim
    do d_c=1,dim
      CALL build_name_with_conductor_domainConductor_and_end(name_head,conductor,d_c,end,E_name(conductor,d_c))
    end do  
  end do  

! Current controlled current source on domain based side of circuit
! See comment regarding naming...
  name_head='F_domain_decomp'
  do conductor=1,dim
    do d_c=1,dim
      CALL build_name_with_conductor_domainConductor_and_end(name_head,conductor,d_c,end,F_name(conductor,d_c))
    end do  
  end do  

! write the spice circuit elements

  CALL write_spice_comment('Domain 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('Domain decomposition Voltage controlled voltage sources (domain to conductor)')
  
  do conductor=1,dim
    do d_c=1,dim
      write(spice_model_file_unit,'(A30,4I6,ES16.6)')E_name(conductor,d_c), &
                                                    internal_nodes(conductor,d_c),internal_nodes(conductor,d_c+1), &
                                                    domain_nodes(d_c),vref_node,real_MVI(conductor,d_c)
    end do
  end do  

  CALL write_spice_comment('Domain decomposition Current controlled current sources (conductor to domain)')
  
  do d_c=1,dim
    do conductor=1,dim
      write(spice_model_file_unit,'(A30,2I6,A,A30,ES16.6)')F_name(conductor,d_c), &
                           vref_node,domain_nodes(d_c),' ',Vmeas_name(conductor),real_MI(d_c,conductor)
    end do
  end do  

END SUBROUTINE write_spice_domain_decomposition_equivalent_circuit
