- DMC McStas simulation working

SKIPPED:
	psi/amorstat.c
	psi/nxamor.c
	psi/pimotor.c
	psi/polterwrite.c
This commit is contained in:
koennecke
2005-07-05 07:06:15 +00:00
parent 9e0447b7dd
commit 96e8cdb2d5
22 changed files with 916 additions and 137 deletions

View File

@ -1,11 +1,13 @@
/*----------------------------------------------------------------------------
McStas simulation to SICS controller module mplementation file. For more details
see mcstas.tex.
McStas simulation to SICS controller module implementation file. For more
details see mcstas.tex.
copyright: see file COPYRIGHT
Mark Koennecke, June 2005
-----------------------------------------------------------------------------*/
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <assert.h>
#include <tcl.h>
@ -54,6 +56,7 @@ int McStasControllerFactory(SConnection *pCon, SicsInterp *pSics,
StringDictAddPair(pNew->scripts,"mccopydata","UNDEFINED");
StringDictAddPair(pNew->scripts,"mcmonfile","UNDEFINED");
pNew->pid = -1;
pNew->monitorScale = 1;
return AddCommand(pSics,"mccontrol",
McStasControllerWrapper,
@ -77,6 +80,16 @@ static int configureController(pMcStasController self, SConnection *pCon,
SCSendOK(pCon);
return 1;
}
if(strcmp(argv[2],"monitorscale") == 0){
self->monitorScale = atof(argv[3]);
if(self->monitorScale <= 0){
SCWrite(pCon,"ERROR: invalid monitor scale",eError);
self->monitorScale = 1;
return 0;
}
SCSendOK(pCon);
return 1;
}
if(!StringDictExists(self->scripts,argv[2])){
snprintf(pBueffel,255,"ERROR: scriptkey %s does not exist",argv[2]);
SCWrite(pCon,pBueffel,eError);
@ -114,6 +127,9 @@ static void listConfiguration(pMcStasController self, SConnection *pCon){
snprintf(pLine,255,"mccontrol.updateintervall = %d\n",
self->updateIntervall);
Tcl_DStringAppend(&txt,pLine,-1);
snprintf(pLine,255,"mccontrol.monitorscale = %f\n",
self->monitorScale);
Tcl_DStringAppend(&txt,pLine,-1);
snprintf(pLine,255,"mccontrol.pid = %d", self->pid);
Tcl_DStringAppend(&txt,pLine,-1);
@ -187,11 +203,22 @@ static int start(pMcStasController self, SConnection *pCon){
SCSendOK(pCon);
return 1;
}
/*-----------------------------------------------------------------------*/
static void wait4Finish(pMcStasController self){
int status;
if(self->pid > 0){
status = waitpid(self->pid,NULL,WNOHANG);
if(status >= 0){
self->pid = -1;
}
}
}
/*-------------------------------------------------------------------------*/
int McStasControllerWrapper(SConnection *pCon, SicsInterp *pSics,
void *pData, int argc, char *argv[]){
pMcStasController self = NULL;
char pBueffel[255];
char pBueffel[255], pFile[132];
self = (pMcStasController)pData;
assert(self);
@ -210,7 +237,11 @@ int McStasControllerWrapper(SConnection *pCon, SicsInterp *pSics,
return runScript(self,pCon,pSics,argc,argv);
} else if(strcmp(argv[1],"start") == 0){
return start(self,pCon);
} else {
} else if(strcmp(argv[1],"finish") == 0){
wait4Finish(self);
SCSendOK(pCon);
return 1;
} else {
snprintf(pBueffel,255,"ERROR: subcommand %s to mccontrol unknown",
argv[1]);
SCWrite(pCon,pBueffel,eError);
@ -256,6 +287,7 @@ int McStasStart(pMcStasController self, CounterMode mode, float fPreset){
self->pid = atoi(pResult);
self->startTime = time(NULL);
self->lastUpdate = self->startTime - self->updateIntervall;
self->lastMonitorRead = self->startTime;
return OKOK;
}
/*------------------------------------------------------------------------*/
@ -277,9 +309,15 @@ static long readMonFile(pMcStasController self){
/*------------------------------------------------------------------------*/
int McStasStatus(pMcStasController self, float *fControl){
char pResult[256];
long monValue;
float monValue;
int status, i;
/*
* check at max any second, else we keep the system busy and
* there is no CPU left for McStas
*/
SicsWait(1);
status = invokeScript(self,"mcisrunning",pServ->pSics,pResult, 255);
if(status == 0){
strncpy(self->errorText,pResult,255);
@ -300,23 +338,31 @@ int McStasStatus(pMcStasController self, float *fControl){
* handle monitor mode
*/
if(status == 1 && self->mode == ePreset){
/*
* check only any three seconds, else SICS uses up all the CPU time
* and the simulation has no chance.
*/
if(time(NULL) < self->lastMonitorRead + 3) {
return HWBusy;
}
monValue = -1;
/*
* try to read the monfile up to three times. Problems reading it
* can be synchronisation problems with McStas
*/
for(i = 0, monValue = -1; i < 3; i++){
monValue = readMonFile(self);
for(i = 0, monValue = -1; i < 7; i++){
monValue = (float)readMonFile(self);
if(monValue >= 0){
break;
}
}
if(monValue < 0){
strncpy(self->errorText,"Failed to read monitor file",255);
return HWFault;
return HWBusy;
}
*fControl = (float)monValue;
if(monValue >= (long)self->fPreset){
self->lastMonitorRead = time(NULL);
monValue *= self->monitorScale;
*fControl = monValue;
if(monValue >= self->fPreset){
McStasStop(self);
}
}
@ -348,10 +394,12 @@ int McStasTransferData(pMcStasController self){
return OKOK;
}
self->lastUpdate = time(NULL);
if(self->pid >= 0){
status = invokeScript(self,"mcdump",pServ->pSics,pResult, 255);
if(status == 0){
strncpy(self->errorText,pResult,255);
self->lastUpdate = time(NULL) - self->updateIntervall;
return HWFault;
}
}
@ -359,9 +407,9 @@ int McStasTransferData(pMcStasController self){
status = invokeScript(self,"mccopydata",pServ->pSics,pResult, 255);
if(status == 0){
strncpy(self->errorText,pResult,255);
self->lastUpdate = time(NULL) - self->updateIntervall;
return HWFault;
}
self->lastUpdate = time(NULL);
return OKOK;
}
/*-------------------------------------------------------------------------*/