cable_checks.F90 28 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 116 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 166 167 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 360 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 629 630 631 632 633 634 635 636 637 638 639 640 641 642 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 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 704 705 706 707 708 709 710 711 712 713 714 715 716 717 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 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
!
! This file is part of SACAMOS, State of the Art CAble MOdels in 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-2017 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 gt_zero_check(p,cable_spec_error,cable_name,message)
! SUBROUTINE cylindrical_check(r,cable_spec_error,cable_name,message)
! SUBROUTINE cylindrical_with_dielectric_check(r,rd,cable_spec_error,cable_name,message)
! SUBROUTINE coax_with_dielectric_check(r,rs,rd,cable_spec_error,cable_name,message)
! SUBROUTINE twisted_pair_check(r,rd,s,cable_spec_error,cable_name,message)
! SUBROUTINE shielded_twisted_pair_check(r,rd,s,rs,rd2,cable_spec_error,cable_name,message)
! SUBROUTINE spacewire_check(r,rd,s,rs,rd2,stpr,rs2,rd3,cable_spec_error,cable_name,message)
! SUBROUTINE rectangular_check(w,h,cable_spec_error,cable_name,message)
! SUBROUTINE flex_cable_check(w,h,s,dxo,dyo,cable_spec_error,cable_name,message)
! SUBROUTINE dielectric_check(epsr,cable_spec_error,cable_name,message)
! SUBROUTINE transfer_impedance_check(Zt,cable_spec_error,cable_name,message)
! SUBROUTINE conductivity_check(sigma,cable_spec_error,cable_name,message)
! SUBROUTINE surface_impedance_check(Zt,sigma,rs,t,cable_spec_error,cable_name,message)
!
! NAME
!     gt_zero_check
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     check that a parameter,p, is greater than zero
!     If the check fails the cable_spec_error flag is set on return, otherwise it is left unchanged
!
! COMMENTS
!      
!
! HISTORY
!
!     started 14/11/2016 CJS 
!     3/8/2017 return with error message
!     16/11/2017 CJS Include network synthesis process to replace s-domain transfer functions
!
!
SUBROUTINE gt_zero_check(p,cable_spec_error,cable_name,message)

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  real(dp),intent(IN) :: p  ! input test parameter
  
  logical,intent(INOUT)  :: cable_spec_error    ! error flag
  
  character(LEN=line_length),intent(IN)              :: cable_name  
  character(LEN=error_message_length),intent(INOUT) :: message 

! local variables

! START

if (cable_spec_error) RETURN ! return if an error has already been flagged

if (p.LE.0d0) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Parameter is less than or equal to zero'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

RETURN

END SUBROUTINE gt_zero_check
!
! NAME
!     cylindrical_check
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     check that the parameters defining a cylindrical cable are consistent
!     The wire radius must be greater than zero
!     If the check fails the cable_spec_error flag is set on return, otherwise it is left unchanged
!
! COMMENTS
!      
!
! HISTORY
!
!     started 28/10/2016 CJS 
!     3/8/2017 return with error message
!
!
SUBROUTINE cylindrical_check(r,cable_spec_error,cable_name,message)

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  real(dp),intent(IN) :: r
  
  logical,intent(INOUT)  :: cable_spec_error
  
  character(LEN=line_length),intent(IN) :: cable_name  
  character(LEN=error_message_length),intent(INOUT) :: message 

! local variables

! START

if (cable_spec_error) RETURN ! return if an error has already been flagged

if (r.LE.0d0) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Radius is less than or equal to zero'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

RETURN

END SUBROUTINE cylindrical_check
!
! NAME
!     cylindrical_with_dielectric_check
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     check that the parameters defining a cylindrical cable with dielectric are consistent
!     the wire radius must be greater than zero and 
!     the dielectric radius must be greater than or equal to the conductor radius.
!     If the check fails the cable_spec_error flag is set on return, otherwise it is left unchanged
!
! COMMENTS
!      
!
! HISTORY
!
!     started 28/10/2016 CJS 
!     3/8/2017 return with error message
!
!
SUBROUTINE cylindrical_with_dielectric_check(r,rd,cable_spec_error,cable_name,message)

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  real(dp),intent(IN) :: r
  real(dp),intent(IN) :: rd
  
  logical,intent(INOUT)  :: cable_spec_error
  
  character(LEN=line_length),intent(IN) :: cable_name  
  character(LEN=error_message_length),intent(INOUT) :: message 

