/NOPR
/NOLIST
!   fmagbc.mac
!   Macro to apply the magnetic boundary conditions (MXWF and MVDI)
!   arg1  = first component name
!   arg2  = 2nd component name
!   arg3  = 3rd component name
!   arg4  = 4th component name
!   arg5  = 5th component name
!   arg6  = 6th component name
!   arg7  = 7th component name
!   arg8  = 8th component name
!   arg9  = 9th component name
!
! This macro generates a character array _cmpn which contains
! the names of the components for which force conditions were
! applied.
!
!           ANSYS Revision 5.3
!
*set,_elmtd
*dim,_elmtd,,3
_nodo=0
*get,_rout,active,,rout
*if,_rout,ne,17,then
  *if,_rout,ne,21,then
    _nodo=1       !
    /nerr,1,1e5
    *msg,warn
    Enter Prep7 or Solution  before issuing this command macro
  *endif
*endif

!   /nerr,0,1e5,-1
!  *if,_nodo,eq,1,:end
*if,_nodo,eq,0,then
  *get,_dimn,active,,solu,dimn        ! 1=axisym, 2=planar, 3=3d
  cm,_elm,elem
! Perform correction check on planar or 3d analysis
*if,_dimn,eq,3,then         ! if 3d detected, then
  esel,,ename,,13,53,40     ! select all possible 2-d emag field elements
  esel,a,ename,,121
  *get,_num2d,elem,,count
  *if,_num2d,gt,0,then
   _dimn=2               ! if 2-d emag element exists, switch dimensionality
  *else
   _dimn=3
  *endif
  cmsel,s,_elm
  _num2d=
