#!/bin/sh
#**********************************************************************
#**                                                                 ***
#** vemmaxima                                                       ***
#**   transforms the splitted vecfem equation file /tmp/terms.$3    ***
#**   into a maxima session file, starts the maxima 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 maxima 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 maxima   ***
#**   to optimize the code and to hold the fortran standart of 24   ***
#**   continuation cards.                                           ***
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
EXT=$$
echo "maxima 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/maxima2.$EXT
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
echo "s/%EXT_/$3/1" > /tmp/sedin.$EXT
echo "s/%NK_/$1/1" >> /tmp/sedin.$EXT
echo "s/%INCLUDE_/$VECFEM_ROOT/1" >> /tmp/sedin.$EXT
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  chain the file pieces :                                        ***
#**                                                                 ***
cat $2/maxima1.h /tmp/maxima2.$EXT $2/maxima3.h |
sed -f /tmp/sedin.$EXT > /tmp/maximainput.$EXT
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  call maxima :                                                  ***
#**                                                                 ***
maxima  < /tmp/maximainput.$EXT
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  chance maxima output files                                     ***
#**                                                                 ***
cat -s /tmp/vemusermasks.$3 |
sed 's/0/.FALSE./g
     s/1/.TRUE./g' > /tmp/masks.$EXT
mv /tmp/masks.$EXT /tmp/vemusermasks.$3
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#** rmove work files                                                ***
#**                                                                 ***
rm /tmp/*.$EXT
