void forkView3D(int typeOfViewport) { viewManager *viewport; int childPID, code; int i; view3DStruct doView3D; int pipe0[2],pipe1[2]; int *anIndex; char envAXIOM[100],runView[100]; int j,k; LLPoint *anLLPoint; LPoint *anLPoint; #ifdef DEBUG fprintf(stderr,"Pipe calls for 3D\n"); #endif check(pipe(pipe0)); check(pipe(pipe1)); #ifdef DEBUG fprintf(stderr,"Fork routine for 3D\n"); #endif switch(childPID = check(fork())) { case -1: printf("Cannot create a new process - you probably have too many things running already.\n"); return; case 0: /***************************** * child process * *****************************/ /* map pipes from viewport manager to standard input and output */ #ifdef DEBUG fprintf(stderr,"Mapping pipes to standard I/O in 3D\n"); #endif check(dup2(pipe0[0],0)); check(dup2(pipe1[1],1)); close(pipe0[0]); close(pipe0[1]); close(pipe1[0]); close(pipe1[1]); #ifdef DEBUG fprintf(stderr,"Executing ThreeDimensionalViewport process\n"); #endif sprintf(envAXIOM,"%s",getenv("AXIOM")); sprintf(runView,"%s%s",envAXIOM,"/lib/view3D"); check(execl(runView,runView,NULL)); fprintf(stderr,"The viewport manager could not execute view3D.\nCheck that view3D is on your PATH.\n"); exit(-1); default: /****************************** * parent process * ******************************/ if (!(viewport = (viewManager *)malloc(sizeof(viewManager)))) { printf("Ran out of memory trying to create a new viewport process.\n"); return; } viewport->viewType = typeOfViewport; viewport->PID = childPID; /* set up pipes to child process */ close(pipe0[0]); close(pipe1[1]); viewport->viewIn = pipe1[0]; viewport->viewOut = pipe0[1]; /* add new viewport to global list */ viewport->nextViewport = viewports; viewports = viewport; if (viewport->viewIn <0) { fprintf(stderr, "The viewport manager could not create connection to a 3D viewport window. Try again.\n"); return; } else { code = readViewport(viewport,&acknow,intSize); if (code < 0) { fprintf(stderr, "The viewport manager could not read from a 3D viewport window\ncode=%d\nack=%d\n",code,acknow); return; } } makeView3DFromSpadData(&doView3D,typeOfViewport); /* tell the child that parent is a viewport manager */ i = no; write(viewport->viewOut,&i,sizeof(int)); write(viewport->viewOut,&doView3D,sizeof(view3DStruct)); i = strlen(doView3D.title)+1; write(viewport->viewOut,&i,intSize); /* tell the length of the title to child */ write(viewport->viewOut,doView3D.title,i); /* tell the title to the child */ write(viewport->viewOut,&(doView3D.lightVec[0]),floatSize); write(viewport->viewOut,&(doView3D.lightVec[1]),floatSize); write(viewport->viewOut,&(doView3D.lightVec[2]),floatSize); /* send generalized 3D components */ write(viewport->viewOut,&(doView3D.numOfPoints),intSize); for (i=0; i<doView3D.numOfPoints; i++) { write(viewport->viewOut,&(refPt(doView3D,i)->x),floatSize); write(viewport->viewOut,&(refPt(doView3D,i)->y),floatSize); write(viewport->viewOut,&(refPt(doView3D,i)->z),floatSize); write(viewport->viewOut,&(refPt(doView3D,i)->c),floatSize); } write(viewport->viewOut,&(doView3D.lllp.numOfComponents),intSize); anLLPoint = doView3D.lllp.llp; for (i=0; i<doView3D.lllp.numOfComponents; i++,anLLPoint++) { write(viewport->viewOut,&(anLLPoint->prop.closed),intSize); write(viewport->viewOut,&(anLLPoint->prop.solid),intSize); write(viewport->viewOut,&(anLLPoint->numOfLists),intSize); anLPoint = anLLPoint->lp; for (j=0; j<anLLPoint->numOfLists; j++,anLPoint++) { write(viewport->viewOut,&(anLPoint->prop.closed),intSize); write(viewport->viewOut,&(anLPoint->prop.solid),intSize); write(viewport->viewOut,&(anLPoint->numOfPoints),intSize); anIndex = anLPoint->indices; for (k=0; k<anLPoint->numOfPoints; k++,anIndex++) write(viewport->viewOut,anIndex,intSize); } /* for LPoints in LLPoints (j) */ } /* for LLPoints in LLLPoints (i) */ /*** get acknowledge from viewport */ code = readViewport(viewport,&(viewport->viewWindow),sizeof(Window)); sleep(1); /* wait a second...*/ send_int(spadSock,viewport->PID); /* acknowledge to spad */ } /* switch */ } /* forkView3D() */
void spoonView3D(int type) { int i,j,k,code,pipe0[2],pipe1[2]; char * env_fricas; char * run_view; LLPoint *anLLPoint; LPoint *anLPoint; int *anIndex; fricas_sprintf_to_buf1(errorStr, "%s", "creating pipes"); check(pipe(pipe0)); check(pipe(pipe1)); switch(fork()) { case -1: fprintf(stderr,"can't create a child process\n"); fprintf(stderr,"you may have too many processes running\n"); exit(-1); case 0: /* Child */ fricas_sprintf_to_buf1(errorStr, "%s", "(viewAlone) mapping of pipes to standard I/O for view3D"); check(dup2(pipe0[0],0)); check(dup2(pipe1[1],1)); close(pipe0[0]); close(pipe0[1]); close(pipe1[0]); close(pipe1[1]); fricas_sprintf_to_buf1(errorStr, "%s", "(viewAlone) execution of the ThreeDimensionalViewport process"); env_fricas = getenv("AXIOM"); { size_t env_fricas_len = strlen(env_fricas); if (env_fricas_len > 20000) { fprintf(stderr, "AXIOM env var too long\n"); exit(-1); } run_view = malloc(env_fricas_len + strlen("/lib/view3D") + 1); if(!run_view) { fprintf(stderr, "(viewAlone) out of memory\n"); exit(-1); } strcpy(run_view, env_fricas); strcat(run_view, "/lib/view3D"); } check(execl(run_view, run_view, NULL)); fprintf(stderr,"Could not execute view3D!\n"); exit(-1); default: /* Parent */ viewP.viewType = type; /* set up pipes to child process */ close(pipe0[0]); close(pipe1[1]); viewP.viewIn = pipe1[0]; viewP.viewOut = pipe0[1]; if (viewP.viewIn <0) { fprintf(stderr, "can't set up pipes to viewport process. Try again.\n"); return; } else { code = read(viewP.viewIn,&ack,intSize); if (code < 0) { fprintf(stderr,"can't read from viewport process pipe. Try again.\n"); return; } } makeView3DFromFileData(type); /* tell child it is to be a stand alone program */ i = yes; fprintf(stderr," Transmitting data to viewport...\n"); write(viewP.viewOut,&i,intSize); write(viewP.viewOut,&doView3D,sizeof(view3DStruct)); i = strlen(doView3D.title)+1; write(viewP.viewOut,&i,intSize); /* tell the length of the title to child */ write(viewP.viewOut,doView3D.title,i); /* tell the title to the child, child */ write(viewP.viewOut,&(doView3D.lightVec[0]),floatSize); write(viewP.viewOut,&(doView3D.lightVec[1]),floatSize); write(viewP.viewOut,&(doView3D.lightVec[2]),floatSize); write(viewP.viewOut,&(doView3D.numOfPoints),intSize); for (i=0; i<doView3D.numOfPoints; i++) { write(viewP.viewOut,&(refPt(doView3D,i)->x),floatSize); write(viewP.viewOut,&(refPt(doView3D,i)->y),floatSize); write(viewP.viewOut,&(refPt(doView3D,i)->z),floatSize); write(viewP.viewOut,&(refPt(doView3D,i)->c),floatSize); } /* send generalized 3D components */ write(viewP.viewOut,&(doView3D.lllp.numOfComponents),intSize); anLLPoint = doView3D.lllp.llp; for (i=0; i<doView3D.lllp.numOfComponents; i++,anLLPoint++) { write(viewP.viewOut,&(anLLPoint->prop.closed),intSize); write(viewP.viewOut,&(anLLPoint->prop.solid),intSize); write(viewP.viewOut,&(anLLPoint->numOfLists),intSize); anLPoint = anLLPoint->lp; for (j=0; j<anLLPoint->numOfLists; j++,anLPoint++) { write(viewP.viewOut,&(anLPoint->prop.closed),intSize); write(viewP.viewOut,&(anLPoint->prop.solid),intSize); write(viewP.viewOut,&(anLPoint->numOfPoints),intSize); anIndex = anLPoint->indices; for (k=0; k<anLPoint->numOfPoints; k++,anIndex++) write(viewP.viewOut,anIndex,intSize); } /* for LPoints in LLPoints (j) */ } /* for LLPoints in LLLPoints (i) */ fprintf(stderr," Done.\n"); /*** get acknowledge from viewport */ code = read(viewP.viewIn,&(viewP.viewWindow),sizeof(Window)); sleep(1); /* wait a second...*/ exit(0); } /* switch */ } /* spoonView3D() */
bool AToCDensitySample() { /* We will do our linear algebra using Epetra */ VectorType<double> vecType = new EpetraVectorType(); /* Create a mesh. It will be of type BasisSimplicialMesh, and will * be built using a PartitionedLineMesher. */ MeshType meshType = new BasicSimplicialMeshType(); MeshSource mesher = new PartitionedRectangleMesher(-1.0, 1.0, 32, 1, -1.0, 1.0, 32, 1, meshType); Mesh mesh = mesher.getMesh(); /* Create a cell filter that will identify the maximal cells * in the interior of the domain */ CellFilter interior = new MaximalCellFilter(); Expr x = new CoordExpr(0); Expr y = new CoordExpr(1); BasisFamily L1 = new Lagrange(1); DiscreteSpace discSpace(mesh, List(L1, L1), vecType); /* Discretize some expression for the force. We'll pick a linear function * so that it can be interpolated exactly, letting us check the * validity of our interpolations. */ L2Projector proj(discSpace, List(x, y)); Expr F = proj.project(); /* create a sampler */ cout << "making grid" << std::endl; AToCPointLocator locator(mesh, interior, createVector(tuple(200, 200))); AToCDensitySampler sampler(locator, vecType); CToAInterpolator forceInterpolator(locator, F); cout << "making points" << std::endl; /* create a bunch of particles */ int nCells = mesh.numCells(2); int nPts = 15000; Array<double> pos(2*nPts); Array<double> f(F.size() * nPts); Array<Point> physPts; /* We'll generate random sample points in a way that lets us make an exact check * of the density recovery. We pick random cells, then random local coordinates * within each cell. This way, we can compute the density exactly as we * go, giving us something to check the recovered density against. */ Array<int> counts(nCells); for (int i=0; i<nPts; i++) { /* pick a random cell */ int cell = (int) floor(nCells * drand48()); counts[cell]++; /* generate a point in local coordinates */ double s = drand48(); double t = drand48() * (1.0-s); Point refPt(s, t); /* map to physical coordinates */ mesh.pushForward(2, tuple(cell), tuple(refPt), physPts); Point X = physPts[0]; pos[2*i] = X[0]; pos[2*i+1] = X[1]; } cout << "sampling..." << std::endl; Expr density = sampler.sample(createVector(pos), 1.0); cout << "computing forces..." << std::endl; forceInterpolator.interpolate(pos, f); double maxForceErr = 0.0; for (int i=0; i<nPts; i++) { double x0 = pos[2*i]; double y0 = pos[2*i+1]; double fx = x0; double fy = y0; double df = ::fabs(fx - f[2*i]) + ::fabs(fy - f[2*i+1]); maxForceErr = max(maxForceErr, df); } cout << "max force error = " << maxForceErr << std::endl; cout << "writing..." << std::endl; /* Write the field in VTK format */ FieldWriter w = new VTKWriter("Density2d"); w.addMesh(mesh); w.addField("rho", new ExprFieldWrapper(density)); w.write(); double errorSq = 0.0; double tol = 1.0e-6; return SundanceGlobal::passFailTest(::sqrt(errorSq), tol); }