Blame view

SRC/BUNDLE_DOMAIN_CREATION/create_global_domain_structure.F90 61.3 KB
886c558b   Steve Greedy   SACAMOS Public Re...
1
!
fe64b32b   Chris Smartt   Update file heade...
2
! This file is part of SACAMOS, State of the Art CAble MOdels for Spice. 
886c558b   Steve Greedy   SACAMOS Public Re...
3
4
5
! It was developed by the University of Nottingham and the Netherlands Aerospace 
! Centre (NLR) for ESA under contract number 4000112765/14/NL/HK.
! 
fe64b32b   Chris Smartt   Update file heade...
6
! Copyright (C) 2016-2018 University of Nottingham
886c558b   Steve Greedy   SACAMOS Public Re...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
! 
! 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
!
fe64b32b   Chris Smartt   Update file heade...
28
!
886c558b   Steve Greedy   SACAMOS Public Re...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
! NAME
!     SUBROUTINE create_global_domain_structure
!
! DESCRIPTION
!   The processes implemented in this subroutine calculate the matrices which are required to 
!   characterise the multi-conductor cable propagation in both a local, domain based system for the Spice model development
!   and in a global basis for analysis of the bundle as a whole for the validation test circuit.
!   The matrices characterising the propagation are :
!       the domain based 'high frequency' L and C matrices,
!       the domain based frequency dependent Z and Y matrices,
!       the global  'high frequency' L and C matrices,
!       the global frequency dependent Z and Y matrices,
!       the voltage and current domain decomposition matrices MI and MV
!
!   For shielded domains the L, C, Z, and Y matrices are known from the cable data structures
!   For any overshield domains and the external domain these matrices are calculated here via
!   calls to the appopriate PUL calculation subroutines
!
!   The processes implemented in this subroutine are summarised as follows:
!
! 1. Count the total number of conductors in the bundle and Allocate and reset the local data structures 
! 2. Loop over the cables filling the referencing arrays with local information
! 3. For each cable, work out which domain it is in and hence its reference conductor number.
! 4. Count the number of conductors in the external domain to check whether it is viable i.e. at least 2 conductors
! 5. build the global_domain numbering, ignoring any unused domains
! 6. build the domain based L and C matrices plus the global MI and MV matrices and the global L and C matrices
! 7. Calculate the inductance and capacitance matrices for the external domains (domains within overshields and the external domain)
! 8. Copy the cable based conductor impedance (loss) models to the bundle structure
189467e4   Steve Greedy   First Public Release
57
! 8b. Copy the cable based conductor labels to the bundle structure
886c558b   Steve Greedy   SACAMOS Public Re...
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
! 9  save numbering information required for the transfer impedance calculation
! 10. deallocate local memory
!     
! COMMENTS
!     
!
! HISTORY
!    started 2/12/2015 CJS
!     27/4/2016 CJS: Include a conductor based impedance (loss) model
!     24/5/2016 CJS: Fix errors in MI and MV matrices for multi-conductor overshield domains
!     7/7/2016 CJS: Allow the use of the Laplace solver for L,C,G matrix calculation in external and overshield domains.
!     9/9/2016 CJS: Fix problem with MV and MI matrix for the twisted pair model. Elements were being overwritten incorrectly.
!                   when adding the external conductor contribution to the domain decomposition matrices   
!     4/10/2016 CJS: Save the local domain conductor numbering
!     17/5/2017 CJS: Work out which domains are Shielded Twisted Pair differential mode domains and flag these
!                    so that we can work out the is_shield flag properly in all circumstances.
189467e4   Steve Greedy   First Public Release
74
75
!     18/10/2017 CJS: include 8b. Copy the cable based conductor labels to the bundle structure
!     16/11/2017 CJS Include network synthesis process to replace s-domain transfer functions
0cd31073   Chris Smartt   Fix numbering err...
76
77
!     16/3/2018 CJS add y offset for ML_flex_cable
!     22/10/2019 CJS Fix bug with numbering here: overshields were numbered using terminal conductor number not global domain number
886c558b   Steve Greedy   SACAMOS Public Re...
78
!
cfcbcb46   Chris Smartt   Update of proximi...
79
!     October 2023 CJS Proximity_effects branch: Embed FastHenry2 frequency dependent proximity effects
ca596cf4   Chris Smartt   Start to embed Fa...
80
!
886c558b   Steve Greedy   SACAMOS Public Re...
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

