!
! 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_method_of_characteristics_equivalent_circuit
!
! NAME
!     write_spice_method_of_characteristics_equivalent_circuit
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     This code writes the circuit components required to implement the Spice circuit model for the 
!     method of characteristics for a single mode of propagation
!
!     The circuit is seen in Theory_Manual_Figure 3.9
!
!     INPUTS REQUIRED
!     1. The nodes at the terminations of the modal line (2 modal nodes and a reference node)
!     2. The mode impedance and propagation delay
!     3. The first free node available
!     4. The reference node number
!     5. The propagation correction filter function
!
!     OUTPUTS
!     The components required to implement the model are written to
!     the subcircuit file
!     
! COMMENTS
!     Frequency dependent method of characteristics implemented using a propagation correction filter applied at both ends of the
!     model
!
! HISTORY
!
!     STAGE 2 developments started 2/2/2016
!     STAGE 4 developments started 22/4/2016 CJS
!     15/6/2016 CJS simplify the code using subroutines for creating new node numbers and for writing comments. Also
!                   write the s-domain transfer function controlled sources using a separate subroutine which allows
!                   support for all spice versions.
!     9/5/2017 CJS Document software with reference to Theory_Manual 
!     20/10/2017 CJS call subroutine to write delay lines so we can choose to use LTRA or T elements
!     16/11/2017 CJS Include network synthesis process to replace s-domain transfer functions
!

SUBROUTINE write_spice_method_of_characteristics_equivalent_circuit(modal_end1_node,modal_end2_node,vref_node,&
                                                                    ZT_reference_end1_node,ZT_reference_end2_node, &
                                                                    Vplus_node,Vminus_node, &
                                                                    next_free_node,domain,mode,Zc,delay,Hp,length )

USE type_specifications
USE general_module
USE spice_cable_bundle_module
USE filter_module

IMPLICIT NONE

! variables passed to the subroutine

integer,intent(IN) :: modal_end1_node                     ! existing node for connection at end 1
integer,intent(IN) :: modal_end2_node                     ! existing node for connection at end 2
integer,intent(IN) :: vref_node                           ! sub-circuit reference node number
integer,intent(IN) :: ZT_reference_end1_node              ! existing node for method of characteristic source at end 1 
                                                          ! to be included allowing for trasfer impedance sources
integer,intent(IN) :: ZT_reference_end2_node              ! existing node for method of characteristic source at end 2
                                                          ! to be included allowing for trasfer impedance sources
integer,intent(OUT) :: Vplus_node                         ! Node number for the +z travelling characteristic variable (with no time delay)
integer,intent(OUT) :: Vminus_node                        ! Node number for the -z travelling characteristic variable (with no time delay)
integer,intent(INOUT)  :: next_free_node                  ! next free spice subcircuit node number 
integer,intent(IN)      :: domain                         ! domain number 
integer,intent(IN)      :: mode                           ! mode number within this domain 

real(dp),intent(IN)      :: Zc                            ! mode characteristic impedance
real(dp),intent(IN)      :: delay                         ! mode propagation delay (s)
Type(Sfilter),intent(IN) :: Hp                            ! mode propagation coffection filter function
real(dp),intent(IN)      :: length                        ! transmission line length (m)

! local variables

integer,parameter :: end1=1
integer,parameter :: end2=2

! component names:
character(LEN=spice_name_length) :: name_head
  
character(len=spice_name_length) :: modal_line_ZC_end1_name  ! characteristic impedance on MOC model termination sub-circuits
character(len=spice_name_length) :: modal_line_ZC_end2_name

character(len=spice_name_length) :: modal_line_E_end1_name   ! controlled voltage sources on MOC model termination sub-circuit
character(len=spice_name_length) :: modal_line_E_end2_name

character(len=spice_name_length) :: delay_line_pz_name       ! delay line names
character(len=spice_name_length) :: delay_line_mz_name

character(len=spice_name_length) :: delay_line_E1_pz_name    ! Delay line source term1 names
character(len=spice_name_length) :: delay_line_E1_mz_name

