Пример #1
0
struct figureData *figureFromPython(PyObject *pyFigure) {
	struct figureData *figure=safeCalloc(1, sizeof(struct figureData));

	if (pyFigure==Py_None) {
		figure->dim=-1;
		return figure;
	}
	figure->dim=PyList_Size(pyFigure)-1;

	if (PyErr_Occurred() || figure->dim<0)
		throw("Wrong figure");
	figure->count=safeMalloc(sizeof(GLint) * (figure->dim+1));

	PyObject *vertices=PyList_GET_ITEM(pyFigure, 0);
	figure->count[0]=PyList_Size(vertices);
	if (PyErr_Occurred())
		throw("Wrong list of vertices");
	figure->vertices=safeCalloc(figure->count[0], sizeof(GLdouble *));
	for (int i=0; i<figure->count[0]; i++) {
		PyObject *vertex=PyList_GET_ITEM(vertices, i);
		figure->vertices[i]=safeMalloc(sizeof(GLdouble) * figure->dim);
		if ((PyTuple_Size(vertex)!=figure->dim) || (PyErr_Occurred()))
			throw("Wrong list of vertices");
		for (int j=0; j<figure->dim; j++) {
			PyObject *value=PyTuple_GET_ITEM(vertex, j);
			figure->vertices[i][j]=PyFloat_AsDouble(value);
		}
		if (!safeCheckPos(figure->vertices[i], figure->dim))
			throw("Wrong vertex position")
	}
	if (PyErr_Occurred())
		throw("Wrong list of vertices");

	figure->boundary=safeCalloc(figure->dim+1, sizeof(GLint **));
	for (int i=1; i<=figure->dim; i++) {
		PyObject *faces=PyList_GET_ITEM(pyFigure, i);
		figure->count[i]=PyList_Size(faces);
		if (PyErr_Occurred())
			throw("Wrong topology");
		figure->boundary[i]=safeCalloc(figure->count[i], sizeof(GLint *));
		for (int j=0; j<figure->count[i]; j++) {
			PyObject *face=PyList_GET_ITEM(faces, j);
			int count=PyList_Size(face);
			if (PyErr_Occurred())
				throw("Wrong topology");
			figure->boundary[i][j]=safeMalloc(sizeof(GLint) * (count+1));
			figure->boundary[i][j][0]=count;
			for (int k=1; k<=count; k++) {
				PyObject *value=PyList_GET_ITEM(face, k-1);
				figure->boundary[i][j][k]=PyInt_AsLong(value);
			}
		}
	}
	if (PyErr_Occurred())
		throw("Wrong topology");

