#!/bin/sh
#**********************************************************************
#**                                                                 ***
#**  vemmaple                                                       ***
#**   transforms the splitted vecfem equation file /tmp/terms.$3    ***
#**   into a maple session file, starts the maple session           ***
#**   and creates the fortran sources which are changed             ***
#**   to the vecfem user routines by vembuild.                      ***
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**        Copyrights University of Karlsruhe, 1995                 ***
#**        Program by L. Grosz                                      ***
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**   arguments: $1 => number of solution components (=NK)          ***
#**              $2 => path of the maple include files              ***
#**              $3 => file extension                               ***
#**                                                                 ***
#**    /tmp/terms.$3 = file of the equation with following syntax:  ***
#**                                                                 ***
#**         fminimize=0,1                                           ***
#**         fsteady=0,1                                             ***
#**       #declarations                                             ***
#**          <name>=<expr>                                          ***
#**       #Dirichlet conditions:                                    ***
#**          DB<i>=<expr>                                           ***
#**       #Initial conditions:                                      ***
#**          U0<i>=<expr>                                           ***
#**       #volume kernels                                           ***
#**          IM(3)=<expr>                                           ***
#**       #area kernels                                             ***
#**          IM(2)=<expr>                                           ***
#**       #line kernels                                             ***
#**          IM(1)=<expr>                                           ***
#**       #point kernels                                            ***
#**          IM(0)=<expr>                                           ***
#**                                                                 ***
#**   expr=(x<i>,tau<ij>,u<i>,u<i>x<j>,ut<i>,ut<i>x<j>,             ***
#**                                    v<i>,v<i>x<j>)               ***
#**         fortran expressions but '^' instead of '**'             ***
#**                                                                 ***
#**  the vecfem variables are elongated so that in the fortran      ***
#**  code of the user routines the code will not become longer      ***
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  generated files:                                               ***
#**                                                                 ***
#**  /tmp/vemuserb.$3                                               ***
#**    for compu=1,$1                                               ***
#**                    s<i>=<expr>                                  ***
#**                    t<i>=<expr>                                  ***
#**                    B(compu)=<expr>                              ***
#**                                                                 ***
#**   /tmp/vemuseru0.$3                                             ***
#**    for compu=1,$1                                               ***
#**                    s<i>=<expr>                                  ***
#**                    t<i>=<expr>                                  ***
#**                    U0ZZ(compu)=<expr>                           ***
#**                                                                 ***
#**   /tmp/vemuserf.$3                                              ***
#**    for compu=1,$1; class=0,3                                    *** 
#**                   s<i>=<expr>                                   ***
#**                   t<i>=<expr>                                   ***
#**                   F0(compv,class)      =<expr>                  ***
#**                   F1(compv,class,1)    =<expr>                  ***
#**                         ...                                     ***
#**                   F1(compv,class,class)=<expr>                  ***
#**                                                                 ***
#**   /tmp/vemusrfu.$3                                              ***
#**    for compu=1,$1; compv=1,$1; class=0,3                        *** 
#**                  s<i>=<expr>                                    ***
#**                  t<i>=<expr>                                    ***
#**                  F0U(compv,compu,class)=<expr>                  ***
#**                  F1U(compv,compu,class,1)=<expr>                ***
#**                         ...                                     ***
#**                  F1U(compv,compu,class,class)=<expr>            ***
#**                  F0UX(compv,compu,class,1)=<expr>               ***
#**                         ...                                     ***
#**                  F0UX(compv,compu,class,class)=<expr>           ***
#**                  F1UX(compv,compu,class,1,1)=<expr>             ***
#**                         ...                                     ***
#**                  F1UX(compv,compu,class,class,class)=<expr>     ***
#**   /tmp/vemusrfut.$3                                             ***
#**    for compu=1,$1; compv=1,$1; class=0,3                        *** 
#**                  s<i>=<expr>                                    ***
#**                  t<i>=<expr>                                    ***
#**                  F0UT(compv,compu,class)=<expr>                 ***
#**                  F1UT(compv,compu,class,1)=<expr>               ***
#**                         ...                                     ***
#**                  F1UT(compv,compu,class,class)=<expr>           ***
#**                  F0UTX(compv,compu,class,1)=<expr>              ***
#**                         ...                                     ***
#**                  F0UTX(compv,compu,class,class)=<expr>          ***
#**                  F1UTX(compv,compu,class,1,1)=<expr>            ***
#**                         ...                                     ***
#**                  F1UTX(compv,compu,class,class,class)=<expr>    ***
#**                                                                 ***
#**     /tmp/vemusermasks.$3                                        ***
#**                                                                 ***
#**                  LSYM=.TRUE./.FALSE.                            ***
#**                                                                 ***
#**   expr=(xzzzz<i>,tauzzz<ij>,uzz(i),dudxzz(i,j),utzz(i),         ***
#**         dutdxzz(i,j),s<i>,t<i>)                                 ***
#**                                                                 ***
#**   fortran expression, where s<i> t<i> are variables of maple    ***
#**   to optimize the code and to hold the fortran standart of 24   ***
#**   continuation cards.                                           ***
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
CHANCEEXP () {
if test -a /tmp/$1 ;  then
  cp /tmp/$1 /tmp/CHANCEEXP.$$
  sed 's/\*\*10.D0/\*\*10/g
       s/signum(/SIGN(1.D0,/g
       s/\*\*9.D0/\*\*9/g
       s/\*\*8.D0/\*\*8/g
       s/\*\*7.D0/\*\*7/g
       s/\*\*6.D0/\*\*6/g
       s/\*\*5.D0/\*\*5/g
       s/\*\*4.D0/\*\*4/g
       s/\*\*3.D0/\*\*3/g
       s/\*\*2.D0/\*\*2/g' /tmp/CHANCEEXP.$$ > /tmp/$1
  rm /tmp/CHANCEEXP.$$
fi   
}
#**********************************************************************
EXT=$$
echo "maple input file is generated." 
$VECFEM_AWK 'BEGIN{NK='$1';FS="="}{
buffer=$0;i=1
# elongate name of tau
while (i<=3){
  ra=sprintf("tau%s",i);neu=sprintf("tauzzzzz%s",i)
  gsub(ra,neu,buffer)
i++}

k=NK
while (1<=k){
  i=1
  # chance and elongate name of the u and v derivatives
  while (i<=3){
   ra=sprintf("ut%sx%s",k,i);neu=sprintf("dutdxzz[%s,%s]",k,i)
   gsub(ra,neu,buffer)
   ra=sprintf("u%sx%s",k,i);neu=sprintf("dudxzz[%s,%s]",k,i)
   gsub(ra,neu,buffer)
   ra=sprintf("v%sx%s",k,i);neu=sprintf("dvdxzz[%s,%s]",k,i)
   gsub(ra,neu,buffer)
  i++}

  # chance and elonagte name of u and v
  ra=sprintf("ut%s",k);neu=sprintf("utzz[%s]",k)
  gsub(ra,neu,buffer)
  ra=sprintf("u%s",k);neu=sprintf("uzz[%s]",k)
  gsub(ra,neu,buffer)
  ra=sprintf("v%s",k);neu=sprintf("vzz[%s]",k)
  gsub(ra,neu,buffer)

k--}

i=1
# elongate name of x
while (i<=3){
 ra=sprintf("x%s",i);neu=sprintf("xzzzz%s",i)
 gsub(ra,neu,buffer)
i++}

# change braces of the DB, U0 and IM
split(buffer,parts)
ra="\\(";neu="[";gsub(ra,neu,parts[1])
ra="\\)";neu="]";gsub(ra,neu,parts[1])
printf "%s:=%s;\n",parts[1],parts[2]
}
END{} ' /tmp/terms.$3 > /tmp/maple2.$EXT
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  chain the file pieces :                                        ***
#**                                                                 ***
echo "s/%EXT_/$3/g" > /tmp/sedin.$EXT
echo "s/%NK_/$1/1" >> /tmp/sedin.$EXT
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  chain the file pieces :                                        ***
#**                                                                 ***
cat $2/maple1.h /tmp/maple2.$EXT $2/maple3.h |
   sed -f /tmp/sedin.$EXT > /tmp/mapleinput.$EXT
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  call maple :                                                   ***
#**                                                                 ***
maple -e 2 -q < /tmp/mapleinput.$EXT
# old maple versions: maple -e 2 -f -q < /tmp/mapleinput.$EXT
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  chance maple output files                                      ***
#**                                                                 ***
cat -s /tmp/vemusermasks.$3 |
sed 's/VEMfalse/.FALSE./g
     s/VEMtrue/.TRUE./g' > /tmp/masks.$EXT
mv /tmp/masks.$EXT /tmp/vemusermasks.$3
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  maple has some problems with exponents:                        ***
#**                                                                 ***
CHANCEEXP vemuserb.$3
CHANCEEXP vemuseru0.$3
CHANCEEXP vemuserf.$3
CHANCEEXP vemusrfu.$3
CHANCEEXP vemusrfut.$3
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#** rmove work files                                                ***
#**                                                                 ***
rm /tmp/*.$EXT