character(len=spice_name_length) :: delay_line_E2_pz_name    ! Delay line source term1 names
character(len=spice_name_length) :: delay_line_E2_mz_name

character(len=spice_name_length) :: delay_line_ZC_pz_name   ! Delay line termination impedance names
character(len=spice_name_length) :: delay_line_ZC_mz_name

integer :: modal_line_E_end1_node     ! controlled voltage sourcenodes on MOC model termination sub-circuit
integer :: modal_line_E_end2_node

integer :: delay_line_E1_pz_node      ! Delay line source term1 nodes
integer :: delay_line_E1_mz_node

integer :: delay_line_E2_pz_node      ! Delay line source term2 nodes
integer :: delay_line_E2_mz_node

integer :: delay_line_ZC_pz_node     ! Delay line characteristic impedance termination nodes
integer :: delay_line_ZC_mz_node

! START

! First write the method of characteristics termination circuits

  CALL write_spice_comment('Modal impedance: end1')
  name_head='RZCm'
  CALL build_name_with_domain_mode_and_end(name_head,domain,mode,end1,modal_line_ZC_end1_name)
  CALL create_new_node(modal_line_E_end1_node,next_free_node)
  write(spice_model_file_unit,'(A30,2I6,ES16.6)')modal_line_ZC_end1_name,modal_end1_node,modal_line_E_end1_node,Zc
  
  CALL write_spice_comment('Modal impedance: end2')
  name_head='RZCm'
  CALL build_name_with_domain_mode_and_end(name_head,domain,mode,end2,modal_line_ZC_end2_name)
  CALL create_new_node(modal_line_E_end2_node,next_free_node)
  write(spice_model_file_unit,'(A30,2I6,ES16.6)')modal_line_ZC_end2_name,modal_end2_node,modal_line_E_end2_node,Zc


! Write the termination controlled sources including the propagtion correction
  CALL write_spice_comment('Modal frequency dependent voltage controlled voltage source : end1')
  name_head='m'
  CALL build_name_with_domain_mode_and_end(name_head,domain,mode,end1,modal_line_E_end1_name)
  CALL create_new_node(delay_line_ZC_mz_node,next_free_node)
  CALL write_s_domain_controlled_voltage_source(modal_line_E_end1_name,                        &
                                                delay_line_ZC_mz_node,vref_node,               &
                                                modal_line_E_end1_node,ZT_reference_end1_node, &
                                                Hp,1d0,vref_node,next_free_node)                 ! note: gain set to 1.0

  CALL write_spice_comment('Modal frequency dependent voltage controlled voltage source : end2')
  name_head='m'
  CALL build_name_with_domain_mode_and_end(name_head,domain,mode,end2,modal_line_E_end2_name)
  CALL create_new_node(delay_line_ZC_pz_node,next_free_node)
  CALL write_s_domain_controlled_voltage_source(modal_line_E_end2_name,                        &
                                                delay_line_ZC_pz_node,vref_node,               &
                                                modal_line_E_end2_node,ZT_reference_end2_node, &
                                                Hp,1d0,vref_node,next_free_node)                 ! note: gain set to 1.0

! Second write the delay lines for the +z and -z travelleing modes 

  CALL write_spice_comment('Delay line for positive z propagation')
  name_head='T_pz'
  CALL build_name_with_domain_mode_and_end(name_head,domain,mode,end1,delay_line_pz_name)
  CALL create_new_node(delay_line_E1_pz_node,next_free_node)
  
!  write(spice_model_file_unit,'(A30,4I6,A4,ES16.6,A4,ES16.6)')delay_line_pz_name,  &
!                       delay_line_ZC_pz_node,vref_node, &
!                       delay_line_E1_pz_node,vref_node, &
!                     ' Z0=',Zc,' TD=',delay
  CALL write_delay_line(delay_line_pz_name,delay_line_ZC_pz_node,vref_node,  &
                        delay_line_E1_pz_node,vref_node,Zc,delay,length)

  CALL write_spice_comment('Delay line for negative z propagation')
  name_head='T_mz'
  CALL build_name_with_domain_mode_and_end(name_head,domain,mode,end2,delay_line_mz_name)
  CALL create_new_node(delay_line_E1_mz_node,next_free_node)