*endif

  *get,_mnu,active,,menu
  _iappl=0
  *set,_cmpn
  *dim,_cmpn,char,9
  _cmpn(1)='  ','  ','  ','  ','  ','  ','  ','  ','  '

  !     start of new code   c_5/8/95
  _arg1=arg1
  *get,_cchk,parm,_arg1,type
  *if,_cchk,eq,0,then
    *if,_mnu,eq,1,then
      *msg,warn
      Component name must be input to apply force boundary conditions
    *else
      *msg,warn
      Component name must be input (and enclosed in single quotes)
    *endif
  *endif
  *if,_cchk,ge,3,then
    _cmpn(1)=arg1
  *elseif,_arg1,ne,0,then
    *msg,warn
    The Component name must consist of valid character string
  *endif

  _arg1=arg2
  *get,_cchk,parm,_arg1,type
  *if,_cchk,ge,3,then
    _cmpn(2)=arg2
  *elseif,_arg1,ne,0,then
    *msg,warn
    The Component name must consist of valid character string
  *endif

  _arg1=arg3
  *get,_cchk,parm,_arg1,type
  *if,_cchk,ge,3,then
    _cmpn(3)=arg3
  *elseif,_arg1,ne,0,then
    *msg,warn
    The Component name must consist of valid character string
  *endif

  _arg1=arg4
  *get,_cchk,parm,_arg1,type
  *if,_cchk,ge,3,then
    _cmpn(4)=arg4
  *elseif,_arg1,ne,0,then
    *msg,warn
    The Component name must consist of valid character string
  *endif

  _arg1=arg5
  *get,_cchk,parm,_arg1,type
  *if,_cchk,ge,3,then
    _cmpn(5)=arg5
  *elseif,_arg1,ne,0,then
    *msg,warn
    The Component name must consist of valid character string
  *endif

  _arg1=arg6
  *get,_cchk,parm,_arg1,type
  *if,_cchk,ge,3,then
    _cmpn(6)=arg6
  *elseif,_arg1,ne,0,then
    *msg,warn
    The Component name must consist of valid character string
  *endif

  _arg1=arg7
  *get,_cchk,parm,_arg1,type
  *if,_cchk,ge,3,then
    _cmpn(7)=arg7
  *elseif,_arg1,ne,0,then
    *msg,warn
    The Component name must consist of valid character string
  *endif

  _arg1=arg8
  *get,_cchk,parm,_arg1,type
  *if,_cchk,ge,3,then
    _cmpn(8)=arg8
  *elseif,_arg1,ne,0,then
    *msg,warn
    The Component name must consist of valid character string
  *endif

  _arg1=arg9
  *get,_cchk,parm,_arg1,type
  *if,_cchk,ge,3,then
    _cmpn(9)=arg9
  *elseif,_arg1,ne,0,then
    *msg,warn
    The Component name must consist of valid character string
  *endif

  !     end of new code   c_5/8/95

  cm,_curei,elem
  cm,_curni,node

  !*********************************************************************
  !   The following section was added to determine what type of
  !   analysis was taken place, either electrostatic or magnetic.


  esel,s,ename,,121
  esel,a,ename,,122
  esel,a,ename,,123

  *get,_NumElem,elem,,count   ! >0 elements would signify electrostatic
                              ! =0 elements would signify magnetic
  *if,_NumElem,gt,0,then
     _AType=0   ! Electrostatic
  *else
     _AType=1   ! Magnetic
  *endif

  cmsel,s,_curei

  !*********************************************************************

  !   *stat,_cmpn

  *set,_actm
  *dim,_actm,,9

  !/nerr,0,1e4,-1

  *do,_icom,1,9

    !   /nopr

    _errlv=0
    _nodo=0
    *if,_cmpn(_icom),eq,' ',then
      _nodo=1
      _errlv=_errlv+1
    *endif

    *if,_nodo,eq,0,then

      *get,_type,comp,%_cmpn(_icom)%,type

      *if,_type,eq,0,then
        !   this is NOT a valid component
        *msg,warn,_cmpn(_icom)
        Component %c is not defined.
        _nodo=1
        _errlv=_errlv+1
        _elnum=0
      *else
        !   check for elements
        cmsel,,%_cmpn(_icom)%
        *get,_elnum,elem,,count
        *if,_elnum,eq,0,then
          *msg,warn,_cmpn(_icom)
          Component %c has no elements associated with it.&
          No force boundary conditions were applied to this component
          _cmpn(_icom)='  '
        *endif
      *endif

      *if,_elnum,ne,0,then
        nsle
        esln
        cmsel,u,%_cmpn(_icom)%
        *get,_elnum1,elem,,count
        cm,_curel1,elem

        *if,_AType,eq,1,then,

          *do,_icom1,1,_elnum1
            !  Check the surrounding elements, make sure they have free
            !  space permeability (Magnetic ONLY)
            !    /com, top of the loop on elements _elnum1
            _nodo=0
            *get,_elnum2,elem,,count
            *if,_elnum2,eq,0,exit
            *get,_elmx2,elem,,num,max
            *get,_elmatc,elem,_elmx2,attr,mat
            *get,_elmtd(1),murx,_elmatc,,,const,1
            *get,_elmtd(2),mury,_elmatc,,,const,1

            *if,_dimn,eq,3,then
              *get,_elmtd(3),murz,_elmatc,,,const,1
            *endif

            !   *vscfun,_magflg,sum,_elmtd(1)
            _imatlm=2

            *if,_dimn,eq,3,then
              _imatlm=3
            *endif

            *do,_imat1,1,_imatlm
              !   /nopr
              *if,_elmtd(_imat1),ne,0,then
                *if,_elmtd(_imat1),gt,1,then
                  _nodo=2
                  _errlv=_errlv+1
                *endif
              *endif
            *enddo

            _bh11=0
            *get,_bh11,bh,_elmatc,type      !    6/3#2


            *if,_bh11,ne,0,then
              _nodo=2
              _errlv=_errlv+1
            *endif

            *if,_elmtd(1)+_elmtd(2)+_elmtd(3),eq,0,then
              *if,_bh11,eq,0,then
                _nodo=3
              *endif
            *elseif,_elmtd(2),ne,0,then
              *if,_elmtd(1),eq,0,then
                _nodo=2
                _errlv=_errlv+1
              *endif
            *endif

            *if,_dimn,eq,3,then
              *if,_elmtd(3),ne,0,then
                *if,_elmtd(1)*_elmtd(2),eq,0,then
                  _nodo=2
                  _errlv=_errlv+1
                *endif
              *endif
            *endif

            *if,_nodo,eq,2,then
              *msg,warn,_elmatc,_cmpn(_icom)
              Some of the elements (Material %i ) surrounding the part&
              do not correspond to free space. No force boundary&
              conditions applied for component %c
            *endif

            *if,_nodo,eq,3,then
              *msg,warn,_cmpn(_icom),_elmatc
              Some of the elements surrounding the part ( %c ) do not have&
              material properties (Material %i ). Be sure the material has&
              free space permeability assigned.
              _nodo=0
            *endif

            cmsel,,_curel1
            esel,u,mat,,_elmatc
            cm,_curel1,elem

          *enddo

        *else

          *do,_icom1,1,_elnum1

            !  Check the surrounding elements, make sure they have free
            !  space permittivity (Electrostatic ONLY)

            _nodo=0
            *get,_elnum2,elem,,count
            *if,_elnum2,eq,0,exit
            *get,_elmx2,elem,,num,max
            *get,_elmatc,elem,_elmx2,attr,mat
            *get,_elmtd(1),perx,_elmatc,,,const,1
            *get,_elmtd(2),pery,_elmatc,,,const,1
            *if,_dimn,eq,3,then
              *get,_elmtd(3),perz,_elmatc,,,const,1
            *endif

            *if,_elmtd(1)+_elmtd(2)+_elmtd(3),eq,0,then
              *msg,warn,_cmpn(_icom),_elmatc
              Some of the elements surrounding the part ( %c ) do not have&
              material properties (Material %i ). Be sure the material has&
              free space permittivity assigned.
              _nodo=0
            *endif

            cmsel,,_curel1
            esel,u,mat,,_elmatc
            cm,_curel1,elem

          *enddo
        *endif

