!
!     CalculiX - A 3-dimensional finite element program
!              Copyright (C) 1998-2011 Guido Dhondt
!
!     This program 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(version 2);
!     
!
!     This program 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.
!
!     You should have received a copy of the GNU General Public License
!     along with this program; if not, write to the Free Software
!     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
!
      subroutine shellsections(inpc,textpart,set,istartset,iendset,
     &  ialset,nset,ielmat,matname,nmat,ielorien,orname,norien,
     &  thicke,kon,ipkon,offset,irstrt,istep,istat,n,iline,ipol,
     &  inl,ipoinp,inp,lakon,iaxial,ipoinpc,mi,icomposite)
!
!     reading the input deck: *SHELL SECTION
!
      implicit none
!
      logical nodalthickness,composite
!
      character*1 inpc(*)
      character*8 lakon(*)
      character*80 matname(*),orname(*),material,orientation
      character*81 set(*),elset
      character*132 textpart(16)
!
      integer mi(*),istartset(*),iendset(*),ialset(*),ielmat(mi(3),*),
     &  ielorien(mi(3),*),kon(*),ipkon(*),indexe,irstrt,nset,nmat,
     &  norien,nlayer,iset,icomposite,
     &  istep,istat,n,key,i,j,k,l,imaterial,iorientation,ipos,
     &  iline,ipol,inl,ipoinp(2,*),inp(3,*),iaxial,ipoinpc(0:*)
!
      real*8 thicke(mi(3),*),thickness,offset(2,*),offset1
!
      if((istep.gt.0).and.(irstrt.ge.0)) then
         write(*,*) 
     &        '*ERROR reading *SHELL SECTION: *SHELL SECTION should'
         write(*,*) '  be placed before all step definitions'
         stop
      endif
!
      nodalthickness=.false.
      composite=.false.
      offset1=0.d0
      material(1:1)=' '
      orientation(1:1)=' '
!
      do i=2,n
         if(textpart(i)(1:9).eq.'MATERIAL=') then
            material=textpart(i)(10:89)
         elseif(textpart(i)(1:12).eq.'ORIENTATION=') then
            orientation=textpart(i)(13:92)
         elseif(textpart(i)(1:6).eq.'ELSET=') then
            elset=textpart(i)(7:86)
            elset(81:81)=' '
            ipos=index(elset,' ')
            elset(ipos:ipos)='E'
         elseif(textpart(i)(1:14).eq.'NODALTHICKNESS') then
            nodalthickness=.true.
         elseif(textpart(i)(1:7).eq.'OFFSET=') then
            read(textpart(i)(8:27),'(f20.0)',iostat=istat) offset1
            if(istat.gt.0) call inputerror(inpc,ipoinpc,iline)
         elseif(textpart(i)(1:9).eq.'COMPOSITE') then
            composite=.true.
         else
            write(*,*) 
     &   '*WARNING reading *SHELL SECTION: parameter not recognized:'
            write(*,*) '         ',
     &                 textpart(i)(1:index(textpart(i),' ')-1)
            call inputwarning(inpc,ipoinpc,iline)
         endif
      enddo
!
!     check for the existence of the material (not for composites)
!
      if(.not.composite) then
         do i=1,nmat
            if(matname(i).eq.material) exit
         enddo
         if(i.gt.nmat) then
            write(*,*) 
     &        '*ERROR reading *SHELL SECTION: nonexistent material'
            call inputerror(inpc,ipoinpc,iline)
            stop
         endif
         imaterial=i
      elseif(material(1:1).ne.' ') then
         write(*,*) '*ERROR reading *SHELL SECTION: COMPOSITE and'
         write(*,*) '       MATERIAL are mutually exclusive parameters'
         stop
      endif
!
!     check for the existence of the orientation, if any
!
      if(orientation(1:1).eq.' ') then
         iorientation=0
      else
         do i=1,norien
            if(orname(i).eq.orientation) exit
         enddo
         if(i.gt.norien) then
            write(*,*)
     &         '*ERROR reading *SHELL SECTION: nonexistent orientation'
            call inputerror(inpc,ipoinpc,iline)
            stop
         endif
         iorientation=i
      endif
!
!     check for the existence of the element set
!
      do i=1,nset
         if(set(i).eq.elset) exit
      enddo
      if(i.gt.nset) then
         elset(ipos:ipos)=' '
         write(*,*) '*ERROR reading *SHELL SECTION: element set ',elset
         write(*,*) '       has not yet been defined. '
         call inputerror(inpc,ipoinpc,iline)
         stop
      endif
      iset=i
!
      call getnewline(inpc,textpart,istat,n,key,iline,ipol,inl,
     &     ipoinp,inp,ipoinpc)