!  write(spice_model_file_unit,'(A30,4I6,A4,ES16.6,A4,ES16.6)')delay_line_mz_name,  &
!                       delay_line_ZC_mz_node,vref_node, &
!                       delay_line_E1_mz_node,vref_node, &
!                     ' Z0=',Zc,' TD=',delay
  CALL write_delay_line(delay_line_mz_name,delay_line_ZC_mz_node,vref_node,  &
                        delay_line_E1_mz_node,vref_node,Zc,delay,length)


  CALL write_spice_comment('Modal impedances on modal positive z propagation delay lines')
  name_head='RZC_pz'
  CALL build_name_with_domain_mode_and_end(name_head,domain,mode,end1,delay_line_ZC_pz_name)
  write(spice_model_file_unit,'(A30,2I6,ES16.6)')delay_line_ZC_pz_name,    &
                             delay_line_ZC_pz_node,vref_node,Zc

  CALL write_spice_comment('Modal impedances on modal negative z propagation delay lines')
  name_head='RZC_mz'
  CALL build_name_with_domain_mode_and_end(name_head,domain,mode,end2,delay_line_ZC_mz_name)
  write(spice_model_file_unit,'(A30,2I6,ES16.6)')delay_line_ZC_mz_name,    &
                             delay_line_ZC_mz_node,vref_node,Zc


! Write delay line controlled sources for the +z (Theory_Manual_Eqn  3.58 ) and -z (Theory_Manual_Eqn 3.59 )
! travelleing modes including the propagtion correction
  
  CALL write_spice_comment('Delay line frequency dependent controlled sources for positive z propagation')
  name_head='m_pz'
  CALL build_name_with_domain_mode_and_end(name_head,domain,mode,end1,delay_line_E1_pz_name)
  name_head='m_pz'
  CALL build_name_with_domain_mode_and_end(name_head,domain,mode,end2,delay_line_E2_pz_name) 
  CALL create_new_node(delay_line_E2_pz_node,next_free_node)
  CALL write_s_domain_controlled_voltage_source(delay_line_E1_pz_name,                        &
                                                modal_end1_node,vref_node,                    &
                                                delay_line_E1_pz_node,delay_line_E2_pz_node,  &
                                                Hp,2d0,vref_node,next_free_node)                ! note: gain set to 2.0
  CALL write_s_domain_controlled_voltage_source(delay_line_E2_pz_name,                        &
                                                modal_line_E_end1_node,vref_node,             &
                                                delay_line_E2_pz_node,vref_node,              &
                                                Hp,-1d0,vref_node,next_free_node)               ! note: gain set to -1.0
  
  CALL write_spice_comment('Delay line frequency dependent controlled sources for negative z propagation')
  name_head='m_mz'
  CALL build_name_with_domain_mode_and_end(name_head,domain,mode,end1,delay_line_E1_mz_name)
  name_head='m_mz'
  CALL build_name_with_domain_mode_and_end(name_head,domain,mode,end2,delay_line_E2_mz_name) 
  CALL create_new_node(delay_line_E2_mz_node,next_free_node)
  CALL write_s_domain_controlled_voltage_source(delay_line_E1_mz_name,                        &
                                                modal_end2_node,vref_node,                    &
                                                delay_line_E1_mz_node,delay_line_E2_mz_node,  &
                                                Hp,2d0,vref_node,next_free_node)                ! note: gain set to 2.0
  CALL write_s_domain_controlled_voltage_source(delay_line_E2_mz_name,                        &
                                                modal_line_E_end2_node,vref_node,             &
                                                delay_line_E2_mz_node,vref_node,              &
                                                Hp,-1d0,vref_node,next_free_node)               ! note: gain set to -1.0
                                                
! save the node numbers for the +z and -z travelling characteristic variables (with no time delay) as these
! may be required for transfer impedance coupling models

  Vplus_node= delay_line_E1_pz_node
  Vminus_node=delay_line_E1_mz_node
                               
END SUBROUTINE write_spice_method_of_characteristics_equivalent_circuit
