/out,_per3d,out
!    per3d.mac
!    Generates Periodic bc for a 3D model scalar
!
!    Component input. The following components must be defined
!                     and they must be node components
!     component
!       name  location of nodes in the component
!      cp_1:          side 1 of the model
!      cp_2:          side 2 of the model
!
!     arguement input
!     arg1 = coordinate system number to locate the periodic plane
!     arg2 = axis normal to face of the planes for which the periodic
!            conditions will be placed
!          = 1(x)
!          = 2(y)
!          = 3(z)

!     arg3 = 0 (even)
!          = 1 (odd)

fini
/prep7
cmsel,,cp_1
*get,_cn_1,node,,count
cmsel,,cp_2
*get,_cn_2,node,,count
*if,_cn_1*_cn_2,eq,0,then
   *msg,info
   Side 1 or Side 2 of the requried component input does not have&
   any nodes. No action.
*endif
/nerr,0,1e4
/uis,msgpop,3
*if,_cn_1*_cn_2,eq,0,:exi

cmsel,,cp_2
*get,_nmx_2,node,,num,max
*set,_use_2
*dim,_use_2,,_nmx_2


cmsel,,cp_1
cmsel,a,cp_2
*if,arg3,eq,0,then
   cpdel,all,,,any
   numc,cp
   *get,_perc,active,,cp
   /pbc,cp,1
*else
   cedel,all,,,any
   numc,ce
   *get,_perc,active,,ce
   /pbc,ce,1
*endif
_perc0=_perc

cmsel,,cp_1
*get,_n1_mx,node,,num,max
*set,_ndno
*dim,_ndno,,_n1_mx
*vfill,_ndno(1),ramp,1,1
*set,_mskv
*dim,_mskv,,_n1_mx
*vget,_mskv(1),node,1,nsel
*voper,_mskv(1),_mskv(1),gt,0
*vmask,_mskv(1)
/nopr
/out,_data
*vwrite,_ndno(1)
(f15.0)
/out,_per3d,out,,append
/gopr
*set,_ndnoa
*dim,_ndnoa,,_cn_1
*vread,_ndnoa(1),_data
(f15.0)


csys,arg1
_ilocv=arg2
*set,_locv
*dim,_locv,char,3
_locv(1)='x','y','z'
*set,_nx_1
*dim,_nx_1,,3

cmsel,,cp_2
*get,_perv,node,,mxloc,%_locv(_ilocv)%
cmsel,a,cp_1

*if,arg3,eq,0,then
 *do,_icp,1,_cn_1
  *get,_nx_1(1),node,_ndnoa(_icp),loc,%_locv(1)%
  *get,_nx_1(2),node,_ndnoa(_icp),loc,%_locv(2)%
  *get,_nx_1(3),node,_ndnoa(_icp),loc,%_locv(3)%
  _nx_1(_ilocv)=_perv
  _nd_2=node(_nx_1(1),_nx_1(2),_nx_1(3))
  _perc=_perc+1
  _use_2(_nd_2)=_use_2(_nd_2)+1
  cp,_perc,mag,_ndnoa(_icp),_nd_2
 *enddo
*else
 *do,_icp,1,_cn_1
  *get,_nx_1(1),node,_ndnoa(_icp),loc,%_locv(1)%
  *get,_nx_1(2),node,_ndnoa(_icp),loc,%_locv(2)%
  *get,_nx_1(3),node,_ndnoa(_icp),loc,%_locv(3)%
  _nx_1(_ilocv)=_perv
  _nd_2=node(_nx_1(1),_nx_1(2),_nx_1(3))
  _perc=_perc+1
  _use_2(_nd_2)=_use_2(_nd_2)+1
  cp,_perc,mag,_ndnoa(_icp),_nd_2
 *enddo
 ce,_perc,0,_ndnoa(_icp),mag,1,_nd_2,mag,1
*endif

_percn=_perc-_perc0
/nopr
/out,per3d,sum
*msg,info
________SUMMARY OF PERIODIC CONSTRAINT GENERATION_____
*msg,info,_cn_1
 Number of nodes in component 1:________ %i
*msg,info,_cn_2
 Number of nodes in component 2:________ %i
*msg,info,arg1
 Coordinate system used to locate CP_2:_ %i
 *msg,info,_locv(arg2)
 Coordinate axis to locate CP_2 plane:__ %c
 *msg,info,_percn
 Number of conditions generated:________ %i
 *if,arg3,eq,0,then
 *msg,info
 Lateral conditions generated:__________ Coupling (EVEN)
 *else
 *msg,info
 Lateral conditions generated:__________ Constraint Eqns (ODD)
 *endif
*msg,info

*msg,info
 Be sure to check the array _USE_2 in the&
 parameter file PER3D.PAR for the number&
 of times each node in CP_2 was used.  If&
 _USE_2(i)=3, then node i (in CP_2)&
 was used 3 times.

 *msg,info

/out
/gopr
*if,_mg1,eq,0,then
 *uili,per3d,sum
*endif
parsav,all,per3d,par

:exi
/nerr,1,1e4
/uis,msgpop,2
cmsel,,cp_1
cmsel,a,cp_2
/out