	return figure;
}
Пример #2
0
void *safeCalloc(size_t num, size_t size){
	void *ptr = calloc(num, size);
	if(ptr == NULL)
		ptr = safeCalloc(num, size);
	
	return ptr;
}
Пример #3
0
Файл: graph.c Проект: crcox/lens
Trace createTrace(Graph G, char *object) {
  int t;
  Trace T = (Trace) safeCalloc(1, sizeof *T, "createTrace:T");
  for (t = 0; t < G->numTraces && G->trace[t]; t++);
  if (t >= G->numTraces) {
    int n = imax(t + 2, 2 * G->numTraces);
    G->trace = safeRealloc(G->trace, n * sizeof(Trace), "G->trace");
    memset(G->trace + G->numTraces, 0, (n - G->numTraces) * sizeof(Trace));
    G->numTraces = n;
  }
  G->trace[t] = T;
  T->num = t;

  T->graph = G;
  if (T->num == 0) T->color = copyString("black");
  else T->color = nextColor();
  T->maxVals = DEF_GR_values;
  T->active = TRUE;
  T->transient = FALSE;
  T->visible = TRUE;
  T->val = (point *) safeMalloc(T->maxVals * sizeof(point), "T->val");
  T->object = copyString(object);
  G->tracesChanged = TRUE;
  refreshPropsLater(G);
  return T;
}
Пример #4
0
PyObject *scriptFigureOpen(PyObject *self, PyObject *args) {
	PyObject *pyFigure;
	int preserveRotation=false;
	if (!PyArg_ParseTuple(args, "O|i", &pyFigure, &preserveRotation))
		return NULL;

	struct figureData *figure=figureFromPython(pyFigure);
	if (figure) {
		if (preserveRotation && (figure->dim==-1)) {
			figure->dim=figureData.dim;
			figure->count=safeCalloc(figure->dim+1, sizeof(int));
		}
		figureOpen(figure, preserveRotation);
	}
	if (!PyErr_Occurred()) {
		if (preserveRotation) {
			scriptEventsPerform(&scriptEventsModified);
		} else {
			scriptEventsPerform(&scriptEventsNew);
			scriptEventsPerform(&scriptEventsOpen, NULL);
		}
	}
	if (PyErr_Occurred())
		return NULL;
	return Py_None;
}
Пример #5
0
Файл: graph.c Проект: crcox/lens
Graph createGraph(void) {
  int g;
  Graph G = (Graph) safeCalloc(1, sizeof *G, "createGraph:G");

  for (g = 0; g < Root->numGraphs && Root->graph[g]; g++);
  if (g >= Root->numGraphs) {
    int n = imax(g + 2, 2 * Root->numGraphs);
    Root->graph = safeRealloc(Root->graph, n * sizeof(Graph), "Root->graph");
    memset(Root->graph + Root->numGraphs, 0,
           (n - Root->numGraphs) * sizeof(Graph));
    Root->numGraphs = n;
  }
  Root->graph[g] = G;
  G->num      = g;

  G->updateOn = ON_REPORT;
  G->updateEvery = 1;

  G->width    = 0;
  G->height   = 0;
  G->cols     = DEF_GR_columns;

  G->max = G->min = 0.0;
  G->fixMax   = FALSE;
  G->fixMin   = TRUE;
  G->maxVal   = -LARGE_VAL;
  G->minVal   = LARGE_VAL;
  G->scaleY   = 0;

  G->clearOnReset = FALSE;
  G->storeOnReset = TRUE;

  G->maxX     = 0;
  G->hidden   = TRUE;
  if (!Batch) showGraph(G);
  return G;
}
Пример #6
0
/** Allocate and initialise a new scope structure.
	@return A newly allocated @a Scope structure.

	If memory can not be allocated, the function will not return.
*/
Scope *newScope(void) {
    Scope *scope;
    scope = (Scope *) safeCalloc(sizeof(Scope), "newScope");
    return scope;
}
Пример #7
0
void initLinkTypes(void) {
  LinkTypeName = (char **) safeCalloc(LINK_NUM_TYPES, sizeof(char *),
				      "LinkTypeName");
}
Пример #8
0
char *evaluateStringAsBashCommand(char *command, char *input) {
  char *res;
  int okay, errorOnInput;
  int exitcode;
  int pipesToBash[2];
  int pipesFromBash[2];
  pid_t pid;
  int childStatus;
  char readBuffer[READ_BUFFER_SIZE];
  int readLen;
  char *buf;
  int len;
  int i;

  if ((command == NULL) || (strlen(command) == 0)) {
    printMessage(1,"Warning in bashevaluate: no command provided\n");
    return NULL;
  }

  res = NULL;
  okay = 0;
  fflush(NULL);
  
  // Create two unnamed pipe
  if ((input != NULL) && (pipe(pipesToBash) == -1)) {
    // Error creating the pipe
    printMessage(1,"Warning in bashevaluate: error while creating a pipe");
  } else {
    if (pipe(pipesFromBash) == -1) {
      // Error creating the pipe
      printMessage(1, "Warning in bashevaluate: error while creating a pipe");
    } else {
      // Fork
      //
      // Flush before forking
      //
      fflush(NULL);
      if ((pid = fork()) == -1) {
	// Error forking
	printMessage(1, "Warning in bashevaluate: error while forking");
      } else {
	// Fork worked
	if (pid == 0) {
	  // I am the child
	  //
	  // Close the unneeded ends of the pipes.
	  //
	  if (input != NULL) close(pipesToBash[1]);
	  close(pipesFromBash[0]);
	  
	  // Connect my input and output to the pipe
	  //
	  if (input != NULL) {
	    if (dup2(pipesToBash[0],0) == -1) {
	      _exit(1);
	    }
	  }
	  if (dup2(pipesFromBash[1],1) == -1) {
	    _exit(1);
	  }
	  
	  // Execute bash
	  //
	  fflush(NULL);
	  execlp("sh","sh","-c",command,(char *) NULL);
	  fflush(NULL);

	  _exit(1);
	} else {
	  // I am the father
	  //
	  // Close the unneeded ends of the pipes.
	  //
	  if (input != NULL) close(pipesToBash[0]);
	  close(pipesFromBash[1]);
	  
	  // Do my job
	  //
	  errorOnInput = 0;
	  if (input != NULL) {
	    if (write(pipesToBash[1],input,
		      strlen(input) * sizeof(char)) == -1) {
	      printMessage(1,"Warning in bashevaluate: unable to write to bash");
	      errorOnInput = 1;
	    }
	    close(pipesToBash[1]);
	  }

	  fflush(NULL);

	  if (!errorOnInput) {
	    do {
	      readLen = read(pipesFromBash[0],readBuffer,READ_BUFFER_SIZE);
	      if (readLen > 0) {
		if (res == NULL) {
		  res = safeCalloc(readLen + 1, sizeof(char));
		  buf = res;
		} else {
		  len = strlen(res);
		  buf = safeCalloc(len + readLen + 1, sizeof(char));
		  strcpy(buf,res);
		  free(res);
		  res = buf;
		  buf += len;
		}
		for (i=0;i<readLen;i++) {
		  *buf = (readBuffer[i] == '\0') ? '?' : readBuffer[i];
		  buf++;
		}
	      }
	    } while (readLen == READ_BUFFER_SIZE);

	    // Wait for my child to exit
	    wait(&childStatus);
	    
	    // Read the rest of the pipe if it filled up again after 
	    // having been emptied already.
	    do {
	      readLen = read(pipesFromBash[0],readBuffer,READ_BUFFER_SIZE);
	      if (readLen > 0) {
		if (res == NULL) {
		  res = safeCalloc(readLen + 1, sizeof(char));
		  buf = res;
		} else {
		  len = strlen(res);
		  buf = safeCalloc(len + readLen + 1, sizeof(char));
		  strcpy(buf,res);
		  free(res);
		  res = buf;
		  buf += len;
		}
		for (i=0;i<readLen;i++) {
		  *buf = (readBuffer[i] == '\0') ? '?' : readBuffer[i];
		  buf++;
		}
	      }
	    } while (readLen == READ_BUFFER_SIZE);

	    if (WEXITSTATUS(childStatus) != 0) {
	      printMessage(1, "Warning in bashevaluate: the exit code of the child process is %d.\n", WEXITSTATUS(childStatus));
	    } else {
	      printMessage(2, "Information in bashevaluate: the exit code of the child process is %d.\n", WEXITSTATUS(childStatus));
	    }

	    close(pipesFromBash[0]);

	    okay = 1;
	    if (res == NULL) {
	      res = safeCalloc(2, sizeof(char));
	    }
	    len = strlen(res);
	    if (len >= 1) {
	      if (res[len-1] == '\n') res[len-1] = '\0';
	    }
	  }
	}
      }
    }
  }

  if (!okay) {
    if (res != NULL) free(res);
    res = NULL;
  }

  fflush(NULL);

  return res;
}
Пример #9
0
void externalPlot(char *library, mpfr_t a, mpfr_t b, mp_prec_t samplingPrecision, int random, node *func, int mode, mp_prec_t prec, char *name, int type) {
  void *descr;
  void  (*myFunction)(mpfr_t, mpfr_t);
  char *error;
  mpfr_t x_h,x,y,temp,perturb,ulp,min_value;
  double xd, yd;
  FILE *file;
  gmp_randstate_t state;
  char *gplotname;
  char *dataname;
  char *outputname;


  gmp_randinit_default (state);

  if(samplingPrecision > prec) {
    sollyaFprintf(stderr, "Error: you must use a sampling precision lower than the current precision\n");
    return;
  }

  descr = dlopen(library, RTLD_NOW);
  if (descr==NULL) {
    sollyaFprintf(stderr, "Error: the given library (%s) is not available (%s)!\n",library,dlerror());
    return;
  }

  dlerror(); /* Clear any existing error */
  myFunction = (void (*)(mpfr_t, mpfr_t)) dlsym(descr, "f");
  if ((error = dlerror()) != NULL) {
    sollyaFprintf(stderr, "Error: the function f cannot be found in library %s (%s)\n",library,error);
    return;
  }

  if(name==NULL) {
    gplotname = (char *)safeCalloc(13 + strlen(PACKAGE_NAME), sizeof(char));
    sprintf(gplotname,"/tmp/%s-%04d.p",PACKAGE_NAME,fileNumber);
    dataname = (char *)safeCalloc(15 + strlen(PACKAGE_NAME), sizeof(char));
    sprintf(dataname,"/tmp/%s-%04d.dat",PACKAGE_NAME,fileNumber);
    outputname = (char *)safeCalloc(1, sizeof(char));
    fileNumber++;
    if (fileNumber >= NUMBEROFFILES) fileNumber=0;
  }
  else {
    gplotname = (char *)safeCalloc(strlen(name)+3,sizeof(char));
    sprintf(gplotname,"%s.p",name);
    dataname = (char *)safeCalloc(strlen(name)+5,sizeof(char));
    sprintf(dataname,"%s.dat",name);
    outputname = (char *)safeCalloc(strlen(name)+5,sizeof(char));   
    if ((type==PLOTPOSTSCRIPT) || (type==PLOTPOSTSCRIPTFILE)) sprintf(outputname,"%s.eps",name);
  }

  
  /* Beginning of the interesting part of the code */
  file = fopen(gplotname, "w");
  if (file == NULL) {
    sollyaFprintf(stderr,"Error: the file %s requested by plot could not be opened for writing: ",gplotname);
    sollyaFprintf(stderr,"\"%s\".\n",strerror(errno));
    return;
  }
  sollyaFprintf(file, "# Gnuplot script generated by %s\n",PACKAGE_NAME);
  if ((type==PLOTPOSTSCRIPT) || (type==PLOTPOSTSCRIPTFILE)) sollyaFprintf(file,"set terminal postscript eps color\nset out \"%s\"\n",outputname);
  sollyaFprintf(file, "set xrange [%1.50e:%1.50e]\n", mpfr_get_d(a, GMP_RNDD),mpfr_get_d(b, GMP_RNDU));
  sollyaFprintf(file, "plot \"%s\" using 1:2 with dots t \"\"\n",dataname);
  fclose(file);

  file = fopen(dataname, "w");
  if (file == NULL) {
    sollyaFprintf(stderr,"Error: the file %s requested by plot could not be opened for writing: ",dataname);
    sollyaFprintf(stderr,"\"%s\".\n",strerror(errno));
    return;
  }

  mpfr_init2(x_h,samplingPrecision);
  mpfr_init2(perturb, prec);
  mpfr_init2(x,prec);
  mpfr_init2(y,prec);
  mpfr_init2(temp,prec);
  mpfr_init2(ulp,prec);
  mpfr_init2(min_value,53);

  mpfr_sub(min_value, b, a, GMP_RNDN);
  mpfr_div_2ui(min_value, min_value, 12, GMP_RNDN);

  mpfr_set(x_h,a,GMP_RNDD);
  
  while(mpfr_less_p(x_h,b)) {
    mpfr_set(x, x_h, GMP_RNDN); // exact
    
    if (mpfr_zero_p(x_h)) {
      mpfr_set(x_h, min_value, GMP_RNDU);
    }
    else {
      if (mpfr_cmpabs(x_h, min_value) < 0) mpfr_set_d(x_h, 0., GMP_RNDN);
      else mpfr_nextabove(x_h);
    }

    if(random) {
      mpfr_sub(ulp, x_h, x, GMP_RNDN);
      mpfr_urandomb(perturb, state);
      mpfr_mul(perturb, perturb, ulp, GMP_RNDN);
      mpfr_add(x, x, perturb, GMP_RNDN);
    }

    (*myFunction)(temp,x);
    evaluateFaithful(y, func, x,prec);
    mpfr_sub(temp, temp, y, GMP_RNDN);
    if(mode==RELATIVE) mpfr_div(temp, temp, y, GMP_RNDN);
    xd =  mpfr_get_d(x, GMP_RNDN);
    if (xd >= MAX_VALUE_GNUPLOT) xd = MAX_VALUE_GNUPLOT;
    if (xd <= -MAX_VALUE_GNUPLOT) xd = -MAX_VALUE_GNUPLOT;
    sollyaFprintf(file, "%1.50e",xd);
    if (!mpfr_number_p(temp)) {
      if (verbosity >= 2) {
	changeToWarningMode();
	sollyaPrintf("Information: function undefined or not evaluable in point %s = ",variablename);
	printValue(&x);
	sollyaPrintf("\nThis point will not be plotted.\n");
	restoreMode();
      }
    }
    yd = mpfr_get_d(temp, GMP_RNDN);
    if (yd >= MAX_VALUE_GNUPLOT) yd = MAX_VALUE_GNUPLOT;
    if (yd <= -MAX_VALUE_GNUPLOT) yd = -MAX_VALUE_GNUPLOT;
    sollyaFprintf(file, "\t%1.50e\n", yd);
  }

  fclose(file);
 
  /* End of the interesting part.... */

  dlclose(descr);
  mpfr_clear(x);
  mpfr_clear(y);
  mpfr_clear(x_h);
  mpfr_clear(temp);
  mpfr_clear(perturb);
  mpfr_clear(ulp);
  mpfr_clear(min_value);

  if ((name==NULL) || (type==PLOTFILE)) {
    if (fork()==0) {
      daemon(1,1);
      execlp("gnuplot", "gnuplot", "-persist", gplotname, NULL);
      perror("An error occurred when calling gnuplot ");
      exit(1);
    }
    else wait(NULL);
  }
  else { /* Case we have an output: no daemon */
    if (fork()==0) {
      execlp("gnuplot", "gnuplot", "-persist", gplotname, NULL);
      perror("An error occurred when calling gnuplot ");
      exit(1);
    }
    else {
      wait(NULL);
      if((type==PLOTPOSTSCRIPT)) {
	remove(gplotname);
	remove(dataname);
      }
    }
  }
  
  free(gplotname);
  free(dataname);
  free(outputname);
  return;
}