SUBROUTINE create_global_domain_structure(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

! global cable, conductor and domain numbers and loop variables
  integer :: cable
  integer :: tot_n_cables  
  integer :: tot_n_cables_without_ground_plane
  
  integer :: tot_n_conductors,conductor,conductor_count,terminal_conductor
  integer :: tot_n_domains,tot_n_viable_domains,domain,domain_count
  integer :: tot_n_internal_domains

! local conductor and domain numbers and loop variables

  integer :: local_n_conductors,local_conductor,n_cable_conductors
  integer :: local_cable_conductor,local_domain_conductor
  integer :: local_n_domains,local_domain
  integer :: first_external_domain
0cd31073   Chris Smartt   Fix numbering err...
116
  integer :: reference_conductor,global_reference_conductor
886c558b   Steve Greedy   SACAMOS Public Re...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  integer :: first_external_conductor
  
! variables for looking at overshields and determining which domain cables are in
  integer :: tot_n_overshields,n_overshield_domains,overshield
  integer,allocatable  :: overshield_shape(:)
  real(dp),allocatable :: overshield_x(:)
  real(dp),allocatable :: overshield_y(:)
  real(dp),allocatable :: overshield_r(:)
  real(dp),allocatable :: overshield_w(:)
  real(dp),allocatable :: overshield_w2(:)
  real(dp),allocatable :: overshield_h(:)
  integer,allocatable  :: overshield_domain(:)
  integer,allocatable  :: overshield_reference_terminal_conductor(:)

  integer,allocatable  :: overshield_n_conductors(:)
  integer,allocatable  :: overshield_terminal_conductor(:,:) 
  
  real(dp) :: cable_x,cable_y,dist_cable_to_overshield_centre
  logical :: is_overshield_domain

! referencing arrays
  integer,allocatable :: terminal_conductor_to_cable(:)  
  integer,allocatable :: terminal_conductor_to_cable_local_domain(:)  
  integer,allocatable :: terminal_conductor_to_global_domain(:)  
  integer,allocatable :: terminal_conductor_to_global_domain_conductor(:)  
  integer,allocatable :: terminal_conductor_to_reference_global_domain_conductor(:)  
  integer,allocatable :: terminal_conductor_to_reference_terminal_conductor(:)  
  integer,allocatable :: global_domain_conductor_to_terminal_conductor(:)  
  
  logical,allocatable :: terminal_conductor_is_reference_conductor(:)  
  logical,allocatable :: domain_is_TP_differential_mode(:)  

  integer,allocatable :: cable_reference_domain(:) 
  integer,allocatable :: cable_reference_conductor(:) 
  
  integer,allocatable :: external_terminal_conductor(:)
  logical :: is_external_domain
  integer :: external_conductor_count
  
  integer,allocatable :: global_domain_reference_conductor(:)

! structure used by the Per-Unit-Length parameter calculation for overshield and external domains
  type(PUL_type)    :: PUL

! loop variables for matrix operations
  integer :: dim
  integer :: row,col
  integer :: row_l,col_l
  integer :: row_g,col_g
189467e4   Steve Greedy   First Public Release
166
167
  
  character(LEN=3) :: conductor_string  ! used to generate the conductor labels in the bundle
886c558b   Steve Greedy   SACAMOS Public Re...
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
     
! START

  if(verbose) write(*,*)'CALLED: create_global_domain_structure'
  
! Copy some data to local variables for clarity of notation

  tot_n_cables=bundle%n_cables
  
  if (bundle%ground_plane_present) then
    tot_n_cables_without_ground_plane=tot_n_cables-1
  else
    tot_n_cables_without_ground_plane=tot_n_cables
  end if
  
  if(verbose) write(*,*)' Total number of cables=',tot_n_cables
  if(verbose) write(*,*)' Total number of cables not including ground plane=',tot_n_cables_without_ground_plane
  
! 1a. Count the total number of conductors in the bundle

  tot_n_conductors=0
  do cable=1,tot_n_cables  
    tot_n_conductors = tot_n_conductors + bundle%cable(cable)%tot_n_conductors                   
  end do
  
  if(verbose) write(*,*)' Total number of conductors=',tot_n_conductors
  
  bundle%tot_n_conductors=tot_n_conductors
  bundle%system_dimension=tot_n_conductors-1
  
! 1b Allocate and reset the local data structures 

  ALLOCATE( terminal_conductor_to_cable(1:tot_n_conductors) )
  ALLOCATE( terminal_conductor_to_cable_local_domain(1:tot_n_conductors) )
  ALLOCATE( terminal_conductor_to_global_domain(1:tot_n_conductors) )
  ALLOCATE( terminal_conductor_to_global_domain_conductor(1:tot_n_conductors) )
  ALLOCATE( terminal_conductor_to_reference_global_domain_conductor(1:tot_n_conductors) )
  ALLOCATE( terminal_conductor_to_reference_terminal_conductor(1:tot_n_conductors) )
  ALLOCATE( terminal_conductor_is_reference_conductor(1:tot_n_conductors) )
  ALLOCATE( global_domain_conductor_to_terminal_conductor(1:tot_n_conductors) )
  terminal_conductor_to_cable(1:tot_n_conductors)=0
  terminal_conductor_to_cable_local_domain(1:tot_n_conductors)=0
  terminal_conductor_to_global_domain(1:tot_n_conductors)=0
  terminal_conductor_to_global_domain_conductor(1:tot_n_conductors)=0
  terminal_conductor_to_reference_global_domain_conductor(1:tot_n_conductors)=0
  terminal_conductor_to_reference_terminal_conductor(1:tot_n_conductors)=0
  terminal_conductor_is_reference_conductor(1:tot_n_conductors)=.FALSE.
  global_domain_conductor_to_terminal_conductor(1:tot_n_conductors)=0
   
! numbering information required for the transfer impedance calculation
  ALLOCATE( bundle%terminal_conductor_is_shield_flag(1:bundle%tot_n_conductors) )
  ALLOCATE( bundle%terminal_conductor_to_inner_domain(1:bundle%tot_n_conductors) )
  ALLOCATE( bundle%terminal_conductor_to_outer_domain(1:bundle%tot_n_conductors) )
  ALLOCATE( bundle%terminal_conductor_to_global_domain_conductor(1:bundle%tot_n_conductors) )
  ALLOCATE( bundle%terminal_conductor_to_local_domain_conductor(1:bundle%tot_n_conductors) )
  ALLOCATE( bundle%terminal_conductor_to_reference_terminal_conductor(1:bundle%tot_n_conductors) )
  bundle%terminal_conductor_is_shield_flag(1:bundle%tot_n_conductors)=.FALSE.
  bundle%terminal_conductor_to_inner_domain(1:bundle%tot_n_conductors)=0
  bundle%terminal_conductor_to_outer_domain(1:bundle%tot_n_conductors)=0
  bundle%terminal_conductor_to_global_domain_conductor(1:bundle%tot_n_conductors)=0
  bundle%terminal_conductor_to_local_domain_conductor(1:bundle%tot_n_conductors)=0
  bundle%terminal_conductor_to_reference_terminal_conductor(1:bundle%tot_n_conductors)=0
  
! 2. Loop over the cables filling the referencing arrays with local information
! Also count the total number of internal domains in the bundle
  conductor=0
  tot_n_domains=0
  
  do cable=1,tot_n_cables
  
    local_n_domains=bundle%cable(cable)%tot_n_domains
    tot_n_domains=tot_n_domains+bundle%cable(cable)%n_internal_domains
    
    do local_domain=1,local_n_domains
    
      local_n_conductors=bundle%cable(cable)%local_domain_n_conductors(local_domain)-1 ! subtract 1 as the reference conductor is included
      
      do local_conductor=1,local_n_conductors
    
        conductor=conductor+1
        terminal_conductor_to_cable(conductor)=cable
        terminal_conductor_to_cable_local_domain(conductor)=local_domain
        
      end do
      
    end do ! next conductor in this cable
    
  end do
  
  tot_n_internal_domains=tot_n_domains
  first_external_domain=tot_n_domains+1
  
  if(verbose) then
    write(*,*)'Total number of internal domains=',tot_n_domains
    write(*,*)'First external domain=',first_external_domain
  end if
  
! check counting...
  if (conductor.NE.tot_n_conductors) then
    run_status='ERROR in create_global_domain_structure: last conductor.NE.tot_n_conductors'
    CALL write_program_status()
    STOP 1
  end if

! 3. For each cable, work out which domain it is in and hence its reference conductor number.

! 3a. first count the overshields

  tot_n_overshields=0
  do cable=1,tot_n_cables
    if (bundle%cable(cable)%cable_type.EQ.cable_geometry_type_overshield) then 
      tot_n_overshields=tot_n_overshields+1
    end if
  end do
  
  if(verbose) write(*,*)'Total number of over-shields=',tot_n_overshields  

! 3b. Get the position of the overshields
  if (tot_n_overshields.GT.0) then
  
    ALLOCATE( overshield_shape(1:tot_n_overshields)  )
    ALLOCATE( overshield_x(1:tot_n_overshields)  )
    ALLOCATE( overshield_y(1:tot_n_overshields)  )
    ALLOCATE( overshield_r(1:tot_n_overshields)  )
    ALLOCATE( overshield_w(1:tot_n_overshields)  )
    ALLOCATE( overshield_w2(1:tot_n_overshields)  )
    ALLOCATE( overshield_h(1:tot_n_overshields)  )
    ALLOCATE( overshield_reference_terminal_conductor(1:tot_n_overshields) )
    
    overshield=0
    conductor_count=0
    do cable=1,tot_n_cables
      if (bundle%cable(cable)%cable_type.EQ.cable_geometry_type_overshield) then 
        overshield=overshield+1
        overshield_shape(overshield)=circle
        overshield_x(overshield)=bundle%cable_x_offset(cable)
        overshield_y(overshield)=bundle%cable_y_offset(cable)
        overshield_r(overshield)=bundle%cable(cable)%parameters(1)  ! overshield radius
        overshield_w(overshield)=0d0
        overshield_w2(overshield)=0d0
        overshield_h(overshield)=0d0
        overshield_reference_terminal_conductor(overshield)=conductor_count+1
      end if
      conductor_count=conductor_count+bundle%cable(cable)%tot_n_conductors
    end do
    
    ALLOCATE( overshield_domain(1:tot_n_overshields)  )
    overshield_domain(1:tot_n_overshields)=0
    ALLOCATE( overshield_n_conductors(1:tot_n_overshields)  )
    overshield_n_conductors(1:tot_n_overshields)=0
    ALLOCATE( overshield_terminal_conductor(1:tot_n_overshields,1:tot_n_conductors)  ) ! over the top memory allocation here but simpler this way
    overshield_terminal_conductor(1:tot_n_overshields,1:tot_n_conductors)=0
    
  end if  ! n_overshields.GT.0

! allocate array for domain reference conductors assuming worst case that all overshields and external domain are viable  
  ALLOCATE( global_domain_reference_conductor(1:tot_n_domains+tot_n_overshields+1) )
  global_domain_reference_conductor(1:tot_n_domains+tot_n_overshields+1)=0
  
! 3c. Loop over cables and see whether it sits within an overshield or not  
  
  ALLOCATE( cable_reference_domain(1:bundle%n_cables)  )
  cable_reference_domain(1:bundle%n_cables)=0
  ALLOCATE( cable_reference_conductor(1:bundle%n_cables)  )
  cable_reference_conductor(1:bundle%n_cables)=0
  n_overshield_domains=0     ! counter for overshield domains
  
  do cable=1,tot_n_cables
  
    if (bundle%cable(cable)%cable_type.NE.cable_geometry_type_overshield) then 
! if this cable is not an overshield then see whether it is WITHIN an overshield

      cable_x=bundle%cable_x_offset(cable)
      cable_y=bundle%cable_y_offset(cable)
      
      do overshield=1,tot_n_overshields
      
        dist_cable_to_overshield_centre=sqrt( (cable_x-overshield_x(overshield))**2+  &
                                              (cable_y-overshield_y(overshield))**2  )
        if ( dist_cable_to_overshield_centre.LT.overshield_r(overshield) ) then
! we conclude that this cable is with the overshield

          if (overshield_domain(overshield).EQ.0) then 
          
! this overshield domain is unallocated so create a new domain
  
            tot_n_domains=tot_n_domains+1
            n_overshield_domains=n_overshield_domains+1
            if(verbose) write(*,*)'Creating overshield domain=',tot_n_domains
            overshield_domain(overshield)=tot_n_domains
            
            global_domain_reference_conductor(tot_n_domains)=overshield_reference_terminal_conductor(overshield)
0cd31073   Chris Smartt   Fix numbering err...
360
                        
886c558b   Steve Greedy   SACAMOS Public Re...
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
          end if ! this overshield domain is unallocated
          
          cable_reference_domain(cable)=overshield_domain(overshield)
          
        end if   ! this cable is with the overshield
        
      end do   ! next overshield to check
      
      terminal_conductor=terminal_conductor+bundle%cable(cable)%tot_n_conductors ! update the terminal conductor count
      
    end if ! not an overshield
    
  end do ! next cable

  if (verbose) write(*,*) 'n_overshield_domains=',n_overshield_domains

! 3c part 2. loop over the overshield domains and count the number of external conductors in each one  

  do overshield=1,tot_n_overshields
 
    terminal_conductor=0   ! count the terminal conductor number
    conductor_count=0      ! count the number of condcutors referenced to this overshield
    
    do cable=1,tot_n_cables_without_ground_plane
      
      if (cable_reference_domain(cable).EQ.overshield_domain(overshield)) then
! this cable contributes its external conductors to the overshield

        first_external_conductor=terminal_conductor+bundle%cable(cable)%tot_n_conductors-bundle%cable(cable)%n_external_conductors+1
        
        do conductor=1,bundle%cable(cable)%n_external_conductors
          conductor_count=conductor_count+1
          overshield_terminal_conductor(overshield,conductor_count)=first_external_conductor+(conductor-1)
        end do
        
      end if
      
      terminal_conductor=terminal_conductor+bundle%cable(cable)%tot_n_conductors ! update the terminal conductor count

    end do ! next cable  
    
    overshield_n_conductors(overshield)=conductor_count
    
  end do ! next overshield
  
  if (verbose) then
    write(*,*)
    write(*,*)'Overshield conductor information'
    do overshield=1,tot_n_overshields
      write(*,*)'Overshield:',overshield,' Number of condcutors=',overshield_n_conductors(overshield)
      write(*,*)' Overshield_conductor    terminal_conductor '
      do conductor=1,overshield_n_conductors(overshield)
        write(*,*)conductor,overshield_terminal_conductor(overshield,conductor)
      end do
    end do ! next overshield
    write(*,*)
  end if
  
! 3d. We now have unallocated reference domains, these are not in overshields so must be in the external domain

! 4. Count the number of conductors in the external domain to check whether it is viable i.e. at least 2 conductors

  bundle%tot_n_external_conductors=0
  do cable=1,tot_n_cables    
    if (cable_reference_domain(cable).EQ.0) then    
! this cable reference domain must be the external domain
      bundle%tot_n_external_conductors=bundle%tot_n_external_conductors+bundle%cable(cable)%n_external_conductors     
    end if ! reference domain not yet set    
  end do ! next cable
  
  if(verbose) write(*,*)'Number of conductors in external domain=',bundle%tot_n_external_conductors

! 4b.     Create an external domain and allocate unallocated reference domains now...
  ALLOCATE( external_terminal_conductor(1:bundle%tot_n_external_conductors) )
  external_terminal_conductor(1:bundle%tot_n_external_conductors)=0
  
  tot_n_viable_domains=tot_n_domains
  tot_n_domains=tot_n_domains+1
  
  if (bundle%tot_n_external_conductors.GT.1) then
    tot_n_viable_domains=tot_n_viable_domains+1
  end if
  
  if(verbose) write(*,*)'Creating external domain=',tot_n_domains
    
  terminal_conductor=0
  external_conductor_count=0
    
  do cable=1,tot_n_cables
    
    is_external_domain=.FALSE.
    if (cable_reference_domain(cable).EQ.0) then ! this cable reference domain must be the external domain
      cable_reference_domain(cable)=tot_n_domains
      is_external_domain=.TRUE.
    end if
      
    do local_conductor=1,bundle%cable(cable)%tot_n_conductors
    
      terminal_conductor=terminal_conductor+1
        
      if ( (is_external_domain).AND.(bundle%cable(cable)%local_reference_conductor(local_conductor).EQ.0) ) then        
        external_conductor_count=external_conductor_count+1
        external_terminal_conductor(external_conductor_count)=terminal_conductor      
      end if
        
    end do
        
    if (is_external_domain) then ! this cable reference domain must be the external domain
      global_domain_reference_conductor(tot_n_domains)=terminal_conductor
    end if
      
  end do ! next cable
    
! check the conductor count
  if (external_conductor_count.NE.bundle%tot_n_external_conductors) then
    run_status='ERROR in create_global_domain_structure: external_conductor_count.NE.bundle%tot_n_external_conductors'
    CALL write_program_status()
    STOP 1
  end if
  
  if(verbose) then  
    write(*,*)'External domain terminal conductors:'
    do conductor=1,bundle%tot_n_external_conductors
      write(*,*)'conductor=',external_terminal_conductor(conductor)
    end do ! next cable
  end if
  
  bundle%tot_n_domains=tot_n_viable_domains
  
  if(verbose) then  
    write(*,*)'Total number of domains=',tot_n_domains
    write(*,*)'Total number of viable domains=',tot_n_viable_domains
    write(*,*)'Cable reference domain numbers:'
    do cable=1,tot_n_cables  
      write(*,*)'cable=',cable,' reference domain=',cable_reference_domain(cable) 
    end do ! next cable
  end if
  
  ALLOCATE( domain_is_TP_differential_mode(1:tot_n_domains) )
  domain_is_TP_differential_mode(1:tot_n_domains)=.FALSE.

! 5. build the global_domain numbering, ignoring any unused domains

  conductor=0       ! this is the external conductor number
  domain_count=0
  conductor_count=0 ! this is the conductor count for the domain based numbering
  
! 5a. Count conductors from internal domains initially    
  do cable=1,tot_n_cables
  
    local_cable_conductor=0
    local_n_domains=bundle%cable(cable)%tot_n_domains
        
! 17/5/2017 CJS Add a check here to flag twisted pair differential mode domains
! The check is hrd wired based on cable types
 
    if (bundle%cable(cable)%cable_type.EQ.cable_geometry_type_twisted_pair) then 
    
      domain_is_TP_differential_mode(domain_count+1)=.TRUE.
      
    else if (bundle%cable(cable)%cable_type.EQ.cable_geometry_type_shielded_twisted_pair) then 
    
      domain_is_TP_differential_mode(domain_count+1)=.TRUE.

    else if (bundle%cable(cable)%cable_type.EQ.cable_geometry_type_spacewire) then 
    
      domain_is_TP_differential_mode(domain_count+1)=.TRUE.
      domain_is_TP_differential_mode(domain_count+3)=.TRUE.
      domain_is_TP_differential_mode(domain_count+5)=.TRUE.
      domain_is_TP_differential_mode(domain_count+7)=.TRUE.
    
    end if

! exclude the external domain  
    do local_domain=1,local_n_domains
    
      if (local_domain.NE.local_n_domains) then

! This is an internal domain so add to the domain count
        domain_count=domain_count+1
        domain=domain_count
      
        local_n_conductors=bundle%cable(cable)%local_domain_n_conductors(local_domain)-1 ! subtract 1 as the reference conductor is included
      
        do local_conductor=1,local_n_conductors
    
          conductor=conductor+1
          local_cable_conductor=local_cable_conductor+1
          conductor_count=conductor_count+1
          terminal_conductor_to_global_domain(conductor)=domain
          terminal_conductor_to_cable_local_domain(conductor)=local_conductor
          terminal_conductor_to_global_domain_conductor(conductor)=conductor_count                        
          bundle%cable(cable)%global_domain_conductor(local_cable_conductor)=conductor_count
          bundle%cable(cable)%terminal_conductor(local_cable_conductor)=conductor
       
        end do
        
      else
! This is an external domain so leave until later but count the global conductors.

        local_n_conductors=bundle%cable(cable)%local_domain_n_conductors(local_domain)-1 ! subtract 1 as the reference conductor is included     
        do local_conductor=1,local_n_conductors    
          conductor=conductor+1        
          local_cable_conductor=local_cable_conductor+1
        end do
      
      end if ! internal or external domain
      
    end do ! next conductor in this cable
    
  end do ! next cable

! 5b. Include the 'external' domain conductors now  
  do domain=first_external_domain,tot_n_domains
  
    conductor=0       ! this is the external conductor number
    do cable=1,tot_n_cables
  
      local_cable_conductor=0
      local_n_domains=bundle%cable(cable)%tot_n_domains

      do local_domain=1,local_n_domains
    
        if (local_domain.NE.local_n_domains) then

! This is an internal domain so this is already included, only count the conductors
      
          local_n_conductors=bundle%cable(cable)%local_domain_n_conductors(local_domain)-1 ! subtract 1 as the reference conductor is included
      
          do local_conductor=1,local_n_conductors
    
            conductor=conductor+1
            local_cable_conductor=local_cable_conductor+1
        
          end do
        
        else
! This is an external domain so add the conductors if it is the currrent external domain

          local_n_conductors=bundle%cable(cable)%local_domain_n_conductors(local_domain)-1 ! subtract 1 as the reference conductor is included     
          do local_conductor=1,local_n_conductors    
            conductor=conductor+1       
            local_cable_conductor=local_cable_conductor+1
            
            if (cable_reference_domain(cable).EQ.domain) then
            
              conductor_count=conductor_count+1       
              terminal_conductor_to_global_domain(conductor)=domain
              terminal_conductor_to_cable_local_domain(conductor)=local_conductor
              terminal_conductor_to_global_domain_conductor(conductor)=conductor_count                       
              bundle%cable(cable)%global_domain_conductor(local_cable_conductor)=conductor_count
              bundle%cable(cable)%terminal_conductor(local_cable_conductor)=conductor
              
            end if ! this external domain is the current one to be included
             
          end do ! next local conductor
      
        end if ! internal or external domain
        
      end do ! next local domain
           
    end do ! next cable
    
  end do ! next external domain
  
! 5c. Set the  global_domain_conductor_to_terminal_conductor from terminal_conductor_to_global_domain_conductor
  
  if (verbose) then
0cd31073   Chris Smartt   Fix numbering err...
629
  
886c558b   Steve Greedy   SACAMOS Public Re...
630
    write(*,*)'conductor,terminal_conductor_to_global_domain_conductor(conductor)'
0cd31073   Chris Smartt   Fix numbering err...
631
    
886c558b   Steve Greedy   SACAMOS Public Re...
632
633
634
    do conductor=1,tot_n_conductors
      write(*,*)conductor,terminal_conductor_to_global_domain_conductor(conductor)
    end do 
0cd31073   Chris Smartt   Fix numbering err...
635
        
886c558b   Steve Greedy   SACAMOS Public Re...
636
637
638
639
640
641
642
  end if 
  
! loop over terminal conductors
  do conductor=1,tot_n_conductors
    global_domain_conductor_to_terminal_conductor(terminal_conductor_to_global_domain_conductor(conductor))=conductor
  end do 
  
0cd31073   Chris Smartt   Fix numbering err...
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
  if (verbose) then
  
    write(*,*)'conductor,global_domain_conductor_to_terminal_conductor(conductor)'
    
    do conductor=1,tot_n_conductors
      write(*,*)conductor,global_domain_conductor_to_terminal_conductor(conductor)
    end do 
        
  end if 

! 22/10/2019 CJS Fix bug with numbering here: overshields were numbered using terminal conductor number
!                not global domain conductor as it should be at this stage
! 5d. Renumber the reference conductors for overshield domains. At the moment these are terminal conductors
!     not global domain conductors

  do overshield=1,tot_n_overshields
  
    domain=overshield_domain(overshield)

    reference_conductor=global_domain_reference_conductor(domain)
      
    global_reference_conductor=terminal_conductor_to_global_domain_conductor(reference_conductor)
    global_domain_reference_conductor(domain)=global_reference_conductor

  end do ! next overshield 
   
! 5e. Set the global_domain_reference_conductor information, also flag all the reference conductors in internal domains
886c558b   Steve Greedy   SACAMOS Public Re...
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
  
  conductor=0       ! this is the external conductor number
  
  write(*,*)
  write(*,*)
  write(*,*)'    terminal     cable      local    local_domain   global      cable     local_ref   ref terminal   ref global     '
  write(*,*)'    conductor              domain     conductor     domain     conductor  conductor    conductor    domain conductor'
  
  do cable=1,tot_n_cables
  
    local_n_domains=bundle%cable(cable)%tot_n_domains
    
    local_cable_conductor=0

    do local_domain=1,local_n_domains
          
      local_n_conductors=bundle%cable(cable)%local_domain_n_conductors(local_domain)-1 ! subtract 1 as the reference conductor is included
                  
      do local_domain_conductor=1,local_n_conductors
      
        conductor=conductor+1
        local_cable_conductor=local_cable_conductor+1
        
        reference_conductor=bundle%cable(cable)%local_reference_conductor(local_cable_conductor)
        
        if (reference_conductor.NE.0) then
! the reference conductor is defined locally        
          terminal_conductor_to_reference_global_domain_conductor(conductor)=   &
                      bundle%cable(cable)%global_domain_conductor(reference_conductor)

! 27/4/2016 CJS                      
          domain=terminal_conductor_to_global_domain(conductor)
! 16/5/2016 this should be the global not the local number...          global_domain_reference_conductor(domain)=reference_conductor
          global_domain_reference_conductor(domain)=bundle%cable(cable)%global_domain_conductor(reference_conductor)
0cd31073   Chris Smartt   Fix numbering err...
704
                               
886c558b   Steve Greedy   SACAMOS Public Re...
705
        else
0cd31073   Chris Smartt   Fix numbering err...
706
707
708
        
! the reference conductor is defined in terms of the reference domain of the cable         
   
886c558b   Steve Greedy   SACAMOS Public Re...
709
          domain=cable_reference_domain(cable) 
0cd31073   Chris Smartt   Fix numbering err...
710
711
712
713
714
          
          global_reference_conductor=global_domain_reference_conductor(domain)     
          
          reference_conductor=global_domain_conductor_to_terminal_conductor(global_reference_conductor)
          
886c558b   Steve Greedy   SACAMOS Public Re...
715
716
          terminal_conductor_to_reference_global_domain_conductor(conductor)=  &
                    terminal_conductor_to_global_domain_conductor(reference_conductor)
0cd31073   Chris Smartt   Fix numbering err...
717
                    
886c558b   Steve Greedy   SACAMOS Public Re...
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
          reference_conductor=0
                    
        end  if
        
        local_conductor=terminal_conductor_to_reference_global_domain_conductor(conductor)
        terminal_conductor_to_reference_terminal_conductor(conductor)=  &
          global_domain_conductor_to_terminal_conductor(local_conductor)
          
        terminal_conductor_is_reference_conductor(terminal_conductor_to_reference_terminal_conductor(conductor))=.TRUE.
        
        write(*,8000)bundle%cable(cable)%terminal_conductor(local_cable_conductor),&
                     cable,local_domain,local_domain_conductor, &
                     terminal_conductor_to_global_domain(conductor),    &
                     local_cable_conductor,reference_conductor, &
                     terminal_conductor_to_reference_terminal_conductor(conductor), &
                     terminal_conductor_to_reference_global_domain_conductor(conductor)
8000 format(5I11,4I13)
                            
      end do ! next local domain conductor
        
    end do ! next local domain
           
  end do ! next cable
   
! 6.  Now that we have counted the number of domains we can allocate the 
!     domain based number of conductors and L and C matrices plus
!     the global MI and MV matrices 

! Note: this allocation includes non-viable domains
  ALLOCATE( bundle%n_conductors(1:tot_n_domains) ) 
  bundle%n_conductors(1:tot_n_domains)=0
  
  ALLOCATE( bundle%L(1:tot_n_domains) ) 
  ALLOCATE( bundle%C(1:tot_n_domains) )
  ALLOCATE( bundle%Z(1:tot_n_domains) ) 
  ALLOCATE( bundle%Y(1:tot_n_domains) )

! 6a. Count the number of conductors in each domain
  if (verbose) write(*,*)'terminal_conductor   domain   local_domain_conductor'
  do conductor=1,tot_n_conductors 
    domain=terminal_conductor_to_global_domain(conductor)
    bundle%n_conductors(domain)=bundle%n_conductors(domain)+1

! 4/10/2016 Save the local domain coonductor numbering
    bundle%terminal_conductor_to_local_domain_conductor(conductor)=bundle%n_conductors(domain)
    
    if (verbose) write(*,*)conductor,domain,bundle%terminal_conductor_to_local_domain_conductor(conductor)

  end do ! next conductor
  
! Note there may be unviable overshield domains which need to be excluded in the following loop...  
  
! internal  domains and overshield domains don't include the reference conductor so add this here
  do domain=1,tot_n_internal_domains+n_overshield_domains
    bundle%n_conductors(domain)=bundle%n_conductors(domain)+1
  end do
  
  if (verbose) then
    write(*,*)
    write(*,*)'Number of conductors in each domain'
    write(*,*)'       domain     n_conductors'
    do domain=1,tot_n_domains
      write(*,*)domain,bundle%n_conductors(domain)
    end do
  end if
  
! allocate the global voltage and current domain transformation matrices
! strictly the dimension should be bundle%tot_n_conductors-1
! this would require many checks later on to prevent array bounds problems with no ground plane specified
! so we allocate the extra space then ignore the last row and col when doing the domain decomposition

  dim=bundle%tot_n_conductors
  
  if(verbose) then
    write(*,*)'ALLOCATING global_MI and global_MV, dimension:',dim
  end if

  bundle%global_MI%dim=dim
  ALLOCATE( bundle%global_MI%mat(1:dim,1:dim) ) 
  bundle%global_MI%mat(1:dim,1:dim)=0d0

  bundle%global_MV%dim=dim
  ALLOCATE( bundle%global_MV%mat(1:dim,1:dim) ) 
  bundle%global_MV%mat(1:dim,1:dim)=0d0

! Allocate the conductor based impedance (loss) model and reset all the parameters
  ALLOCATE( bundle%conductor_impedance(1:dim) )
  do conductor=1,bundle%tot_n_conductors
    bundle%conductor_impedance(conductor)%radius=0d0
    bundle%conductor_impedance(conductor)%width=0d0
    bundle%conductor_impedance(conductor)%height=0d0
    bundle%conductor_impedance(conductor)%conductivity=0d0
    bundle%conductor_impedance(conductor)%thickness=0d0
    bundle%conductor_impedance(conductor)%Resistance_multiplication_factor=1d0
  end do
  
! allocate the global inductance and capacitance matrices - these are used in the analytic solution
! which is used for validation

  dim=bundle%tot_n_conductors-1 
  
  if(verbose) then
    write(*,*)'ALLOCATING global_L and global_C, dimension:',dim
  end if

  bundle%global_L%dim=dim
  ALLOCATE( bundle%global_L%mat(1:dim,1:dim) ) 
  bundle%global_L%mat(1:dim,1:dim)=0d0

  bundle%global_C%dim=dim
  ALLOCATE( bundle%global_C%mat(1:dim,1:dim) ) 
  bundle%global_C%mat(1:dim,1:dim)=0d0

  if(verbose) then
    write(*,*)'ALLOCATING global_Z and global_Y, dimension:',dim
  end if
  bundle%global_Z%dim=dim
  ALLOCATE( bundle%global_Z%sfilter_mat(1:dim,1:dim) ) 
  do row=1,dim
    do col=1,dim
      bundle%global_Z%sfilter_mat(row,col)=0d0         ! set all filters to zero filter for now
    end do
  end do
  
  bundle%global_Y%dim=dim
  ALLOCATE( bundle%global_Y%sfilter_mat(1:dim,1:dim) ) 
  do row=1,dim
    do col=1,dim
      bundle%global_Y%sfilter_mat(row,col)=0d0        ! set all filters to zero filter for now
    end do
  end do
  
! Allocate the domain based terminal conductor list  CJS 27/4/2016
  ALLOCATE( bundle%terminal_conductor_list(1:bundle%tot_n_domains))
  
! 6b. Copy the cable based internal domain L and C matrices to bundle structure
!     and construct the global MI and MV matrices from the cable based structures 
 
  terminal_conductor=0       ! this is the external conductor number
  domain_count=0
  
  do cable=1,tot_n_cables_without_ground_plane
  
    local_n_domains=bundle%cable(cable)%tot_n_domains
    
    local_cable_conductor=0
    n_cable_conductors=bundle%cable(cable)%tot_n_conductors
        
! copy the cable based MI and MV matrices to the global structure. The cable based
! MI and MV matrices are on the basis of terminal_conductor numbering
    if (verbose) then
      write(*,*)'Copy MI, MV, cable=',cable,' of', tot_n_cables_without_ground_plane
      write(*,*)'Matrix dimension',n_cable_conductors+1
      write(*,*)'intial terminal_conductor count=',terminal_conductor
    end if
    
    do row_l=1,n_cable_conductors+1 ! add 1 as the reference conductor must be included
                
      do col_l=1,n_cable_conductors+1 ! add 1 as the reference conductor must be included

! In the global MI, MV structure rows correspond to the global domain conductor number, cols correspond to the terminal conductor number 
        
        if (row_l.NE.n_cable_conductors+1) then
          row_g=terminal_conductor_to_global_domain_conductor(terminal_conductor+row_l)
        else
          row_g=terminal_conductor_to_reference_global_domain_conductor(terminal_conductor+row_l-1) ! last conductor for this cable and the reference    
        end if
      
        if (col_l.NE.n_cable_conductors+1) then
          col_g=terminal_conductor+col_l
        else
          col_g=terminal_conductor_to_reference_terminal_conductor(terminal_conductor+col_l-1) ! last conductor for this cable and the reference
        end if
        
        bundle%global_MI%mat(row_g,col_g)=bundle%cable(cable)%MI%mat(row_l,col_l)
        bundle%global_MV%mat(row_g,col_g)=bundle%cable(cable)%MV%mat(row_l,col_l)
        
        if (verbose) write(*,*)'Copy MV element',row_g,col_g,bundle%global_MV%mat(row_g,col_g)

      end do ! next col
      
    end do ! next row
    
    do local_domain=1,local_n_domains  ! exclude the external domain for now
    
      if (local_domain.NE.local_n_domains) then

! This is an internal domain so add to the domain count
        
        domain_count=domain_count+1 
        domain=domain_count
        
! 6c copy the domain based L and C matrices to the domain based L and C in the bundle structure                       #

        if (verbose) write(*,*)'Copy the domain based L and C matrices to the domain based L and C in the bundle structure'

        if (verbose) write(*,*)'domain=',domain   
 
        dim=bundle%cable(cable)%L_domain(local_domain)%dim
        ALLOCATE( bundle%L(domain)%mat(dim,dim) ) 
        bundle%L(domain)%dim=dim
        if (verbose) write(*,*)'Allocating bundle%L(domain)        ,dim=',dim
        
        dim=bundle%cable(cable)%C_domain(local_domain)%dim
        ALLOCATE( bundle%C(domain)%mat(dim,dim) ) 
        bundle%C(domain)%dim=dim
        if (verbose) write(*,*)'Allocating bundle%C(domain)        ,dim=',dim
 
        dim=bundle%cable(cable)%Z_domain(local_domain)%dim
        ALLOCATE( bundle%Z(domain)%sfilter_mat(dim,dim) ) 
        bundle%Z(domain)%dim=dim
        if (verbose) write(*,*)'Allocating bundle%Z(domain)        ,dim=',dim
        
        dim=bundle%cable(cable)%Y_domain(local_domain)%dim
        ALLOCATE( bundle%Y(domain)%sfilter_mat(dim,dim) ) 
        bundle%Y(domain)%dim=dim
        if (verbose) write(*,*)'Allocating bundle%Y(domain)        ,dim=',dim
      
        local_n_conductors=bundle%cable(cable)%local_domain_n_conductors(local_domain)
      
        do row_l=1,local_n_conductors-1 ! subtract 1 as the reference conductor is included
                    
          do col_l=1,local_n_conductors-1 ! subtract 1 as the reference conductor is included
          
            bundle%L(domain)%mat(row_l,col_l)=bundle%cable(cable)%L_domain(local_domain)%mat(row_l,col_l)      
            bundle%C(domain)%mat(row_l,col_l)=bundle%cable(cable)%C_domain(local_domain)%mat(row_l,col_l)
            
            bundle%Z(domain)%sfilter_mat(row_l,col_l)=bundle%cable(cable)%Z_domain(local_domain)%sfilter_mat(row_l,col_l)      
            bundle%Y(domain)%sfilter_mat(row_l,col_l)=bundle%cable(cable)%Y_domain(local_domain)%sfilter_mat(row_l,col_l)

! copy the domain based L and C matrices to the global L and C in the bundle structure                        

! in the global L,C structure rows and columns correspond to the global_domain conductor number
            row_g=terminal_conductor_to_global_domain_conductor(terminal_conductor+row_l)
            col_g=terminal_conductor_to_global_domain_conductor(terminal_conductor+col_l)

            bundle%global_L%mat(row_g,col_g)=bundle%L(domain)%mat(row_l,col_l)
            bundle%global_C%mat(row_g,col_g)=bundle%C(domain)%mat(row_l,col_l)

            bundle%global_Z%sfilter_mat(row_g,col_g)=bundle%Z(domain)%sfilter_mat(row_l,col_l)
            bundle%global_Y%sfilter_mat(row_g,col_g)=bundle%Y(domain)%sfilter_mat(row_l,col_l)
         
          end do ! next column of matrix
          
        end do ! next row of matrix

! Added CJS 27/4/2016
        bundle%terminal_conductor_list(domain)%n_elements=local_n_conductors
        ALLOCATE( bundle%terminal_conductor_list(domain)%element(1:local_n_conductors) )
        
        do conductor=1,local_n_conductors-1   ! do reference conductor separately    
        
          bundle%terminal_conductor_list(domain)%element(conductor)=terminal_conductor+conductor    
            
        end do ! next conductor in this domain
        
! reference conductor        
        reference_conductor=global_domain_reference_conductor(domain)       ! note domain based numbering...
        write(*,*)'Domain=',domain
        write(*,*)'Global domain reference conductor=',reference_conductor
        bundle%terminal_conductor_list(domain)%element(local_n_conductors)=  &
                                               global_domain_conductor_to_terminal_conductor(reference_conductor)
          
        terminal_conductor=terminal_conductor+local_n_conductors-1  ! subtract 1 as the count includes the reference conductor

      else
! This is an external domain so leave until later but count the global conductors.

        local_n_conductors=bundle%cable(cable)%local_domain_n_conductors(local_domain)-1 ! subtract 1 as the reference conductor is included     
        do local_conductor=1,local_n_conductors    
          terminal_conductor=terminal_conductor+1        
        end do
      
      end if ! internal or external domain

    end do ! next local domain
    
  end do ! next cable
    
! 7. Calculate the inductance and capacitance matrices for the external domains (domains within overshields and the external domain)

  if(verbose) write(*,*)'first external domain:',first_external_domain
  if(verbose) write(*,*)'total number of domains:',tot_n_domains
  
  overshield=0    ! counter for overshield domains

  do domain=first_external_domain,tot_n_viable_domains   ! exclude the external domain if there is only one conductor
  
! allocate memory for the PUL parameter solver interface

    if(verbose) write(*,*)'Domain:',domain
    if(verbose) write(*,*)'Allocating PUL data structure for ',bundle%n_conductors(domain),' conductors'
    
    CALL allocate_and_reset_PUL_data(PUL,bundle%n_conductors(domain))
     
! copy external conductor information to the PUL structure

    conductor=0        ! this provides a count of the external domain conductor numbering for PUL parameter calculation
  
! loop over cables  
    do cable=1,bundle%n_cables
    
      if (cable_reference_domain(cable).EQ.domain) then
      
! The reference domain of this cable is the domain we are currently working on so add the external 
! conductor information for this cable to the PUL structure

        do local_conductor=1,bundle%cable(cable)%n_external_conductors
      
          if (bundle%cable(cable)%cable_type.NE.cable_geometry_type_ground_plane) then
             
            conductor=conductor+1  ! add another conductor to the external domain
         
            if(verbose) write(*,*)'Adding conductor number',local_conductor,' from cable',cable,'. Domain conductor',conductor
      
            PUL%shape(conductor)=bundle%cable(cable)%external_model(local_conductor)%conductor_type
197629dd   Chris Smartt   Update proximity_...
1034
            PUL%conductor_is_shield(conductor)=bundle%cable(cable)%external_model(local_conductor)%conductor_is_shield
886c558b   Steve Greedy   SACAMOS Public Re...
1035
1036
1037
1038
            PUL%x(conductor)=bundle%cable_x_offset(cable)    
            PUL%y(conductor)=bundle%cable_y_offset(cable)   
            PUL%rtheta(conductor)=bundle%cable_angle(cable)
            
ca8ab9e9   Chris Smartt   Update to proximi...
1039
1040
            PUL%sigma(conductor)=bundle%cable(cable)%external_model(local_conductor)%conductor_sigma
	    
44c11f06   Chris Smartt   Include software ...
1041
1042
            PUL%ox(conductor)=bundle%cable(cable)%external_model(local_conductor)%conductor_ox
            PUL%oy(conductor)=bundle%cable(cable)%external_model(local_conductor)%conductor_oy
886c558b   Steve Greedy   SACAMOS Public Re...
1043
1044
1045
1046
1047
1048
1049
            PUL%r(conductor)=bundle%cable(cable)%external_model(local_conductor)%conductor_radius
            PUL%rw(conductor)=bundle%cable(cable)%external_model(local_conductor)%conductor_width
            PUL%rw2(conductor)=bundle%cable(cable)%external_model(local_conductor)%conductor_width2
            PUL%rh(conductor)=bundle%cable(cable)%external_model(local_conductor)%conductor_height
            PUL%rd(conductor)=bundle%cable(cable)%external_model(local_conductor)%dielectric_radius
            PUL%rdw(conductor)=bundle%cable(cable)%external_model(local_conductor)%dielectric_width
            PUL%rdh(conductor)=bundle%cable(cable)%external_model(local_conductor)%dielectric_height
44c11f06   Chris Smartt   Include software ...
1050
1051
            PUL%rdox(conductor)=bundle%cable(cable)%external_model(local_conductor)%dielectric_ox
            PUL%rdoy(conductor)=bundle%cable(cable)%external_model(local_conductor)%dielectric_oy
886c558b   Steve Greedy   SACAMOS Public Re...
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
            PUL%epsr(conductor)=bundle%cable(cable)%external_model(local_conductor)%dielectric_epsr 
               
          end if ! not a ground plane    
              
        end do ! next cable external conductor
              
      end if  ! cable contributes to this domain
          
    end do ! next cable
   
! Calculate the per-unit-length parameters (inductance and capacitance matrices)
    
! if this is an overshield domain then
    if (verbose) write(*,*)'domain=',domain,' last domain(check)=',first_external_domain+tot_n_overshields
    if(domain.LT.(first_external_domain+n_overshield_domains)) then
    
      overshield=overshield+1

ca8ab9e9   Chris Smartt   Update to proximi...
1070
      if (verbose) write(*,*)'PUL parameter calculation for overshield domain'
886c558b   Steve Greedy   SACAMOS Public Re...
1071
1072
1073
1074
! no ground plane
      PUL%ground_plane_present=.FALSE.
      PUL%ground_plane_angle  =0d0
      PUL%ground_plane_offset =0d0
ca8ab9e9   Chris Smartt   Update to proximi...
1075
1076
1077
1078
1079
1080
1081
1082
      PUL%ground_plane_sigma  =0d0
      PUL%ground_plane_w      =0d0
      PUL%ground_plane_h      =0d0
      PUL%ground_plane_Rdc    =0d0
      PUL%ground_plane_nsegx  =0
      PUL%ground_plane_nsegz  =0
      PUL%ground_plane_nh     =0
           
886c558b   Steve Greedy   SACAMOS Public Re...
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
! add overshield information   
      if (verbose) write(*,*)'Add overshield information'
      is_overshield_domain=.TRUE.
      PUL%overshield_present=.TRUE.
      overshield=domain-first_external_domain+1
      PUL%overshield_shape = overshield_shape(overshield)
      PUL%overshield_x = overshield_x(overshield)
      PUL%overshield_y = overshield_y(overshield)
      PUL%overshield_r = overshield_r(overshield)
      PUL%overshield_w = overshield_w(overshield)
ca8ab9e9   Chris Smartt   Update to proximi...
1093
      PUL%overshield_w2= overshield_w2(overshield)
886c558b   Steve Greedy   SACAMOS Public Re...
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
      PUL%overshield_h = overshield_h(overshield)
      
      PUL%epsr_background = 1d0 ! background permittivity =1.0 within an overshield i.e. cables are in air
      
      if (use_Laplace) then
      
         CALL PUL_LC_Laplace(PUL,bundle%bundle_name,bundle%Y_fit_model_order,bundle%Y_fit_freq_spec,domain)
      
      else
      
        if (verbose) write(*,*)'CALL PUL_LC_calc_overshield_wide_separation_approximation'
        CALL PUL_LC_calc_overshield_wide_separation_approximation(PUL)
        
      end if ! use_Laplace or not...
ca596cf4   Chris Smartt   Start to embed Fa...
1108
1109
1110
1111
1112
1113
      
      if (use_FastHenry) then
      
         CALL PUL_RL_FastHenry2(PUL,bundle%bundle_name,bundle%Y_fit_model_order,bundle%Y_fit_freq_spec,domain)
	 
      end if
886c558b   Steve Greedy   SACAMOS Public Re...
1114
1115
1116
1117
1118
1119
1120
1121

    else
! this is an external domain    
    
! copy ground plane information
      PUL%ground_plane_present=bundle%ground_plane_present
      PUL%ground_plane_angle  =bundle%ground_plane_angle
      PUL%ground_plane_offset =bundle%ground_plane_offset
ca8ab9e9   Chris Smartt   Update to proximi...
1122
1123
1124
1125
1126
1127
1128
1129
      PUL%ground_plane_sigma  =bundle%ground_plane_sigma   ! ground plane conductivity
      PUL%ground_plane_w      =bundle%ground_plane_w       ! ground plane width
      PUL%ground_plane_h      =bundle%ground_plane_h       ! ground plane height
      PUL%ground_plane_Rdc    =bundle%ground_plane_Rdc     ! ground plane dc resistance
      PUL%ground_plane_nsegx  =bundle%ground_plane_nsegx      ! ground plane x discretisation
      PUL%ground_plane_nsegz  =bundle%ground_plane_nsegz      ! ground plane z discretisation
      PUL%ground_plane_nh     =bundle%ground_plane_nh      ! ground plane h discretisation

886c558b   Steve Greedy   SACAMOS Public Re...
1130
1131
1132
1133
1134
1135
! No overshield information   
      is_overshield_domain=.FALSE.
      PUL%overshield_present=.FALSE.
      PUL%overshield_shape=0
      PUL%overshield_r= 0d0
      PUL%overshield_w= 0d0
ca8ab9e9   Chris Smartt   Update to proximi...
1136
      PUL%overshield_w2=0d0
886c558b   Steve Greedy   SACAMOS Public Re...
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
      PUL%overshield_h= 0d0
      
      PUL%epsr_background = 1d0 ! background permittivity =1.0 i.e. cables are in air

      if (use_Laplace) then
      
         CALL PUL_LC_Laplace(PUL,bundle%bundle_name,bundle%Y_fit_model_order,bundle%Y_fit_freq_spec,domain)
      
      else

        CALL PUL_LC_calc_wide_separation_approximation(PUL)

      end if ! use_Laplace or not...
ca596cf4   Chris Smartt   Start to embed Fa...
1150
1151
1152
1153
      
      if (use_FastHenry) then
      
         CALL PUL_RL_FastHenry2(PUL,bundle%bundle_name,bundle%Y_fit_model_order,bundle%Y_fit_freq_spec,domain)
ca8ab9e9   Chris Smartt   Update to proximi...
1154

197629dd   Chris Smartt   Update proximity_...
1155
1156
1157
1158
1159
!! Should we change the conductor model type as we now let FastHenry2 deal with this?
! NO: both models are included at this point, it is only at the point of building the 
! Spice cable bundle model that we can request to include frequency dependent parameters and
! specifically the FastHenry2 frequency dependent proximity effect model as opposed to the
! conductor based internal impedeance model. 	 
ca8ab9e9   Chris Smartt   Update to proximi...
1160
1161
!	 bundle%cable(cable)%conductor_impedance(local_conductor)%impedance_model_type=impedance_model_type_FH2
!	 bundle%cable(cable)%conductor_impedance(local_conductor)%Rdc,
ca596cf4   Chris Smartt   Start to embed Fa...
1162
1163
	 
      end if
886c558b   Steve Greedy   SACAMOS Public Re...
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305

    end if  ! last external domain (i.e. not an overshield domain)

! We now have the L and C matrices for this external domain        
! Copy the domain based L and C matrices to the domain based L and C in the bundle structure        
                
    if (verbose) write(*,*)'domain=',domain
    
    dim=PUL%L%dim
    if (verbose)  write(*,*)'Allocating bundle%L(domain)        ,dim=',dim
    ALLOCATE( bundle%L(domain)%mat(dim,dim) ) 
    bundle%L(domain)%dim=dim
    
    dim=PUL%C%dim
    if (verbose) write(*,*)'Allocating bundle%C(domain)        ,dim=',dim
    ALLOCATE( bundle%C(domain)%mat(dim,dim) ) 
    bundle%C(domain)%dim=dim
    
    dim=PUL%Zfilter%dim
    if (verbose) write(*,*)'Allocating bundle%Z(domain)        ,dim=',dim
    Bundle%Z(domain)%dim=dim
    ALLOCATE(Bundle%Z(domain)%sfilter_mat(dim,dim))
    
    dim=PUL%Yfilter%dim
    if (verbose) write(*,*)'Allocating bundle%Y(domain)        ,dim=',dim
    Bundle%Y(domain)%dim=dim
    ALLOCATE(Bundle%Y(domain)%sfilter_mat(dim,dim))
     
    local_n_conductors=bundle%n_conductors(domain) 
    
    if (is_overshield_domain) then
    
      if (verbose) write(*,*)'Copying L, C, Z, Y matrices in overshield domain, n_conductors=',local_n_conductors, &
                             overshield_n_conductors(overshield)+1
      
      do row_l=1,local_n_conductors-1 ! Note that the reference conductor is included in the conductor count here
               
        do col_l=1,local_n_conductors-1 ! Note that the reference conductor is included in the conductor count here
     
! In the global L,C structure rows and columns correspond to the global_domain conductor number
          row_g=terminal_conductor_to_global_domain_conductor(overshield_terminal_conductor(overshield,row_l))
          col_g=terminal_conductor_to_global_domain_conductor(overshield_terminal_conductor(overshield,col_l))
          
          bundle%L(domain)%mat(row_l,col_l)=PUL%L%mat(row_l,col_l)      
          bundle%C(domain)%mat(row_l,col_l)=PUL%C%mat(row_l,col_l)
          
          bundle%Z(domain)%sfilter_mat(row_l,col_l)=PUL%Zfilter%sfilter_mat(row_l,col_l)
          bundle%Y(domain)%sfilter_mat(row_l,col_l)=PUL%Yfilter%sfilter_mat(row_l,col_l)
          
! copy the domain based L and C matrices to the global L and C in the bundle structure                        
          bundle%global_L%mat(row_g,col_g)=bundle%L(domain)%mat(row_l,col_l)
          bundle%global_C%mat(row_g,col_g)=bundle%C(domain)%mat(row_l,col_l)
          
! copy the domain based Z and Y filter matrices to the global Z and Y in the bundle structure                        
          bundle%global_Z%sfilter_mat(row_g,col_g)=bundle%Z(domain)%sfilter_mat(row_l,col_l)
          bundle%global_Y%sfilter_mat(row_g,col_g)=bundle%Y(domain)%sfilter_mat(row_l,col_l)
         
        end do ! next column of matrix
          
      end do ! next row of matrix

! Added CJS 27/4/2016
      bundle%terminal_conductor_list(domain)%n_elements=local_n_conductors
      ALLOCATE( bundle%terminal_conductor_list(domain)%element(1:local_n_conductors) )
        
      do conductor=1,local_n_conductors-1   ! do reference conductor separately    
        
        bundle%terminal_conductor_list(domain)%element(conductor)=overshield_terminal_conductor(overshield,conductor)
            
      end do ! next conductor in this domain
        
! reference conductor        
      reference_conductor=global_domain_reference_conductor(domain)       ! note domain based numbering...
      bundle%terminal_conductor_list(domain)%element(local_n_conductors)=  &
                                               global_domain_conductor_to_terminal_conductor(reference_conductor)

    else
! external domain
     
      write(*,*)'External domain, n_conductors=',local_n_conductors,' dim=',local_n_conductors-1 
     
      do row_l=1,local_n_conductors-1 ! subtract 1 as the reference conductor is included
               
        do col_l=1,local_n_conductors-1 ! subtract 1 as the reference conductor is included
        
! In the global L,C structure rows and columns correspond to the bundle_domain conductor number
          row_g=terminal_conductor_to_global_domain_conductor(external_terminal_conductor(row_l))
          col_g=terminal_conductor_to_global_domain_conductor(external_terminal_conductor(col_l))
          
          if (verbose) write(*,*)'Copy L,C from PUL structure'
          bundle%L(domain)%mat(row_l,col_l)=PUL%L%mat(row_l,col_l)      
          bundle%C(domain)%mat(row_l,col_l)=PUL%C%mat(row_l,col_l)
          
          if (verbose) write(*,*)'Copy Z,Y from PUL structure'
          bundle%Z(domain)%sfilter_mat(row_l,col_l)=PUL%Zfilter%sfilter_mat(row_l,col_l)
          bundle%Y(domain)%sfilter_mat(row_l,col_l)=PUL%Yfilter%sfilter_mat(row_l,col_l)

! copy the domain based L and C matrices to the global L and C in the bundle structure                        
          if (verbose) write(*,*)'Copy L,C from bundle structure'
          bundle%global_L%mat(row_g,col_g)=bundle%L(domain)%mat(row_l,col_l)
          bundle%global_C%mat(row_g,col_g)=bundle%C(domain)%mat(row_l,col_l)
          
          if (verbose) write(*,*)'Copy Z,Y from bundle structure'
 ! copy the domain based Z and Y filter matrices to the global Z and Y in the bundle structure                        
          bundle%global_Z%sfilter_mat(row_g,col_g)=bundle%Z(domain)%sfilter_mat(row_l,col_l)
          bundle%global_Y%sfilter_mat(row_g,col_g)=bundle%Y(domain)%sfilter_mat(row_l,col_l)
         
        end do ! next column of matrix
          
      end do ! next row of matrix
      
! Added CJS 27/4/2016
      bundle%terminal_conductor_list(domain)%n_elements=local_n_conductors
      ALLOCATE( bundle%terminal_conductor_list(domain)%element(1:local_n_conductors) )
        
      do conductor=1,local_n_conductors-1   ! do reference conductor separately    
        
        bundle%terminal_conductor_list(domain)%element(conductor)=external_terminal_conductor(conductor)  
            
      end do ! next conductor in this domain
        
! reference conductor        
      reference_conductor=global_domain_reference_conductor(domain)       ! note domain based numbering...
      bundle%terminal_conductor_list(domain)%element(local_n_conductors)=  &
                                               global_domain_conductor_to_terminal_conductor(reference_conductor)
      
    end if ! external domain
    
    CALL deallocate_PUL_data(PUL)

  end do ! next external domain

! 8. Copy the cable based conductor impedance (loss) models to the bundle structure

  conductor=0  ! count of terminal conductors
  
  do cable=1,tot_n_cables
                    
      do local_conductor=1,bundle%cable(cable)%tot_n_conductors
      
        conductor=conductor+1
! copy the conductor impedance model for this cable conductor to the bundle structure        
ca8ab9e9   Chris Smartt   Update to proximi...
1306
1307

        write(*,*)'cable=',cable,' conductor=',conductor,' local_conductor=',local_conductor
886c558b   Steve Greedy   SACAMOS Public Re...
1308
1309
1310
1311
        
        bundle%conductor_impedance(conductor)%impedance_model_type=  &
                    bundle%cable(cable)%conductor_impedance(local_conductor)%impedance_model_type
        
886c558b   Steve Greedy   SACAMOS Public Re...
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
        bundle%conductor_impedance(conductor)%radius=  &
                    bundle%cable(cable)%conductor_impedance(local_conductor)%radius

        bundle%conductor_impedance(conductor)%width=  &
                    bundle%cable(cable)%conductor_impedance(local_conductor)%width

        bundle%conductor_impedance(conductor)%height=  &
                    bundle%cable(cable)%conductor_impedance(local_conductor)%height
                    
        bundle%conductor_impedance(conductor)%conductivity=  &
                    bundle%cable(cable)%conductor_impedance(local_conductor)%conductivity
                    
        bundle%conductor_impedance(conductor)%thickness=  &
                    bundle%cable(cable)%conductor_impedance(local_conductor)%thickness
              
        bundle%conductor_impedance(conductor)%Resistance_multiplication_factor=  &
                    bundle%cable(cable)%conductor_impedance(local_conductor)%Resistance_multiplication_factor
ca8ab9e9   Chris Smartt   Update to proximi...
1329
1330
1331
        
        bundle%conductor_impedance(conductor)%Rdc=  &
                    bundle%cable(cable)%conductor_impedance(local_conductor)%Rdc
886c558b   Steve Greedy   SACAMOS Public Re...
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
                    
        if((bundle%conductor_impedance(conductor)%impedance_model_type.EQ.impedance_model_type_filter).OR. &  
           (bundle%conductor_impedance(conductor)%impedance_model_type.EQ.impedance_model_type_cylindrical_shield)) then
                                
          bundle%conductor_impedance(conductor)%ZT_filter=   &
                 bundle%cable(cable)%conductor_impedance(local_conductor)%ZT_filter
                               
        end if
    
      end do ! next conductor on this cable
      
  end do ! next cable
189467e4   Steve Greedy   First Public Release
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362

! 8b. Copy the cable based conductor labels to the bundle structure

  ALLOCATE( bundle%conductor_label(1:bundle%tot_n_conductors) )
  conductor=0  ! count of terminal conductors
  
  do cable=1,tot_n_cables
                    
      do local_conductor=1,bundle%cable(cable)%tot_n_conductors
      
        conductor=conductor+1
! copy the conductor impedance model for this cable conductor to the bundle structure        
        write(conductor_string,'(I3)')conductor
        bundle%conductor_label(conductor)='Conductor number '//conductor_string//'. '//  &
                                          trim(bundle%cable(cable)%conductor_label(local_conductor))
    
      end do ! next conductor on this cable
      
  end do ! next cable
886c558b   Steve Greedy   SACAMOS Public Re...
1363
1364
1365
1366
1367
1368
1369
  
! 9 save numbering information required for the transfer impedance calculation

! first loop over shielded domains. The reference conductor for each domain is a shield (by definition) 
! apart from the shielded twisted pair differential mode exception
! so record this hence set out the inner domain number for shields

0cd31073   Chris Smartt   Fix numbering err...
1370
1371
write(*,*)'****************************************************************'

886c558b   Steve Greedy   SACAMOS Public Re...
1372
1373
1374
1375
1376
  do domain=1,bundle%tot_n_domains-1   ! note we exclude the exterior domain here
  
    reference_conductor=global_domain_reference_conductor(domain)
    terminal_conductor=global_domain_conductor_to_terminal_conductor(reference_conductor)  
    
0cd31073   Chris Smartt   Fix numbering err...
1377
1378
    write(*,*)'domain=',domain,' reference_conductor=',reference_conductor,' terminal_conductor=',terminal_conductor
    
886c558b   Steve Greedy   SACAMOS Public Re...
1379
1380
1381
1382
1383
1384
    if (.NOT.domain_is_TP_differential_mode(domain)) then
      bundle%terminal_conductor_is_shield_flag(terminal_conductor)=.TRUE.
      bundle%terminal_conductor_to_inner_domain(terminal_conductor)=domain
    end if
    
  end do ! next conductor
0cd31073   Chris Smartt   Fix numbering err...
1385
1386
  
write(*,*)'****************************************************************'
886c558b   Steve Greedy   SACAMOS Public Re...
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
 
! work out the outer domain for each conductor and the associated reference conductor
  do conductor=1,bundle%tot_n_conductors
  
    bundle%terminal_conductor_to_outer_domain(conductor)=terminal_conductor_to_global_domain(conductor)   
    bundle%terminal_conductor_to_global_domain_conductor(conductor)=terminal_conductor_to_global_domain_conductor(conductor)   
    bundle%terminal_conductor_to_reference_terminal_conductor(conductor)=   &
           terminal_conductor_to_reference_terminal_conductor(conductor)    
           
  end do ! next conductor
    
! 10. finish up

  if (allocated( overshield_shape )) DEALLOCATE( overshield_shape)
  if (allocated( overshield_x )) DEALLOCATE( overshield_x)
  if (allocated( overshield_y )) DEALLOCATE( overshield_y)
  if (allocated( overshield_r )) DEALLOCATE( overshield_r)
  if (allocated( overshield_w )) DEALLOCATE( overshield_w)
  if (allocated( overshield_w2 )) DEALLOCATE( overshield_w2)
  if (allocated( overshield_h )) DEALLOCATE( overshield_h)
  if (allocated( overshield_domain )) DEALLOCATE( overshield_domain)
  if (allocated( overshield_reference_terminal_conductor)) DEALLOCATE( overshield_reference_terminal_conductor )
  if (allocated( global_domain_reference_conductor )) DEALLOCATE( global_domain_reference_conductor )
  DEALLOCATE( cable_reference_conductor  )
  DEALLOCATE( cable_reference_domain  )
 
! Deallocate the referencing arrays
 
  DEALLOCATE( terminal_conductor_to_cable )  
  DEALLOCATE( terminal_conductor_to_cable_local_domain )  
  DEALLOCATE( terminal_conductor_to_global_domain )  
  DEALLOCATE( terminal_conductor_to_global_domain_conductor )  
  DEALLOCATE( terminal_conductor_to_reference_global_domain_conductor )  
  DEALLOCATE( terminal_conductor_to_reference_terminal_conductor )  
  DEALLOCATE( terminal_conductor_is_reference_conductor )  
  DEALLOCATE( global_domain_conductor_to_terminal_conductor )  
  DEALLOCATE( external_terminal_conductor )

  if (allocated( domain_is_TP_differential_mode )) DEALLOCATE( domain_is_TP_differential_mode )

END SUBROUTINE create_global_domain_structure