!
!     assigning a thickness to the elements
!
      if(.not.composite) then
         if(.not.nodalthickness) then
            read(textpart(1)(1:20),'(f20.0)',iostat=istat) thickness
            if(istat.gt.0) then
               write(*,*) 
     &    '*ERROR reading *SHELL SECTION: shell thickness is lacking'
               call inputerror(inpc,ipoinpc,iline)
            endif
            if(iaxial.ne.0) thickness=thickness/iaxial
            do j=istartset(iset),iendset(iset)
               if(ialset(j).gt.0) then
                  if(lakon(ialset(j))(1:1).ne.'S') then
                     write(*,*) 
     &             '*ERROR reading *SHELL SECTION: *SHELL SECTION can'
                     write(*,*) 
     &                 '       only be used for shell elements.'
                     write(*,*) '       Element ',ialset(j),
     &                 ' is not a shell element.'
                     stop
                  endif
                  indexe=ipkon(ialset(j))
                  do l=1,8
                     thicke(1,indexe+l)=thickness
                  enddo
                  ielmat(1,ialset(j))=imaterial
                  ielorien(1,ialset(j))=iorientation
                  offset(1,ialset(j))=offset1
               else
                  k=ialset(j-2)
                  do
                     k=k-ialset(j)
                     if(k.ge.ialset(j-1)) exit
                     if(lakon(k)(1:1).ne.'S') then
                        write(*,*) 
     &            '*ERROR reading *SHELL SECTION: *SHELL SECTION can'
                        write(*,*) 
     &                    '       only be used for shell elements.'
                        write(*,*) '       Element ',k,
     &                    ' is not a shell element.'
                        stop
                     endif
                     indexe=ipkon(k)
                     do l=1,8
                        thicke(1,indexe+l)=thickness
                     enddo
                     ielmat(1,k)=imaterial
                     ielorien(1,k)=iorientation
                     offset(1,k)=offset1
                  enddo
               endif
            enddo
            call getnewline(inpc,textpart,istat,n,key,iline,ipol,inl,
     &           ipoinp,inp,ipoinpc)
         endif
!     
      else
         if(nodalthickness) then
            write(*,*) '*ERROR reading shellsections: for composite'
            write(*,*) '       materials is the parameter NODAL'
            write(*,*) '       THICKNESS not allowed'
            stop
         endif
!
         icomposite=1
         nlayer=0
         do
            read(textpart(1)(1:20),'(f20.0)',iostat=istat) thickness
            if(istat.gt.0) then
               write(*,*) 
     &     '*ERROR reading *SHELL SECTION: shell thickness is lacking'
               call inputerror(inpc,ipoinpc,iline)
            endif
            if(iaxial.ne.0) thickness=thickness/iaxial
!     
!     reading the material name
!     
            read(textpart(3)(1:80),'(a80)',iostat=istat) material
            if(istat.gt.0) then
               write(*,*) 
     &              '*ERROR reading *SHELL SECTION: no material defined'
               call inputerror(inpc,ipoinpc,iline)
            endif
!     
!     check for the existence of the material
!     
            do i=1,nmat
               if(matname(i).eq.material) exit
            enddo
            if(i.gt.nmat) then
               write(*,*) 
     &      '*ERROR reading *SHELL SECTION: nonexistent material'
               call inputerror(inpc,ipoinpc,iline)
               stop
            endif
            imaterial=i
!     
!     reading the orientation, if any
!     if no orientation is specified, the global orientation defined
!     by the ORIENTATION parameter, if any, will be used
!     
            read(textpart(4)(1:80),'(a80)',iostat=istat) orientation
!     
c            if(orientation(1:1).eq.' ') then
c               iorientation=0
c            else
            if(orientation(1:1).ne.' ') then
               do i=1,norien
                  if(orname(i).eq.orientation) exit
               enddo
               if(i.gt.norien) then
                  write(*,*)
     &        '*ERROR reading *SHELL SECTION: nonexistent orientation'
                  write(*,*) '  '
                  call inputerror(inpc,ipoinpc,iline)
                  stop
               endif
               iorientation=i
            endif
!     
            nlayer=nlayer+1
!     
            do j=istartset(iset),iendset(iset)
               if(ialset(j).gt.0) then
                  if(lakon(ialset(j))(1:3).ne.'S8R') then
                     write(*,*) 
     &           '*ERROR reading *SHELL SECTION: *SHELL SECTION'
                     write(*,*) 
     &                 '       with the option COMPOSITE can'
                     write(*,*) 
     &                 '       only be used for S8R shell elements.'
                     write(*,*) '       Element ',ialset(j),
     &                 ' is not a S8R shell element.'
                     stop
                  endif
                  indexe=ipkon(ialset(j))
                  do l=1,8
                     thicke(nlayer,indexe+l)=thickness
                  enddo
                  ielmat(nlayer,ialset(j))=imaterial
                  ielorien(nlayer,ialset(j))=iorientation
                  offset(1,ialset(j))=offset1
                  lakon(ialset(j))(8:8)='C'
               else
                  k=ialset(j-2)
                  do
                     k=k-ialset(j)
                     if(k.ge.ialset(j-1)) exit
                     if(lakon(k)(1:3).ne.'S8R') then
                        write(*,*) 
     &              '*ERROR reading *SHELL SECTION: *SHELL SECTION'
                        write(*,*) 
     &                       '    with the option COMPOSITE can'
                        write(*,*) 
     &                    '       only be used for shell elements.'
                        write(*,*) '       Element ',k,
     &                    ' is not a S8R shell element.'
                        stop
                     endif
                     indexe=ipkon(k)
                     do l=1,8
                        thicke(nlayer,indexe+l)=thickness
                     enddo
                     ielmat(nlayer,k)=imaterial
                     ielorien(nlayer,k)=iorientation
                     offset(1,k)=offset1
                     lakon(k)(8:8)='C'
                  enddo
               endif
            enddo
!     
            call getnewline(inpc,textpart,istat,n,key,iline,ipol,inl,
     &           ipoinp,inp,ipoinpc)
            if((istat.lt.0).or.(key.eq.1)) return
         enddo
      endif
!     
      return
      end
      
