c gen_waveplot3d - generate a sequence of Plot3D files
c
c
c Copyright (c) 2006-2017 Francesco Poli <invernomuto@paranoici.org>
c 
c Permission is hereby granted, free of charge, to any person obtaining
c a copy of this software and associated documentation files (the
c "Software"), to deal in the Software without restriction, including
c without limitation the rights to use, copy, modify, merge, publish,
c distribute, sublicense, and/or sell copies of the Software, and to
c permit persons to whom the Software is furnished to do so, subject to
c the following conditions:
c 
c The above copyright notice and this permission notice shall be
c included in all copies or substantial portions of the Software.
c 
c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
c EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
c MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
c IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
c CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
c TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
c SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


      program main


c     constant definitions:

      integer,parameter :: imm = 41
      integer,parameter :: jmm = 101
      integer,parameter :: kmm = 2
      integer,parameter :: ngrid = 1
      integer,parameter :: nstep = 25
      integer,parameter :: nextr = 3


c     variable declarations:

      character(12) :: filenm
      integer :: i,j,k,istep,igrid
      real :: ph,v,step0
      real :: x(imm,jmm,kmm),y(imm,jmm,kmm),z(imm,jmm,kmm)
      real :: z0(imm,jmm,kmm)


c     generate undeformed mesh

      do k = 1, kmm
      do j = 1, jmm
      do i = 1, imm
        x (i,j,k) = real(i)
        y (i,j,k) = real(j)
        z0(i,j,k) = real(k)
      end do
      end do
      end do

c     write out 3D mesh Plot3D files:

      v = (y(1,jmm,1) - y(1,1,1))/nstep
      step0 = (nstep+1.0)/2.0 - (y(1,1,1) + y(1,jmm,1))/(2*v)

      open (unit=20,file='wave.p3d')
      write (20,'(a)') '{'
      write (20,'(4x,a)') '"auto-detect-format" : true,'
      write (20,'(4x,a)') '"filenames" : ['

      filenm = 'wave0000.xyz'

      do istep = 1-nextr, nstep+nextr

        do k = 1, kmm
        do j = 1, jmm
        do i = 1, imm
          ph = y(i,j,k) - v*(istep - step0)
          z(i,j,k) = z0(i,j,k) + 2*exp(-(ph/2)**2)
        end do
        end do
        end do

        write (filenm(5:8),'(i4.4)') istep+nextr
        open (unit=1,file=filenm,form='unformatted')
        rewind 1
        write(1) ngrid
        write(1) (imm,jmm,kmm, igrid=1,ngrid)
        do igrid = 1, ngrid
          write(1)
     $    (((x(i,j,k) + (igrid - 1.0),
     $       i=1,imm), j=1,jmm), k=1,kmm),
     $    (((y(i,j,k),
     $       i=1,imm), j=1,jmm), k=1,kmm),
     $    (((z(i,j,k),
     $       i=1,imm), j=1,jmm), k=1,kmm)
        end do
        close(unit=1)

        write (20,'(8x,a,i4.4,3a)',advance='no')
     $   '{ "time" : ', istep+nextr, ', "xyz" : "', filenm, '" }'
        if (istep.ne.nstep+nextr) then
          write (20,'(a)') ','
        else
          write (20,'(a)') ''
        end if

      end do

      write (20,'(4x,a)') ']'
      write (20,'(a)') '}'
      close(unit=20)

      stop
      end
