void TestVector::testCase1() { Vector<int, Alloc> myvec(8); assert(myvec.size() == 8); for (int i = 0; i < 8; i++) myvec[i] = i; myvec.push_back(9); myvec.push_back(10); myvec.insert(myvec.begin() + 8, 8); int i = 0; for (Vector<int>::constIterator it = myvec.begin(); it != myvec.end(); ++it, ++i) { assert(*it == i); } i--; for (Vector<int>::constReverseIterator it = myvec.crbegin(); it != myvec.crend(); ++it, --i) { assert(*it == i); } myvec.erase(myvec.end() - 3, myvec.end()); i = 0; for (Vector<int>::constIterator it = myvec.begin(); it != myvec.end(); ++it, ++i) { assert(*it == i); } std::cout << "case1 passed" << std::endl; }
/** * @brief Add elements to top and bottom of a vector * * This method is to expand a vector to add additional records for padding * purposes. The parameter ntop indicate the number to add to the top of the * vector. nbot indicates the number to add to the bottom * * Elements added to the top have the contents of the first element of the * input vector copied to it. Elements added to the bottom have the last * element copied to it. * * The new vector has the contents of the original copied to it place * immediately after the number of elements added to it. * * @author Kris Becker - 4/6/2011 * * @param ntop Number of elements to add to the top * @param vec number of elements to add to the bottom * @param vector Vector to add elements to * * @return SpiceSegment::SVector Expanded vector */ SpiceSegment::SVector SpiceSegment::expand(int ntop, int nbot, const SpiceSegment::SVector &vec) const { // Add lines to matrix at top and bottom int ndim(vec.dim1()); ntop = std::max(0, ntop); nbot = std::max(0, nbot); int nvals(ndim+ntop+nbot); SVector myvec(nvals); // Duplicate top elements to expanded elements for (int n = 0 ; n < ntop ; n++) { myvec[n] = vec[0]; } // Copy elements from input vector to output for (int n = 0 ; n < vec.dim1() ; n++) { myvec[n+ntop] = vec[n]; } // Duplicate bottom elements to expanded elements for (int n = 0 ; n < nbot ; n++) { myvec[nvals-1-n] = vec[ndim-1]; } return (myvec); }
// initialize opengl components event handler void MyGLWidget::initializeGL() { displayClass = new DisplayClass(); std::ifstream tracer; std::string ts; tracer.open(raytraceconfig); //output filename getline(tracer, ts); displayClass->rayOutputFile = new std::string(cut(ts)); //resolution getline(tracer, ts); glm::vec3 v = getVec(cut(ts)); displayClass->resoX = new float(v.x); displayClass->resoY = new float(v.y); displayClass->rayCamera = new Camera(); //eye getline(tracer, ts); displayClass->rayCamera->eye = getVec(cut(ts)); //view direction getline(tracer, ts); displayClass->rayCamera->center = getVec(cut(ts)); //up vec getline(tracer, ts); displayClass->rayCamera->up = getVec(cut(ts)); //fovy getline(tracer, ts); displayClass->rayCamera->fovy = getVec(cut(ts)).x; displayClass->rayLightPos = new std::vector<glm::vec3*>(); displayClass->rayLightCol = new std::vector<glm::vec3*>(); glm::vec3* lpos; glm::vec3* lcol; for (int lc = 0; lc < 3; lc++) { //lightpos getline(tracer, ts); lpos = new glm::vec3(getVec(cut(ts))); displayClass->rayLightPos->push_back(lpos); //lightcol getline(tracer, ts); lcol = new glm::vec3(getVec(cut(ts))); displayClass->rayLightCol->push_back(lcol); } //ambient color getline(tracer, ts); displayClass->rayAmbientCol = new glm::vec3(getVec(cut(ts))); *displayClass->rayAmbientCol = glm::clamp(*displayClass->rayAmbientCol, 0.0f, 1.0f); //mat1 getline(tracer, ts); ts = cut(ts); std::istringstream myStream = static_cast<std::istringstream>(ts); float tem; displayClass->mtl1 = new float[8]; int ml = 0; while (myStream >> tem) { displayClass->mtl1[ml] = tem; ml++; } //mat2 getline(tracer, ts); ts = cut(ts); myStream = static_cast<std::istringstream>(ts); displayClass->mtl2 = new float[8]; ml = 0; while (myStream >> tem) { displayClass->mtl2[ml] = tem; ml++; } //mat3 getline(tracer, ts); ts = cut(ts); myStream = static_cast<std::istringstream>(ts); displayClass->mtl3 = new float[8]; ml = 0; while (myStream >> tem) { displayClass->mtl3[ml] = tem; ml++; } displayClass->mtlf = new float[8]; displayClass->mtlf[0] = 0.5f; displayClass->mtlf[1] = 0.5f; displayClass->mtlf[2] = 0.0f; displayClass->mtlf[3] = 5.0f; displayClass->mtlf[4] = 0.0f; displayClass->mtlf[5] = 0.0f; displayClass->mtlf[6] = 0.0f; displayClass->mtlf[7] = 0.0f; tracer.close(); std::ifstream config; std::ifstream meshfile; config.open(filename); std::string reader; getline(config, reader); glm::vec3 vec = getVec(reader); int xSize = vec[0]; int zSize = vec[1]; int numItems = vec[2]; std::vector<std::vector<Node*>> grid; for (int fn = 0; fn < xSize; fn++) { std::vector<Node*> col; for (int fx = 0; fx < zSize; fx++) { col.push_back(NULL); } grid.push_back(col); } int i = 0; glm::vec4 origin(((float) xSize)/-2.0f, -0.1f, ((float) zSize)/-2.0f, 1.0f); displayClass->graph = new SceneGraph(); displayClass->floor = new Prism(1, xSize, 0.1f, -1 * zSize, origin); SceneGraph* sg = displayClass->graph; sg->rootNode = new Node(displayClass->floor, NULL, NULL, 0, 0); *sg->rootNode->mtl = 4; sg->rootNode->scale = new glm::mat4();//glm::scale(glm::mat4(), glm::vec3(xSize, 0.1f, zSize))); sg->rootNode->rotate = new glm::mat4();//glm::rotate(glm::mat4(), 0.1f, glm::vec3(0.0f, 1.0f, 0.0f))); sg->rootNode->translate = new glm::mat4(); Furniture* currentPrimitive = NULL; Node* par; glm::mat4 transl; glm::mat4 rot; glm::mat4 scal; Node* thisNode; int xi; float* yi; int zi; int mtl; std::string myName; std::string meshparse; while (i < numItems) { currentPrimitive = NULL; Mesh* myMesh = NULL; getline(config, reader); getline(config, reader); myName = reader; if (myName == "table") { getline(config, reader); mtl = getVec(reader).x; currentPrimitive = new Table(mtl); } else if (myName == "chair") { getline(config, reader); mtl = getVec(reader).x; currentPrimitive = new Chair(mtl); } else if (myName == "cabinet") { getline(config, reader); mtl = getVec(reader).x; currentPrimitive = new Cabinet(mtl); } else if (myName == "lamp") { getline(config, reader); mtl = getVec(reader).x; currentPrimitive = new Lamp(mtl); } else if (myName == "multitable") { getline(config, reader); mtl = getVec(reader).x; currentPrimitive = new Table(mtl); *currentPrimitive->localTransforms->at(currentPrimitive->localTransforms->size() - 1) *= glm::scale(glm::mat4(), glm::vec3(2.0f, 1.0f, 1.0f)); /*for (int n = 0; n < currentPrimitive->localTransforms->size(); n++) { *currentPrimitive->localTransforms->at(n) = glm::translate(glm::mat4(), glm::vec3(0.5f, 0.0f, 0.0f)) * *currentPrimitive->localTransforms->at(n); }*/ } else if (myName == "mesh") { getline(config, reader); meshfile.open(reader); getline(config, reader); mtl = getVec(reader).x; getline(meshfile, meshparse); if (meshparse == "extrusion") { getline(meshfile, meshparse); vec = getVec(meshparse); float len = vec.x; getline(meshfile, meshparse); vec = getVec(meshparse); int numVerts = vec.x; std::vector<glm::vec3> verts; for (int w = 0; w < numVerts; w++) { getline(meshfile, meshparse); vec = getVec(meshparse); glm::vec3 myvec(vec.x, 0.0f, vec.y); verts.push_back(myvec); } myMesh = new Mesh(mtl, len, numVerts, verts); } else if (meshparse == "surfrev") { getline(meshfile, meshparse); vec = getVec(meshparse); int numSlices = vec.x; getline(meshfile, meshparse); vec = getVec(meshparse); int numVerts = vec.x; std::vector<glm::vec4> verts; for (int w = 0; w < numVerts; w++) { getline(meshfile, meshparse); vec = getVec(meshparse); if (vec.x < 0) { std::cerr << "Error: negative x value in surfrev" << std::endl; exit(1); } glm::vec4 myvec(vec.x, vec.y, 0.0f, 1.0f); verts.push_back(myvec); } myMesh = new Mesh(mtl, numSlices, numVerts, verts); } meshfile.close(); } //colors //getline(config, reader); //translation getline(config, reader); vec = getVec(reader); xi = vec.x; yi = new float(0); zi = vec.y; //rotation getline(config, reader); vec = getVec(reader); rot = glm::rotate(glm::mat4(), vec.x, glm::vec3(0.0f, 1.0f, 0.0f)); //scale getline(config, reader); vec = getVec(reader); scal = glm::scale(glm::mat4(), vec); thisNode = new Node(NULL, currentPrimitive, NULL, xi, zi); *thisNode->mtl = mtl; if (myName == "mesh") { thisNode->shape = myMesh; if (*myMesh->kind == 99) { //*yi = *myMesh->height/2.0f; } else if (*myMesh->kind == 100) { //*yi = *myMesh->height/2.0f; } } par = sg->rootNode; if (grid.at(xi).at(zi) != NULL) { par = grid.at(xi).at(zi); if (par->furniture == NULL) { if (par != sg->rootNode) { *yi += *par->shape->height; } } else { *yi += *par->furniture->height; } } thisNode->parent = par; grid.at(xi).at(zi) = thisNode; if (myName == "multitable") { grid.at(xi + 1).at(zi) = thisNode; } //setPar(par, myPar, xi, yi, zi); transl = glm::translate(glm::mat4(), glm::vec3(xi + origin.x + 0.5, *yi, zi + origin.z + 0.5)); if (myName == "multitable") { transl = glm::translate(glm::mat4(), glm::vec3(0.5f, 0.0f, 0.0f)) * transl; } if (thisNode->furniture != NULL) { *thisNode->furniture->height *= scal[1][1]; } else { *thisNode->shape->height *= scal[1][1]; } if (thisNode->parent != displayClass->graph->rootNode) { if (thisNode->furniture == NULL) { if (thisNode->parent->furniture == NULL) { *thisNode->shape->height += *thisNode->parent->shape->height; } else { *thisNode->shape->height += *thisNode->parent->furniture->height; } } else { if (thisNode->parent->furniture == NULL) { *thisNode->furniture->height += *thisNode->parent->shape->height; } else { *thisNode->furniture->height += *thisNode->parent->furniture->height; } } } thisNode->rotate = new glm::mat4(rot);// * *par->rotate); thisNode->translate = new glm::mat4(transl); thisNode->scale = new glm::mat4(scal);// * *par->scale); //add to parent's list of children par->children->push_back(thisNode); i++; } displayClass->selectedNode = displayClass->graph->rootNode; }
int main(int argc, char* argv[]) { #ifdef HAVE_MPI MPI::Init(argc, argv); #endif RCP<MxComm> myComm = rcp(new MxComm()); #if 0 #ifdef HAVE_MPI MPI::Init(argc, argv); //MPI_Init(argc, argv); Epetra_MpiComm myComm(MPI_COMM_WORLD); #else Epetra_SerialComm myComm; #endif #endif // input file method #if 1 std::string inFile; Teuchos::CommandLineProcessor cmdp(false, true); cmdp.setOption("infile", &inFile, "XML format input file."); if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { return -1; } if (inFile == "") { std::cout << "Please specify an input file using --infile=your_file.mx\n"; exit(0); } // now read the input file with trilinos XML reader Teuchos::XMLObject xmlObj(Teuchos::FileInputSource(inFile).getObject()); // get simulation dimension int dim = atoi(MxUtil::XML::getAttr("dim", xmlObj).c_str()); if (dim < 1 or dim > 3) { std::cout << "Simulation dimension invalid or not given, using 3D.\n"; dim = 3; } // get simulation type std::string domain = MxUtil::XML::getAttr("domain", xmlObj).c_str(); if (domain != "frequency" and domain != "time") { std::cout << "Simulation domain invalid or not given, using frequency-domain.\n"; domain = "frequency"; } // create problem MxProblem<1> * prob1d; MxProblem<2> * prob2d; MxProblem<3> * prob3d; switch (dim) { case 1: prob1d = new MxProblem<1>(xmlObj, myComm); prob1d->solve(); delete prob1d; break; case 2: prob2d = new MxProblem<2>(xmlObj, myComm); prob2d->solve(); delete prob2d; break; case 3: prob3d = new MxProblem<3>(xmlObj, myComm); prob3d->solve(); delete prob3d; break; } #endif #if 0 // epetra stuff test MxMap map(10, 0, myComm); Epetra_CrsMatrix mat(Copy, map, 0); int ind = 2; double val = 0; mat.InsertGlobalValues(1, 1, &val, &ind); ind = 3; val = 4; mat.InsertGlobalValues(1, 1, &val, &ind); mat.FillComplete(map, map); Epetra_Vector myvec(map); myvec.Random(); std::cout << myvec; mat.Apply(myvec, myvec); std::cout << myvec; Epetra_CrsMatrix copy(mat); std::cout << mat; MxUtil::Epetra::stripZeros(mat); std::cout << mat; //throw 1; #endif typedef MxDimVector<double, 3> vecd3; typedef MxDimVector<int, 3> veci3; vecd3 midPt(0); #if 0 //std::cout << "Crab cavity setup:\n"; int crabNumCells = 4; double crabCellLen = 2.0 * 0.0192; //meters double crabCavRad = 0.04719; double crabIrisRad = 0.015; double crabCavRho = 0.0136; double crabIrisRho = 0.00331; int crabCellRes = 40; int padCells = 2; int cnx, cny, cnz; double clx, cly, clz; double cox, coy, coz; double crabDelta = crabCellLen / double(crabCellRes); cnz = crabNumCells * crabCellRes + 2 * padCells; clz = double(cnz) * crabDelta; coz = -0.5 * clz; cny = cnx = 2 * (int(ceil(crabCavRad / crabDelta)) + padCells); cly = clx = double(cnx) * crabDelta; coy = cox = -0.5 * clx; veci3 crabN; crabN[0] = cnx; crabN[1] = cny; crabN[2] = cnz; vecd3 crabL; crabL[0] = clx; crabL[1] = cly; crabL[2] = clz; vecd3 crabO; crabO[0] = cox; crabO[1] = coy; crabO[2] = coz; //crabN.print(); //crabL.print(); //crabO.print(); MxGrid<3> crabGrid(crabO, crabN, crabL, &myComm); crabGrid.print(); MxCrabCav crabCav(midPt, crabNumCells, crabCellLen, crabIrisRad, crabCavRad, crabIrisRho, crabCavRho); crabCav.save(crabGrid); Teuchos::ParameterList crabList; crabList.set("geo-mg : levels", 1); crabList.set("geo-mg : smoothers : sweeps", 5); crabList.set("amg : smoothers : sweeps", 1); crabList.set("amg : smoothers : type", "Chebyshev"); crabList.set("eigensolver : output", 2); crabList.set("eigensolver : nev", 15); crabList.set("eigensolver : tol", 1.e-8); crabList.set("eigensolver : block size", 2); crabList.set("eigensolver : num blocks", 30); crabList.set("eigensolver : spectrum", "LM"); crabList.set("wave operator : invert", true); crabList.set("wave operator : invert : tol", 1.e-10); //crabList.set("wave operator : invert : shift", 1000.0); crabList.set("wave operator : invert : max basis size", 40); MxEMSim<dim> crabSim; crabSim.setGrid(&crabGrid); crabSim.setPEC(&crabCav); //crabSim.setGrid(&sphGrid); //crabSim.setPEC(&ell); crabSim.setParameters(crabList); crabSim.setup(); MxSolver<dim> * solver; solver = new MxSolver<dim>(&crabSim, crabList); solver->solve(); delete solver; //return 1; #endif // optimized phc cavity #if 0 double rodRad = 0.003175; // meters const int numRods = 24; double rodx[numRods] = {0.0158406582694, 0.0551748491968, 0.0209567636489, 0.0384658321918, 0.00792032913471, 0.0338604938991, 0.00477355412058, 0.00485955186622, -0.00792032913471, -0.0213143552977, -0.0161832095283, -0.0336062803256, -0.0158406582694, -0.0551748491968, -0.0209567636489, -0.0384658321918, -0.00792032913471, -0.0338604938991, -0.00477355412058, -0.00485955186622, 0.00792032913471, 0.0213143552977, 0.0161832095283, 0.0336062803256}; double rody[numRods] = {0.0, -0.00724351649877, 0.006587367621, 0.0165969314144, 0.013718412474, 0.044161062805, 0.0214427735115, 0.041610853563, 0.013718412474, 0.0514045793038, 0.0148554058905, 0.0250139221487, 1.9399211446e-18, 0.00724351649877, -0.006587367621, -0.0165969314144, -0.013718412474, -0.044161062805, -0.0214427735115, -0.041610853563, -0.013718412474, -0.0514045793038, -0.0148554058905, -0.0250139221487}; std::vector<MxShape<3> *> rods; MxShapeUnion<3> rodsShape; vecd3 rodPos; vecd3 zhat(0); zhat[2] = 1.0; for (int i = 0; i < numRods; i++) { rodPos[0] = rodx[i]; rodPos[1] = rody[i]; rodPos[2] = 0.0; rods.push_back(new MxCylinder(rodPos, zhat, rodRad)); rodsShape.add(rods[i]); } MxDimMatrix<double, 3> sapphEps(0); sapphEps(0, 0) = 9.3; sapphEps(1, 1) = 9.3; sapphEps(2, 2) = 11.5; MxDielectric<3> phcDiel; phcDiel.add(&rodsShape, sapphEps); // conducting cavity double cavLen = 0.019624116824498831; double cavRad = 0.1; MxCylinder cavCyl(0, zhat, cavRad); MxSlab<3> cavCaps(0, zhat, cavLen); MxShapeIntersection<3> phcCav; phcCav.add(&cavCyl); phcCav.add(&cavCaps); // setup grid int rodDiaCells = 6; int pad = 2; double delta = 2.0 * rodRad / double(rodDiaCells); veci3 phcN; phcN[0] = phcN[1] = int(2.0 * cavRad / delta) + 2 * pad; phcN[2] = int(cavLen / delta) + 2 * pad; vecd3 phcL; phcL[0] = phcL[1] = delta * double(phcN[0]); phcL[2] = delta * double(phcN[2]); vecd3 phcO; phcO[0] = phcO[1] = -0.5 * phcL[0]; phcO[2] = -0.5 * phcL[2]; MxGrid<3> phcGrid(phcO, phcN, phcL, &myComm); phcGrid.print(); Teuchos::ParameterList phcList; phcList.set("geo-mg : levels", 1); phcList.set("geo-mg : smoothers : sweeps", 5); phcList.set("eigensolver : output", 2); phcList.set("eigensolver : nev", 15); phcList.set("eigensolver : tol", 1.e-8); phcList.set("eigensolver : block size", 1); phcList.set("eigensolver : num blocks", 30); phcList.set("eigensolver : spectrum", "LM"); phcList.set("wave operator : invert", true); phcList.set("wave operator : invert : tol", 1.e-8); //phcList.set("wave operator : invert : shift", 1000.0); phcList.set("wave operator : invert : max basis size", 40); MxEMSim<dim> phcSim; phcSim.setGrid(&phcGrid); //phcSim.setPEC(&phcCav); phcSim.setDielectric(&phcDiel); phcSim.setParameters(phcList); phcSim.setup(); MxSolver<dim> * solver; solver = new MxSolver<dim>(&phcSim, phcList); solver->solve(); delete solver; for (int i = 0; i < numRods; i++) delete rods[i]; #endif #if 0 double sphR = 0.37; int sphN = 64; MxEllipsoid ell(0.0, sphR); MxGrid<3> sphGrid(-0.5, sphN, 1.0, &myComm); sphGrid.print(); MxDimMatrix<double, 3> rotSapphEps(0); rotSapphEps(0, 0) = 10.225; rotSapphEps(1, 1) = 10.225; rotSapphEps(2, 2) = 9.95; rotSapphEps(0, 1) = rotSapphEps(1, 0) = -0.825; rotSapphEps(0, 2) = rotSapphEps(2, 0) = -0.67360967926537398; rotSapphEps(1, 2) = rotSapphEps(2, 1) = 0.67360967926537398; MxDielectric<3> phcDiel; phcDiel.add(&ell, rotSapphEps); vecd3 ell2Loc(0); ell2Loc[0] = 0.6; vecd3 ell3Loc(0); ell3Loc[0] = 0.3; ell3Loc[2] = 0.3; MxEllipsoid ell2(ell2Loc, sphR); MxEllipsoid ell3(ell3Loc, sphR); MxShapeUnion<3> shUnion; shUnion.add(&ell); shUnion.add(&ell2); shUnion.add(&ell3); //shUnion.save(sphGrid); MxShapeIntersection<3> shInt; shInt.add(&ell); shInt.add(&ell2); shInt.add(&ell3); //shInt.save(sphGrid); MxShapeSubtract<3> shSub; shSub.setBaseShape(&ell); shSub.subtractShape(&ell2); shSub.subtractShape(&ell3); //shSub.save(sphGrid); MxDielectric<3> dielEll; MxDimMatrix<double, 3> epsEll(vecd3(10.0)); // isotropic eps = 10 dielEll.add(&ell, epsEll); Teuchos::ParameterList sphList; sphList.set("geo-mg : levels", 1); sphList.set("geo-mg : smoothers : sweeps", 4); sphList.set("eigensolver : output", 2); sphList.set("eigensolver : nev", 12); sphList.set("eigensolver : tol", 1.e-8); sphList.set("eigensolver : block size", 1); sphList.set("eigensolver : num blocks", 30); sphList.set("eigensolver : spectrum", "LM"); sphList.set("wave operator : invert", true); sphList.set("wave operator : invert : tol", 1.e-8); //sphList.set("wave operator : invert : shift", -0.1); sphList.set("wave operator : invert : shift", 1.0); sphList.set("wave operator : invert : max basis size", 40); MxEMSim<dim> sphSim; sphSim.setGrid(&sphGrid); //sphSim.setDielectric(&dielEll); sphSim.setDielectric(&phcDiel); //sphSim.setPEC(&sphCav); //sphSim.setPEC(&ell); sphSim.setParameters(sphList); sphSim.setup(); MxSolver<dim> * solver; solver = new MxSolver<dim>(&sphSim, sphList); solver->solve(); delete solver; #endif #ifdef HAVE_MPI MPI::Finalize(); //MPI_Finalize(); #endif return 0; }
//#include <vector> void foo3() { std::vector<int> myvec(1); for (auto itr = myvec.cbegin(); itr != myvec.cend(); ++itr) { } }