/*
   * Copyright (C) 2001-2003, CompHEP Collaboration
   *------------------------------------------------------
   * $Id: r_code_ampl.c,v 1.4 2003/06/25 18:46:42 kryukov Exp $
   *
   * $Log: r_code_ampl.c,v $
   * Revision 1.4  2003/06/25 18:46:42  kryukov
   * *** empty log message ***
   *
   * Revision 1.3  2003/04/22 09:01:20  kryukov
   * Apply indent to improve readability of files
   *
   * Revision 1.2  2003/04/20 17:55:02  kryukov
   * Clean up warnings during make
   *
 */

#include "service2/include/os.h"

#include "symb/include/r_code_ampl.h"
#include "symb/include/out_serv.h"
#include "symb/include/diaprins.h"
#include "symb/include/model.h"

static void 
diagramCode (vampl * di)
{
  writeAmDiagram (di, 1, '%', outFile);
}

void 
makeReduceAmpl (int nsub, vamplExt * diagPtr)
{
  char f_name[100];
  vampl *amp;
  int count = 0;
  int i;
  int first = 1;
  int nPolar = 1;

  strcpy (f_name, "a_");

  for (i = 0; i < amp->outno; i++)
    {
      edgeinvert *v = &amp->vertlist[amp->outer[i].vno][amp->outer[i].edno];
      switch (prtclbase[v->partcl - 1].spin)
	{
	case 0:
	  strcat (f_name, "0");
	  break;
	case 2:
	  strcat (f_name, "v");
	  break;
	case 1:
	  break;
	}
      if (prtclbase[v->partcl - 1].spin == 2)
	{
	  if (first)
	    {
	      writeF ("vector m%d", v->lorentz);
	      first = 0;
	    }
	  else
	    writeF (",m%d", v->lorentz);
	}
      if (prtclbase[v->partcl - 1].spin == 1)
	nPolar *= 2;
    }


  sprintf (f_name, "results%cp_ampl_%d.red", f_slash, nsub);
  outFileOpen (f_name);
  outputLanguage = 'R';

  amp = &diagPtr->Ampl;
  writeF ("vector p0");
  for (i = 0; i < amp->outno; i++)
    writeF (",p%d", i + 1);
  writeF ("$\n");


  for (i = 0; i < amp->outno; i++)
    {
      edgeinvert *v = &amp->vertlist[amp->outer[i].vno][amp->outer[i].edno];
      if (prtclbase[v->partcl - 1].spin == 2)
	{
	  if (first)
	    {
	      writeF ("vector m%d", v->lorentz);
	      first = 0;
	    }
	  else
	    writeF (",m%d", v->lorentz);
	}
      if (prtclbase[v->partcl - 1].spin == 1)
	nPolar *= 2;
    }
  if (!first)
    writeF ("$\n");
  writeF ("ARRAY AMPL[%d]$\n", nPolar);
  writeF ("FOR N_:=1:%d DO <<AMPL[N_]:=0$>>\n", nPolar);

  while (diagPtr)
    {
      if (diagPtr->status >= 0)
	{
	  count++;
	  writeF ("%c diagram %d\n", '%', count);
	  diagramCode (&diagPtr->Ampl);
	  writeAmDiagram (&diagPtr->Ampl, 1, '%', outFile);
	}
      diagPtr = diagPtr->next;
    }
  outFileClose ();
}