! local variables

! START

if (cable_spec_error) RETURN ! return if an error has already been flagged

CALL cylindrical_check(r,cable_spec_error,cable_name,message)

if (rd.LT.r) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Dielectric radius is less than the condcutor radius'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

RETURN

END SUBROUTINE cylindrical_with_dielectric_check
!
! NAME
!     coax_with_dielectric_check
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     check that the parameters defining a coax cable are consistent
!     the inner wire radius must be greater than zero and 
!     the shield radius must be greater than the conductor radius.
!     the dielectric radius must be greater than or equal to the shield radius.
!     If the check fails the cable_spec_error flag is set on return, otherwise it is left unchanged
!
! COMMENTS
!     
!
! HISTORY
!
!     started 28/10/2016 CJS 
!     3/8/2017 return with error message
!
!
SUBROUTINE coax_with_dielectric_check(r,rs,rd,cable_spec_error,cable_name,message)

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  real(dp),intent(IN) :: r
  real(dp),intent(IN) :: rs
  real(dp),intent(IN) :: rd
  
  logical,intent(INOUT)  :: cable_spec_error
  
  character(LEN=line_length),intent(IN) :: cable_name  
  character(LEN=error_message_length),intent(INOUT) :: message 

! local variables

! START

if (cable_spec_error) RETURN ! return if an error has already been flagged

CALL cylindrical_check(r,cable_spec_error,cable_name,message)

if (rs.LE.r) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Shield radius is less than or equal to the condcutor radius'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

if (rd.LT.rs) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Dielectric radius is less than the shield radius'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

RETURN

END SUBROUTINE coax_with_dielectric_check
!
! NAME
!     twisted_pair_check
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     check that the parameters defining a twisted pair cable with dielectric are consistent
!     the wire with dielectric checks must be satisfied plus
!     the two wires must not touch i.e. the twisted pair separation must be greater than twice the dielectric radius
!     If the check fails the cable_spec_error flag is set on return, otherwise it is left unchanged
!
! COMMENTS
!      
!
! HISTORY
!
!     started 28/10/2016 CJS 
!     3/8/2017 return with error message
!
!
SUBROUTINE twisted_pair_check(r,rd,s,cable_spec_error,cable_name,message)

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  real(dp),intent(IN) :: r
  real(dp),intent(IN) :: rd
  real(dp),intent(IN) :: s
  
  logical,intent(INOUT)  :: cable_spec_error
  
  character(LEN=line_length),intent(IN) :: cable_name  
  character(LEN=error_message_length),intent(INOUT) :: message 

! local variables

! START

if (cable_spec_error) RETURN ! return if an error has already been flagged

CALL cylindrical_with_dielectric_check(r,rd,cable_spec_error,cable_name,message)

if (s.LE.rd*2d0) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='twisted pair separation is less than or equal to twice the dielectric radius'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

RETURN

END SUBROUTINE twisted_pair_check
!
! NAME
!     shielded_twisted_pair_check
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     check that the parameters defining a shielded twisted pair cable with dielectric are consistent
!     the twisted pair checks must be satisfied
!     the shield must not intersect the twisted pair wires
!     the shield dielectric radius must be greater than or equal to the shield radiius
!     If the check fails the cable_spec_error flag is set on return, otherwise it is left unchanged
!
! COMMENTS
!      
!
! HISTORY
!
!     started 28/10/2016 CJS 
!     3/8/2017 return with error message
!
!
SUBROUTINE shielded_twisted_pair_check(r,rd,s,rs,rd2,cable_spec_error,cable_name,message)

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  real(dp),intent(IN) :: r
  real(dp),intent(IN) :: rd
  real(dp),intent(IN) :: s
  real(dp),intent(IN) :: rs
  real(dp),intent(IN) :: rd2
  
  logical,intent(INOUT)  :: cable_spec_error
  
  character(LEN=line_length),intent(IN) :: cable_name  
  character(LEN=error_message_length),intent(INOUT) :: message 

! local variables

! START

if (cable_spec_error) RETURN ! return if an error has already been flagged

CALL twisted_pair_check(r,rd,s,cable_spec_error,cable_name,message)

