!
!     CalculiX - A 3-dimensional finite element program
!              Copyright (C) 1998-2005 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 fluidsections(inpc,textpart,set,istartset,iendset,
     &  ialset,nset,ielmat,matname,nmat,
     &  kon,ipkon,irstrt,istep,istat,n,iline,ipol,
     &  inl,ipoinp,inp,lakon,ielprop,nprop,nprop_,prop)
!
!     reading the input deck: *FLUID SECTION
!
      implicit none
!
      character*7 typename
      character*8 lakon(*)
      character*80 matname(*),material
      character*81 set(*),elset
      character*132 inpc(*),textpart(16)
!
      integer istartset(*),iendset(*),ialset(*),ielmat(*),
     &  kon(*),ipkon(*),irstrt,nset,nmat,
     &  istep,istat,n,key,i,j,k,imaterial,ipos,
     &  iline,ipol,inl,ipoinp(2,*),inp(3,*),ielprop(*),nprop,nprop_
!
      real*8 prop(*)
!
      if((istep.gt.0).and.(irstrt.ge.0)) then
         write(*,*) '*ERROR in fluidsections: *FLUID SECTION should'
         write(*,*) '  be placed before all step definitions'
         stop
      endif
!
      typename='       '
!
      do i=2,n
         if(textpart(i)(1:9).eq.'MATERIAL=') then
            material=textpart(i)(10:89)
         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:5).eq.'TYPE=') then
            read(textpart(i)(6:12),'(a7)',iostat=istat) typename
            if(istat.gt.0) call inputerror(inpc(iline))
         endif
      enddo
!
!     check for the existence of the set and the material
!
      do i=1,nmat
         if(matname(i).eq.material) exit
      enddo
      if(i.gt.nmat) then
         write(*,*) '*ERROR in fluidsections: nonexistent material'
         write(*,*) '  Card image:'
         write(*,'(a132)') inpc(iline)
         stop
      endif
      imaterial=i
!
      do i=1,nset
         if(set(i).eq.elset) exit
      enddo
      if(i.gt.nset) then
         elset(ipos:ipos)=' '
         write(*,*) '*ERROR in fluidsections: element set ',elset
         write(*,*) '  has not yet been defined. Card image:'
         write(*,'(a132)') inpc(iline)
         stop
      endif
!
!     assigning the elements of the set the appropriate material
!     and property pointer
!
      do j=istartset(i),iendset(i)
         if(ialset(j).gt.0) then
            if(lakon(ialset(j))(1:1).ne.'D') then
               write(*,*) '*ERROR in fluidsections: element ',
     &            ialset(j),' is no fluid element.'
               stop
            endif
            lakon(ialset(j))(2:8)=typename(1:7)
            ielmat(ialset(j))=imaterial
            ielprop(ialset(j))=nprop
         else
            k=ialset(j-2)
            do
               k=k-ialset(j)
               if(k.ge.ialset(j-1)) exit
               if(lakon(k)(1:1).ne.'D') then
                  write(*,*) '*ERROR in fluidsections: element ',
     &                 k,' is no fluid element.'
                  stop
               endif
               lakon(k)(2:8)=typename(1:7)
               ielmat(k)=imaterial
               ielprop(k)=nprop
            enddo
         endif
      enddo
!
!     storing the element properties
!
      if(typename.eq.'ORIFICE') then
         call getnewline(inpc,textpart,istat,n,key,iline,ipol,inl,
     &        ipoinp,inp)
         do j=1,3
            read(textpart(1),'(f40.0)',iostat=istat) prop(nprop+j)
            if(istat.gt.0) call inputerror(inpc(iline))
         enddo
         nprop=nprop+3
         if(nprop.gt.nprop_) then
            write(*,*) '*ERROR in fluidsections: increase nprop_'
            stop
         endif
      elseif(typename(1:6).eq.'SOURCE') then
      elseif(typename(1:7).eq.'       ') then
      else
         write(*,*) '*ERROR in fluidsections: ',typename
         write(*,*) '       is an unknown fluid section type'
         stop
      endif
!
      call getnewline(inpc,textpart,istat,n,key,iline,ipol,inl,
     &     ipoinp,inp)
!
      return
      end

