/*
xmd - molecular dynamics for metals and ceramics
By Jonathan Rifkin <jon.rifkin@uconn.edu>
Copyright 1995-2004 Jonathan Rifkin
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; either version 2
of the License, or (at your option) any later version.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "cdhouse.h"
#include "rcvio.h"
#include "cdcor.h"
#include "iomngr.h"
#include "parse.h"
#include "strsub.h"
#include "cdsubs.h"
#define REPEATCOR
/************************/
/** GLOBAL VARIABLES **/
/************************/
extern Particle_t *a,*b;
extern Simulation_t *s;
extern FILE *out;
extern LIST *inlist;
extern int isig; /* SIGNAL SET BY kill OR Cntl-C */
/* READ REPEAT BLOCK */
void read_repeat (char *instr)
{
int n;
int qflag = 0, nread, nest;
char origstr[256];
char copystr[256], *tailstr, *cmdstr;
BUFF *b;
BOOLEAN CorFileFlag;
BOOLEAN RcvFileFlag;
BOOLEAN UseStep;
BOOLEAN UseSkip;
BOOLEAN ReadToEnd;
FILE *InputFile;
char *InputFileName;
int Step1;
int Step2;
int SkipInterval;
/* Parse command options */
cmdstr = strhed (&instr);
CorFileFlag = FALSE;
RcvFileFlag = FALSE;
UseStep = FALSE;
UseSkip = FALSE;
/* Read from COR or RCV file */
CorFileFlag = (strcmpi(cmdstr,"COR")==0);
RcvFileFlag = (strcmpi(cmdstr,"RCV")==0);
ReadToEnd = FALSE;
if (CorFileFlag || RcvFileFlag)
{
/* Get input file name */
InputFileName = strhed (&instr);
/* Test for reading from specpfied steps */
UseStep = !IsBlank(instr);
/* Get step numbers */
if (UseStep)
{
/* Get starting step */
Step1 = intstrf(&instr);
/* Read to end of file */
if (IsBlank(instr) || instr[0]=='*')
{
ReadToEnd = TRUE;
/* Remove token if '*' */
if (instr[0]=='*')
strhed (&instr);
}
/* Read to specified step */
else
{
Step2 = intstrf(&instr);
}
/* Test for skip interval */
UseSkip = !IsBlank(instr);
if (UseSkip)
SkipInterval = intstrf(&instr);
}
}
/* Repeat N times */
else
{
n = intstrf (&cmdstr);
}
/* INTIALIZE BUFFER */
b = m_ini_buff();
/* READ INPUT LINES UNTIL "END" */
nread = 0;
nest = 1;
while (nest && m_gets_list_f(origstr,256,inlist)!=NULL)
{
/* COPY STRING */
strcpy (copystr, origstr);
tailstr = copystr;
/* ECHO STRING IF NOT FROM BUFFER */
if (!inlist->IsBuffer && s->echo)
printf ("%s\n", origstr);
/* PARSE COMMAND */
cmdstr = strhed (&tailstr);
/* IF END REDUCE NEST COUNT */
if (!strcmpi(cmdstr, "END"))
nest--;
/* IF REPEAT INCREASE NEST COUNT */
else if (!strcmpi(cmdstr, "REPEAT"))
nest++;
/* ADD COMMAND TO BUFFER */
if (nest)
m_add_buff (b, origstr);
nread++;
}
/* CHECK FOR PROPER NESTING */
if (nest)
{
printf ("ERROR (read_repeat): Improper nesting of REPEAT and END.\n");
CleanAfterError();
}
/* ADD BUFFER TO INPUT LIST */
m_add_list (&inlist, b, "b");
/* PROCESS COMMANDS n TIMES */
/* (break if signal set, i.e. isig==1) */
/* Repeat for each step in COR file */
if (CorFileFlag || RcvFileFlag)
{
/* Open binary Cor file */
if (CorFileFlag)
InputFile = fopen (InputFileName, "rb");
/* Open text Rcv file */
else
InputFile = fopen (InputFileName, "rt");
/* Test for input file present */
if (InputFile==NULL)
{
printf ("ERROR: Cannot open file %s\n", InputFileName);
}
/* Process commands for each COR or RCV step */
do
{
/* Read step */
if (CorFileFlag)
ReadCorFile (InputFile, a);
else
readrcv (InputFile, a);
/* Stop input if end of file */
if (feof(InputFile))
break;
/* Stop if above range */
if (UseStep && !ReadToEnd && a->step>Step2)
break;
/* Skip if below range */
if (UseStep && a->step<Step1)
continue;
/* Skip if not on skip interval */
if (UseSkip && ((a->step - Step1) % SkipInterval != 0))
continue;
/* Process command list */
qflag = 0;
while (!qflag && m_gets_list(origstr,256,inlist)!=NULL && !qflag)
{
read_command (origstr, &qflag);
}
if (qflag) {
CleanBeforeEnd();
exit(0);
}
/* "REWIND" BUFFER */
m_rew_list (inlist);
}
while (!feof(InputFile) && isig!=1);
/* Close COR or RCV File */
fclose (InputFile);
}
else
{
while (n-- && isig!=1 && !qflag)
{
qflag = 0;
while (!qflag && m_gets_list(origstr,256,inlist)!=NULL)
{
read_command (origstr, &qflag);
}
/* "REWIND" BUFFER */
m_rew_list (inlist);
}
/* Quit program */
if (qflag) {
CleanBeforeEnd();
exit(0);
}
}
/* CLOSE CURRENT INPUT (AUTOMATICALLY REMOVES BUFFER STORAGE */
m_del_list (&inlist);
}