std::vector<double> LaserSensorModel::RayTrace( Particle particle, SensorData data )
	{
		std::vector<double> zhat(numPoints); // initialize vector of probabilities-per-laser-beam

		PoseSE2 laserH = particle.getPose()*data.laserOffset; // find laser position in world
		
		double laserAngle = laserH.getTheta(); // get the angle of the laser in the world
		double scanAngle = data.StartAngle + laserAngle;  // get the starting scan angle (relative to the laser angle)
							
		for( unsigned int s = 0; s < numPoints; s++ ) { //data.ScanSize
			
			double prob = 1; // this is the starting probability that the laser beam passes through a cell
			
			double xL = laserH.getX(); // this is the x-starting position of the laser beam
			double yL = laserH.getY(); // this is the y-starting position of the laser beam

			double dX = raytraceStepsize * std::cos( scanAngle );
			double dY = raytraceStepsize * std::sin( scanAngle );
			scanAngle += laserSubsample * SensorData::ScanResolution;
			double r = 0;
			
			if( xL < 0 || xL >= map.GetXSize() - 1E-3 || yL < 0 || yL >= map.GetYSize() - 1E-3 ) {
				return std::vector<double>() ;
			}
			
			while( prob > raytraceThreshold ){
				
				xL += dX; // step along the laser ray
				yL += dY;
				r += raytraceStepsize;
				
				if( xL < 0 ) {
					xL = 0;
				}
				else if( xL >= map.GetXSize() - 1E-3 ) {
					xL = map.GetXSize() - 1;
				}
				
				if( yL < 0 ) {
					yL = 0;
				}
				else if( yL >= map.GetYSize() - 1E-3 ) {
					yL = map.GetYSize() - 1;
				}

				double mapVal = map.GetValue( xL, yL );

				prob = prob*mapVal; // check the map, & update the probability that the laser hit something

			} // end while-probability-of-ray-hasn't-hit-a-wall
			
// 			xL = xL-laserH.getX();
// 			yL = yL-laserH.getY();
// 			zhat[s] = std::sqrt( xL*xL + yL*yL ); // store the distance that the laser went on this scan before it hit something
			zhat[s] = r;
		} // end for-every-scan
		
		return zhat;
		
	} // end RayTrace method	