if (rs.LE.s/2d0+rd) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Shield intersects the twisted pair wires'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

if (rd2.LT.rs) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Dielectric radius is less than the shield radius'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

RETURN

END SUBROUTINE shielded_twisted_pair_check
!
! NAME
!     spacewire_check
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     check that the parameters defining spacewire are consistent
!     the shielded twisted pair checks must be satisfied
!     the four shielded twisted pairs must not intersect
!     the outer shield must not intersect the shielded twisted pairs
!     the outer dielectric radius must be greater than or equal to the outer shield radius
!     If the check fails the cable_spec_error flag is set on return, otherwise it is left unchanged
!
! COMMENTS
!      
!
! HISTORY
!
!     started 28/10/2016 CJS 
!     3/8/2017 return with error message
!
!
SUBROUTINE spacewire_check(r,rd,s,rs,rd2,stpr,rs2,rd3,cable_spec_error,cable_name,message)

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  real(dp),intent(IN) :: r
  real(dp),intent(IN) :: rd
  real(dp),intent(IN) :: s
  real(dp),intent(IN) :: rs
  real(dp),intent(IN) :: rd2
  real(dp),intent(IN) :: stpr
  real(dp),intent(IN) :: rs2
  real(dp),intent(IN) :: rd3
  
  logical,intent(INOUT)  :: cable_spec_error
  
  character(LEN=line_length),intent(IN) :: cable_name  
  character(LEN=error_message_length),intent(INOUT) :: message 

! local variables

! START

if (cable_spec_error) RETURN ! return if an error has already been flagged

CALL shielded_twisted_pair_check(r,rd,s,rs,rd2,cable_spec_error,cable_name,message)

if (stpr.LE.sqrt(2d0)*rd2) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Shielded twisted pairs intersect'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

if (rs2.LE.rd2+stpr) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Outer shield intersects the shielded twisted pairs'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

if (rd3.LT.rs2) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Outer dielectric radius is less than the outer shield radius'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

RETURN

END SUBROUTINE spacewire_check
!
! NAME
!     rectangular_check
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     check that the parameters defining rectangular conductors are consistent
!     the conductor width and height must both be greater than zero
!     If the check fails the cable_spec_error flag is set on return, otherwise it is left unchanged
!
! COMMENTS
!      
!
! HISTORY
!
!     started 28/10/2016 CJS 
!     3/8/2017 return with error message
!
!
SUBROUTINE rectangular_check(w,h,cable_spec_error,cable_name,message)

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  real(dp),intent(IN) :: w
  real(dp),intent(IN) :: h
  
  logical,intent(INOUT)  :: cable_spec_error
  
  character(LEN=line_length),intent(IN) :: cable_name  
  character(LEN=error_message_length),intent(INOUT) :: message 

! local variables

! START

if (cable_spec_error) RETURN ! return if an error has already been flagged

if (w.LE.0d0) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Conductor width is less than or equal to zero'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

if (h.LE.0d0) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Conductor height is less than or equal to zero'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

RETURN

END SUBROUTINE rectangular_check
!
! NAME
!     flex_cable_check
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     check that the parameters defining flex cable are consistent
!     the conductor width and height must both be greater than zero
!     the conductor separation must be greater than zero if there is more more than one conductor
!     the dielectric offset in x and y must be greater than or equal to zero
!     If the check fails the cable_spec_error flag is set on return, otherwise it is left unchanged
!
! COMMENTS
!      
!
! HISTORY
!
!     started 28/10/2016 CJS 
!     3/8/2017 return with error message
!
!
SUBROUTINE flex_cable_check(nc,w,h,s,dxo,dyo,cable_spec_error,cable_name,message)

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  integer,intent(IN)  :: nc
  real(dp),intent(IN) :: w
  real(dp),intent(IN) :: h
  real(dp),intent(IN) :: s
  real(dp),intent(IN) :: dxo
  real(dp),intent(IN) :: dyo
  
  logical,intent(INOUT)  :: cable_spec_error
  
  character(LEN=line_length),intent(IN) :: cable_name  
  character(LEN=error_message_length),intent(INOUT) :: message 

! local variables

! START

if (cable_spec_error) RETURN ! return if an error has already been flagged

CALL rectangular_check(w,h,cable_spec_error,cable_name,message)