_errlv=0

        *if,_errlv,eq,0,then
          keyw,fmagbc,1              ! set keyword for FMAGSUM

          !  apply MVDI & Maxwells stress conditions
          cmsel,,%_cmpn(_icom)%
          nsle
          bf,all,mvdi,1
          nsel,r,ext
          esln
          cmsel,u,%_cmpn(_icom)%
          !   *msg,info,_cmpn(_icom)
          !   MXWF for component: %c
          sf,all,mxwf
          *if,_qacheck,eq,1,then
            sflis
          *endif
          _iappl=_iappl+1
          _actm(_icom)=1
        *endif
      *endif
    *else

      _actm(_icom)=0
      _cmpn(_icom)='  '

    *endif
  *enddo

  /nerr,1,1e5
  *if,_iappl,ge,1,then
    *set,_cmpnw
    *dim,_cmpnw,char,1,9
    *set,_chnum
    *dim,_chnum,char,9
    _chnum(1)='one','two','three','four','five','six','seven','eight','nine'
    *do,_icom1,1,8
      *do,_icom2,1,9-_icom1+1
        *if,_actm(_icom1),eq,0,then
          *do,_icom3,_icom1,8
            _actm(_icom3)=_actm(_icom3+1)
            _cmpn(_icom3)=_cmpn(_icom3+1)
          *enddo
        *endif
      *enddo
    *enddo

    *do,_icom1,1,_iappl
      _cmpnw(1,_icom1)=_cmpn(_icom1)
    *enddo

    *if,_mnu,ne,0,then
    *endif

    *if,_iappl,gt,1,then
    *else
    *endif


    *if,_iappl,ge,5,then
    *endif

    *if,_mnu,ne,0,then
    *endif

  *endif

  cmsel,,_curei
  cmsel,,_curni
  cmdele,_curei
  cmdele,_curni
  cmdele,_curel1
  !  :end
*endif

*if,_idbug,eq,0,then
  !  delete parameters except for arry of component names.
  *set,_actm
  *set,_chnum
  *set,_cmpnw
  *set,_elmtd
  _elmx2=
  _elnum=
  _elnum2=
  _errlv=
  _appl=
  _icom1=
  _imat1=
  _imatlm=
  _mnu=
  _nodo=
  _rout=
  _icom2=
  _icom3=
  _elmatc=
  _elnum1=
  _iappl=
  _icom=
  _AType=
  _MaxET=
  _EType=
  cmdele,_elm
*endif
/GOPR
/GOLIST