Example #2
0
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;

}
	/* main routine */
	int main(int argc, char *argv[]){

	FILELog::ReportingLevel() = logINFO;
	VARIABLES vars;
	vars = commandline_input(argc, argv);
	clock_t init, final;
		
	int number_of_particles = vars.num; 
	double timestep = vars.dt;
	bool use_T2 = vars.use_T2; // = false (no T2 decay), = true (T2 decay)
	int num_of_repeat = vars.num_of_repeat; //Number of times to repeat simulation. For every repeat all data is flushed and we start the simulation again.
	int number_of_timesteps; number_of_timesteps = (int)ceil(vars.gs/timestep); 	
	
	double permeability = 0.0;
	double radius = .00535;
	double d = sqrt( 2.0*PI*radius*radius/( sqrt(3.0)*.79 ) );
	//double lattice_size = 0.00601922026995422789215053328651;
	double grad_duration = 4.5;
	double D_extra = 2.5E-6;
	double D_intra = 1.0E-6;
	double T2_e = 200;
	double T2_i = 200;
		
	FILE_LOG(logINFO) << "f = " << 2.0*PI*radius*radius/(sqrt(3.0)*d*d) << std::endl;
		
	Vector3 xhat(1.0,0.0,0.0);
	Vector3 yhat(0.0,1.0,0.0);
	Vector3 zhat(0.0,0.0,1.0);		
		
	Lattice<Cylinder_XY> lattice(D_extra, T2_e, permeability);
	lattice.setLatticeVectors(d,2.0*d*0.86602540378443864676372317075294,d,xhat,yhat,zhat);
	lattice.addBasis(Cylinder_XY(d/2.0, 0.0,  radius,  T2_i, D_intra, 1));
	lattice.addBasis(Cylinder_XY(0.0, d*0.86602540378443864676372317075294,  radius,  T2_i, D_intra, 2));
	lattice.addBasis(Cylinder_XY(d/2.0, 2.0*d*0.86602540378443864676372317075294,  radius,  T2_i, D_intra, 3));
	lattice.addBasis(Cylinder_XY(d, d*0.86602540378443864676372317075294,  radius,  T2_i, D_intra, 4));
	


	double gspacings [] = { 8.0 , 10.5 , 14.0 , 18.5 , 24.5 , 32.5 , 42.5 , 56.5 };
	double bvals [] = {108780, 154720, 219040, 301730, 411980, 558990, 742420, 1000000 };
	double G [9];

	for (int kk = 0; kk < num_of_repeat; kk++) {

	vector<PGSE> measurements_x;
	vector<PGSE> measurements_y;
	vector<PGSE> measurements_z;

	for (int i = 0; i < 8; i++){
		double echo_time = 2.0*grad_duration + gspacings[i];
		G[0] = 0.0;
		G[i+1] = sqrt(bvals[i]/(GAMMA*GAMMA*grad_duration*grad_duration*(grad_duration + gspacings[i] - (grad_duration/3.0))));
		measurements_x.push_back(PGSE(grad_duration,gspacings[i], timestep, G[0], echo_time, number_of_particles, xhat));
		measurements_y.push_back(PGSE(grad_duration,gspacings[i], timestep, G[0], echo_time, number_of_particles, yhat));
		measurements_z.push_back(PGSE(grad_duration,gspacings[i], timestep, G[0], echo_time, number_of_particles, zhat));
		measurements_x.push_back(PGSE(grad_duration,gspacings[i], timestep, G[i+1], echo_time, number_of_particles, xhat));
		measurements_y.push_back(PGSE(grad_duration,gspacings[i], timestep, G[i+1], echo_time, number_of_particles, yhat));
		measurements_z.push_back(PGSE(grad_duration,gspacings[i], timestep, G[i+1], echo_time, number_of_particles, zhat));
	
	}
	
	vector<double> lnsignal(2);
	vector<double> b(2);
	

	
	cout << " trial = " << kk << endl;
		Particles ensemble(number_of_particles,timestep, use_T2);
		lattice.initializeUniformly(ensemble.getGenerator() , ensemble.getEnsemble() );
		
		for (int k = 0; k < measurements_x.size();k++){
			measurements_x[k].updatePhase(ensemble.getEnsemble(), 0.0);
			measurements_y[k].updatePhase(ensemble.getEnsemble(), 0.0);
			measurements_z[k].updatePhase(ensemble.getEnsemble(), 0.0);
		}
		
		for (int i = 1; i <= number_of_timesteps; i++){
			ensemble.updateposition(lattice);
			for (int k = 0; k < measurements_x.size();k++){
				measurements_x[k].updatePhase(ensemble.getEnsemble(), i*timestep);
				measurements_y[k].updatePhase(ensemble.getEnsemble(), i*timestep);
				measurements_z[k].updatePhase(ensemble.getEnsemble(), i*timestep);
			}
		}
		
		
		for (int i = 0; i < 8*2; i+=2){
			
			double ADCx, ADCy, ADCz, diff_time;
			
			lnsignal[0] = log(measurements_x[i].get_signal());
			b[0] = 	measurements_x[i].get_b();
			lnsignal[1] = log(measurements_x[i+1].get_signal());
			b[1] = 	measurements_x[i+1].get_b();
			ADCx = -1.0*linear_regression(lnsignal,b);
			
			//std::cout << b[0] << " " << lnsignal[0] << " " << b[1] << " " << lnsignal[1] << " ";
			lnsignal[0] = log(measurements_y[i].get_signal());
			b[0] = 	measurements_y[i].get_b();
			lnsignal[1] = log(measurements_y[i+1].get_signal());
			b[1] = 	measurements_y[i+1].get_b();
			ADCy = -1.0*linear_regression(lnsignal,b);
			//std::cout << b[0] << " " << lnsignal[0] << " " << b[1] << " " << lnsignal[1] << " ";
			lnsignal[0] = log(measurements_z[i].get_signal());
			b[0] = 	measurements_z[i].get_b();
			lnsignal[1] = log(measurements_z[i+1].get_signal());
			b[1] = 	measurements_z[i+1].get_b();
			ADCz = -1.0*linear_regression(lnsignal,b);
			//std::cout << b[0] << " " << lnsignal[0] << " " << b[1] << " " << lnsignal[1] << std::endl;
			
			diff_time = measurements_x[i].get_DT();
			std::cout << i << " " << diff_time << " " << ADCx << " " << ADCy << " " << ADCz << " " << b[1] << " " << G[1] << std::endl; 
			
		}
			
	}
		

	

		final= clock()-init; //final time - intial time