if (s.LT.0d0) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='conductor separation is less than zero'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

if ((nc.GT.1).AND.(s.EQ.0d0)) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='conductor separation is zero'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

if (dxo.LT.0d0) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Dielectric offset in x is less than zero'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

if (dyo.LT.0d0) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Dielectric offset in y is less than zero'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

RETURN

END SUBROUTINE flex_cable_check
!
! NAME
!     ML_flex_cable_check
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     check that the parameters defining a multi-layer flex cable are consistent
!     This check takes the width, height and offset of two rectangles and checks whether they intersect
!     It is used for checking intersection of rows of conductors and conducotrs and dielectric in flex cable models
!     If the check fails the cable_spec_error flag is set on return, otherwise it is left unchanged
!
! COMMENTS
!      
!
! HISTORY
!
!  
!     started 12/6/2018 CJS, based on flex_cable_check
!
!
SUBROUTINE ML_flex_cable_check(w1,h1,ox1,oy1,w2,h2,ox2,oy2,check_type,cable_spec_error,cable_name,message)

  real(dp),intent(IN) :: w1,h1
  real(dp),intent(IN) :: ox1,oy1
  real(dp),intent(IN) :: w2,h2
  real(dp),intent(IN) :: ox2,oy2
  
  integer,intent(IN) :: check_type

  logical,intent(INOUT)  :: cable_spec_error
  
  character(LEN=line_length),intent(IN) :: cable_name  
  character(LEN=error_message_length),intent(INOUT) :: message 

! local variables

logical :: intersect,internal

real(dp) :: p1,p2,p3,p4
logical :: internalp3,internalp4

logical :: intersectx,internalx,externalx
logical :: intersecty,internaly,externaly

! START

if (cable_spec_error) RETURN ! return if an error has already been flagged

intersect=.FALSE.
internal=.FALSE.
intersectx=.FALSE.
internalx=.FALSE.
externalx=.FALSE.
intersecty=.FALSE.
internaly=.FALSE.
externaly=.FALSE.

p1=ox1-w1/2.0
p2=ox1+w1/2.0
p3=ox2-w2/2.0
p4=ox2+w2/2.0

internalp3=.FALSE.
internalp4=.FALSE.

if ((p3.GE.p1).AND.(p3.LE.p2)) internalp3=.TRUE.
if ((p4.GE.p1).AND.(p4.LE.p2)) internalp4=.TRUE.

if (internalp3.AND.internalp4) then
  intersectx=.TRUE.
  internalx=.TRUE.                        ! rectangle 2 is inside rectangel1
else if (internalp3.OR.internalp4) then
  intersectx=.TRUE.
  internalx=.FALSE.
else
! p3 and p4 are outside the p1-p2 range, we now nead to decide if p1-p2 is inside the range p3-p4
  if ((p3.LT.p1).AND.(p4.GT.p2)) then
    intersectx=.TRUE.
    externalx=.TRUE.                      ! rectangle 2 is outside rectangel1
  end if
end if

p1=oy1-h1/2.0
p2=oy1+h1/2.0
p3=oy2-h2/2.0
p4=oy2+h2/2.0

internalp3=.FALSE.
internalp4=.FALSE.

if ((p3.GE.p1).AND.(p3.LE.p2)) internalp3=.TRUE.
if ((p4.GE.p1).AND.(p4.LE.p2)) internalp4=.TRUE.

if (internalp3.AND.internalp4) then
  intersecty=.TRUE.
  internaly=.TRUE.                        ! rectangle 2 is inside rectangel1
else if (internalp3.OR.internalp4) then
  intersecty=.TRUE.
  internaly=.FALSE.
else
! p3 and p4 are outside the p1-p2 range, we now nead to decide if p1-p2 is inside the range p3-p4
  if ((p3.LT.p1).AND.(p4.GT.p2)) then
    intersecty=.TRUE.
    externaly=.TRUE.                      ! rectangle 2 is outside rectangel1
  end if
end if

if (intersectx.AND.intersecty) intersect=.TRUE.
if (internalx.AND.internaly) internal=.TRUE.
if (externalx.AND.externaly) internal=.TRUE. ! rectangle 2 is outside rectangel1

if ( (check_type.EQ.1).AND.(intersect) ) then
! we are checking conductor intersections
  write(*,*)'Error in cable:',trim(cable_name)
  message='conductors intersect'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN

end if

! this is the chek for dielectrc being outside the condustor row, the dielectric is shape 2 when 
! the subroutine is called
if ( (check_type.EQ.2).AND.( .NOT.(externalx.AND.externaly) ) ) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Dielectric intersects conductors'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

RETURN

END SUBROUTINE ML_flex_cable_check    
!
! NAME
!     dielectric_check
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     check that the parameters defining dielectric models are consistent
!     d.c. relative permittivity must be greater than or equal to 1
!     high frequency relative permittivity must be greater than or equal to 1
!     the poles of the rational function must be on LHS of the s-plane (time domain stability requirement)
!     If the check fails the cable_spec_error flag is set on return, otherwise it is left unchanged
!
! COMMENTS
!      
!
! HISTORY
!
!     started 28/10/2016 CJS 
!     3/8/2017 return with error message
!
!

SUBROUTINE dielectric_check(epsr,cable_spec_error,cable_name,message)

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  type(Sfilter),intent(IN) :: epsr
  
  logical,intent(INOUT)  :: cable_spec_error
  
  character(LEN=line_length),intent(IN) :: cable_name  
  character(LEN=error_message_length),intent(INOUT) :: message 

! local variables

real(dp) :: epsr_value
logical  :: stable

! START

if (cable_spec_error) RETURN ! return if an error has already been flagged

! Check that the d.c. relative permittivity is greater than or equal to 1.0

epsr_value=epsr%a%coeff(0)/epsr%b%coeff(0)

if (epsr_value.LT.1d0) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Relative permittivity is less than 1.0 at d.c.'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

! Check that the relative permittivity is greater than or equal to 1.0 at high frequency

epsr_value=evaluate_Sfilter_high_frequency_limit(epsr)

if (epsr_value.LT.1d0) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Relative permittivity is less than 1.0 at high frequency'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

! check that the poles of the filter function are in the LHS of the s plane

CALL test_filter_pole_stability(epsr,stable)
if (.NOT.stable) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Relative permittivity model has unstable pole(s)'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if

END SUBROUTINE dielectric_check
!
! NAME
!     not_FD_dielectric_check
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     check that the dielectric model is not frequency dependent as this is a limitation for certain models
!     If the check fails the cable_spec_error flag is set on return, otherwise it is left unchanged
!
! COMMENTS
!      
!
! HISTORY
!
!     started 28/10/2016 CJS 
!     3/8/2017 return with error message
!
!

SUBROUTINE not_FD_dielectric_check(epsr,cable_spec_error,cable_name,message)

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  type(Sfilter),intent(IN) :: epsr
  
  logical,intent(INOUT)  :: cable_spec_error
  
  character(LEN=line_length),intent(IN) :: cable_name  
  character(LEN=error_message_length),intent(INOUT) :: message 

! local variables

logical  :: stable

! START

if (cable_spec_error) RETURN ! return if an error has already been flagged

! Check that the d.c. relative permittivity is greater than or equal to 1.0

if ( (epsr%a%order.NE.0).OR.(epsr%b%order.NE.0) ) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Relative permittivity model is frequency dependent'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if


END SUBROUTINE not_FD_dielectric_check
!
! NAME
!     transfer impedance_check
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     check that the parameters defining transfer impedance are consistent
!     the d.c. transfer impedance must be greater than or equal to zero
!     the poles of the rational function must be on LHS of the s-plane (time domain stability requirement)     
!     If the check fails the cable_spec_error flag is set on return, otherwise it is left unchanged
!
! COMMENTS
!      
!
! HISTORY
!
!     started 28/10/2016 CJS 
!     3/8/2017 return with error message
!
!

SUBROUTINE transfer_impedance_check(Zt,cable_spec_error,cable_name,message)

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  type(Sfilter),intent(IN) :: ZT

  logical,intent(INOUT)  :: cable_spec_error
  
  character(LEN=line_length),intent(IN) :: cable_name  
  character(LEN=error_message_length),intent(INOUT) :: message 

! local variables

real(dp) :: Rdc
logical  :: stable

! START

if (cable_spec_error) RETURN ! return if an error has already been flagged

! Check that the d.c. resistance is greater than or equal to zero

Rdc=ZT%a%coeff(0)/ZT%b%coeff(0)

if (Rdc.LT.0d0) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Transfer impedance is less than zero at d.c.'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

! check that the poles of the filter function are in the LHS of the s plane

CALL test_filter_pole_stability(ZT,stable)
if (.NOT.stable) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Transfer impedance model has unstable pole(s)'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if

END SUBROUTINE transfer_impedance_check
!
! NAME
!     conductivity_check
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     check that the conductivity specified is consistent
!     the conductivity specified must be greater than or equal to zero. 
!     Note that zero conductivity indicates a perfect conductor in this software.
!     If the check fails the cable_spec_error flag is set on return, otherwise it is left unchanged
!
! COMMENTS
!      
!
! HISTORY
!
!     started 28/10/2016 CJS 
!     3/8/2017 return with error message
!
!

SUBROUTINE conductivity_check(sigma,cable_spec_error,cable_name,message)

USE type_specifications

IMPLICIT NONE

! variables passed to subroutine

  real(dp),intent(IN) :: sigma
  
  logical,intent(INOUT)  :: cable_spec_error
  
  character(LEN=line_length),intent(IN) :: cable_name  
  character(LEN=error_message_length),intent(INOUT) :: message 

! local variables

! START

if (cable_spec_error) RETURN ! return if an error has already been flagged

if (sigma.LT.0d0) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Conductivity is less than zero'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

END SUBROUTINE conductivity_check
!
! NAME
!     surface_impedance_check
!
! AUTHORS
!     Chris Smartt
!
! DESCRIPTION
!     check that the parameters defining surface impedance for cylindrical shields are consistent
!     The thickness,t , must be greater than or equal to zero on input
!     If t=0 then calculate an equivalent thickness such that the surface impedance is equal to the
!     transfer impedance at low frequency i.e. make the surface and transfer impedance models consistent
!     If t=0 and the conductivity=0 and the transfer impedance at d.c. is not zero then the shield model
!     cannot be specified and an error is flagged.
!     If the check fails the cable_spec_error flag is set on return, otherwise it is left unchanged
!
! COMMENTS
!      
!
! HISTORY
!
!     started 28/10/2016 CJS 
!     3/8/2017 return with error message
!
!

SUBROUTINE surface_impedance_check(ZT,sigma,rs,t,cable_spec_error,cable_name,message)

USE type_specifications
USE constants

IMPLICIT NONE

! variables passed to subroutine

  type(Sfilter) :: ZT

  real(dp),intent(INOUT) :: sigma
  real(dp),intent(IN) :: rs
  real(dp),intent(INOUT) :: t
  
  logical,intent(INOUT)  :: cable_spec_error
  
  character(LEN=line_length),intent(IN) :: cable_name  
  character(LEN=error_message_length),intent(INOUT) :: message 

! local variables

  real(dp) :: Rdc

! START

if (cable_spec_error) RETURN ! return if an error has already been flagged

CALL conductivity_check(sigma,cable_spec_error,cable_name,message)

if (t.LT.0d0) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Shield thickness is less than zero'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if 

! calculate Rdc, the transfer impedance at d.c.
 Rdc=ZT%a%coeff(0)/ZT%b%coeff(0)

! If t=0 then calculate an equivalent thickness such that the surface impedance is equal to the
! transfer impedance at low frequency
  
if ((t.EQ.0d0).AND.(sigma.NE.0d0)) then
! we need to calculate the thickness to be consistent with the transfer impedance at d.c. i.e. R_dc = ZT_dc
  
  if (Rdc.NE.0d0) then
! calculate the 'equivalent thickness' for the shield
    t=1d0/(2d0*pi*rs*sigma*Rdc)
  else
! The transfer impedance is zero so set the surface impedance to be zero by setting sigma=0
    t=0d0
    sigma=0d0
  end if
  
end if
  
if ((t.EQ.0d0).AND.(sigma.EQ.0d0).AND.(Rdc.GT.small)) then
  write(*,*)'Error in cable:',trim(cable_name)
  message='Shield conductivity and thickness cannot both be set to zero if there is a non-zero transfer impedance'
  write(*,'(A)')trim(message)
  cable_spec_error=.TRUE.
  RETURN
end if

END SUBROUTINE surface_impedance_check