Example #1
0
Data         *
QpGenSparseSeq::makeData( double    c_[],
                          int    krowQ[],  int  jcolQ[],  double dQ[],
                          double  xlow_[],  char ixlow_[],
                          double  xupp_[],  char ixupp_[],
                          int    krowA[],
                          int    jcolA[],  double dA[],
                          double    b_[],
                          int    krowC[],
                          int    jcolC[],  double dC[],
                          double  clow_[],  char  iclow_[],
                          double  cupp_[],  char  icupp_[] )
{
    // Objective funcition
    SimpleVectorHandle c( new SimpleVector( c_, nx ) );

    nnzQ = krowQ[nx];
    SparseSymMatrixHandle Q( new SparseSymMatrix( nx, nnzQ,
                             krowQ, jcolQ, dQ ) );

    // Bounds on variables
    SimpleVectorHandle xlow( new SimpleVector( xlow_, nx ) );
    SimpleVectorHandle ixlow( new SimpleVector( nx ) );
    ixlow->copyFromArray( ixlow_ );

    SimpleVectorHandle xupp( new SimpleVector( xupp_, nx ) );
    SimpleVectorHandle ixupp( new SimpleVector( nx ) );
    ixupp->copyFromArray( ixupp_ );

    // Equality constraints
    nnzA = 0;
    if( my > 0 )    nnzA = krowA[my];
    SparseGenMatrixHandle A( new SparseGenMatrix( my, nx,
                             nnzA, krowA, jcolA, dA ) );

    SimpleVectorHandle b( new SimpleVector( b_, my ) );

    // Inequality constraints
    nnzC = 0;
    if( mz > 0 )    nnzC = krowC[mz];
    SparseGenMatrixHandle C( new SparseGenMatrix( mz, nx,
                             nnzC, krowC, jcolC, dC ) );

    SimpleVectorHandle clow( new SimpleVector( clow_, mz ) );
    SimpleVectorHandle iclow( new SimpleVector( mz ) );
    iclow->copyFromArray( iclow_ );

    SimpleVectorHandle cupp( new SimpleVector( cupp_, mz ) );
    SimpleVectorHandle icupp( new SimpleVector( mz ) );
    icupp->copyFromArray( icupp_ );

    QpGenData *
    data = new QpGenData( SparseLinearAlgebraPackage::soleInstance(),
                          c, Q, xlow, ixlow, xupp, ixupp,
                          A, b,
                          C, clow, iclow, cupp, icupp );

    return data;
}
int main(int argc, char* argv[]) {

   if (argc < 2) {
      std::cout << "Usage: simpleBD h" << std::endl;
      exit(-1);
   }

   const unsigned int num_particles = 1;
   const double h_in = atof(argv[1]);


   const double end_time = 1.0;
   //const double dt = 0.0001;
   const double dt = h_in*h_in/PI;

   /*
    * Create Compartments (a structured grid from r = (0,0,0) to r = (1,0.1,0.1). resolution = h = h_in for all dimensions)
    */
   StructuredGrid grid(Vect3d(0.25-h_in,0,0), Vect3d(0.75+h_in,1,1), Vect3d(h_in,h_in,h_in));

   /*
    * Create "red" species with D=1. Fill with a uniform distribution of particles
    */
   Species red(1, grid);


   /*
    * create diffusion operator and apply to red species
    */
   Diffusion bd; bd.add_species(red);

   /*
    * Create simulation boundary planes
    */
   xplane xlow(0,1),xhigh(1,-1);
   yplane ylow(0,1),yhigh(1,-1);
   zplane zlow(0,1),zhigh(1,-1);
   xplane couple_C_to_M_1(0.25,-1);
   xplane couple_C_to_M_2(0.75,1);

   xplane first_line_of_compartments(0.25-0.5*h_in,1);
   xplane second_line_of_compartments(0.25+0.5*h_in,1);
   xplane second_last_line_of_compartments(0.75-0.5*h_in,1);
   xplane last_line_of_compartments(0.75+0.5*h_in,1);



   /*
    * Create jump (periodic) boundaries
    */
   JumpBoundaryWithCorrection<xplane> jb1(xlow,xhigh-xlow); jb1.add_species(red);
   JumpBoundary<yplane> jb2(ylow,yhigh-ylow); jb2.add_species(red);
   JumpBoundary<zplane> jb3(zlow,zhigh-zlow); jb3.add_species(red);
   JumpBoundary<yplane> jb4(yhigh,ylow-yhigh); jb4.add_species(red);
   JumpBoundary<zplane> jb5(zhigh,zlow-zhigh); jb5.add_species(red);

   /*
    * Create reflective boundary
    */
   ReflectiveBoundary<xplane> rb(xhigh); rb.add_species(red);

   /*
    * create Next Subvolume Method operator. The constructor takes the compartment grid as input
    */
   NextSubvolumeMethod nsm(red.grid);

   // add diffusion equations to nsm
   //nsm.add_diffusion(red);
   nsm.add_diffusion(red, red.D/pow(h_in,2));


   /*
    * Setup reactions for coupling
    */

//   nsm.remove_diffusion_between(red, second_line_of_compartments, first_line_of_compartments);
//   nsm.remove_diffusion_between(red, second_last_line_of_compartments, last_line_of_compartments);
//   nsm.add_diffusion_between(red, (2.0*h_in/sqrt(PI*red.D*dt))*(red.D/pow(h_in,2)), second_line_of_compartments, first_line_of_compartments);
//   nsm.add_diffusion_between(red, (2.0*h_in/sqrt(PI*red.D*dt))*(red.D/pow(h_in,2)), second_last_line_of_compartments, last_line_of_compartments);

   nsm.add_diffusion_between(red,red.D/pow(h_in,2),ylow, yhigh);
   nsm.add_diffusion_between(red,red.D/pow(h_in,2),yhigh, ylow);
   nsm.add_diffusion_between(red,red.D/pow(h_in,2),zlow, zhigh);
   nsm.add_diffusion_between(red,red.D/pow(h_in,2),zhigh, zlow);

//   nsm.clear_reactions(first_line_of_compartments);
//   nsm.clear_reactions(last_line_of_compartments);

   nsm.list_reactions();



   /*
    * Create coupling boundaries between compartments and free-space
    */
   Intersection<xplane,xplane> compartment_volume(couple_C_to_M_1, couple_C_to_M_2);
   CouplingBoundary_C_to_M<Intersection<xplane,xplane> > c_to_m(compartment_volume, nsm); c_to_m.add_species(red, dt);
   CouplingBoundary_M_to_C<Intersection<xplane,xplane> > m_to_c(compartment_volume, nsm); m_to_c.add_species(red);


   std::vector<int> bins_m, bins_c;
   const int num_bins = 100;
   bins_m.assign(num_bins,0);
   bins_c.assign(red.grid.get_cells_along_axes()[0],0);

   for (int i = 0; i < 50000; ++i) {
	   std::cout << "doing iteration " << i << std::endl;
	   red.fill_uniform(Vect3d(0,0,0),Vect3d(1.0,1,1),num_particles);
	   // calculate next reaction times for all compartments
	   nsm.reset_all_priorities();

	   /*
	    * Run simulation until end_time with timestep dt
	    */
	   run(red, end_time, dt,  nsm, bd, jb1, jb2, jb3, jb4, jb5, rb,  m_to_c, c_to_m);

	   if (red.mols.size() > 0) {
		   const double scaled_position = (red.mols.r[0][0])*num_bins;
		   if ((scaled_position < 0) || (scaled_position > num_bins)) {
			   printf("outside area: position = (%f %f %f)\n",red.mols.r[0][0],red.mols.r[0][1],red.mols.r[0][2]);
		   }
		   const int index = int(scaled_position);
		   bins_m[index]++;
		   red.mols.delete_molecule(0);
	   }

	   const int nc = red.grid.size();
	   for(int i = 0; i < nc; i++) {
		   Vect3i cell_indices = red.grid.get_cell_indicies(i);
		   bins_c[cell_indices[0]] += red.copy_numbers[i];
		   red.copy_numbers[i] = 0;
	   }
   }




   std::ofstream f;
   f.open(("output/simpleBD_couple_single_mols_"+std::string(argv[1])+".dat").c_str());
   f << end_time << ' ';
   int count = 0;
   BOOST_FOREACH(int x,bins_m) {
      f << x <<' ';
      count += x;
   }
int main(int argc, char* argv[]) {

	if (argc < 2) {
		std::cout << "Usage: simpleReactWithJumpCorrection num_particles" << std::endl;
		exit(-1);
	}

	const unsigned int num_particles = atoi(argv[1]);

	std::ofstream tf;
	tf.open(("time_simpleReactWithJumpCorrection_"+std::string(argv[1])+".dat").c_str());
	boost::progress_timer t(tf);

	const double end_time = 5.0;
	const double dt = 0.001;

	/*
	 * Create "red" species with D=1
	 */
	Species red(1);
	//red.fill_uniform(0.0,1.0,1);

	/*
	 * create diffusion operator and apply to red species
	 */
	Diffusion bd; bd.add_species(red);

	/*
	 * Create simulation boundary planes
	 */
	AxisAlignedPlane<0> xlow(0,1);
	AxisAlignedPlane<0> xhigh(1,-1);
	AxisAlignedPlane<1> ylow(0,1),yhigh(1,-1);
	AxisAlignedPlane<2> zlow(0,1),zhigh(1,-1);

	/*
	 * Create input flux (0 -> red) boundary
	 */
	FluxBoundary fb(Vect3d(1,0,0),Vect3d(0,1,0),Vect3d(0,0,1),num_particles); fb.add_species(red);

	/*
	 * Create jump (periodic) boundaries
	 */
	JumpBoundaryWithCorrection<AxisAlignedPlane<0> > jb1(xlow,Vect3d(1,0,0)); jb1.add_species(red);
	JumpBoundary<AxisAlignedPlane<1> > jb2(ylow,Vect3d(0,1,0)); jb2.add_species(red);
	JumpBoundary<AxisAlignedPlane<2> > jb3(zlow,Vect3d(0,0,1)); jb3.add_species(red);
	JumpBoundary<AxisAlignedPlane<1> > jb4(yhigh,Vect3d(0,-1,0)); jb4.add_species(red);
	JumpBoundary<AxisAlignedPlane<2> > jb5(zhigh,Vect3d(0,0,-1)); jb5.add_species(red);

	/*
	 * Create reflective boundary
	 */
	ReflectiveBoundary<AxisAlignedPlane<0> > rb(xhigh); rb.add_species(red);

	/*
	 * Create unimolecular reaction operator (red -> 0)
	 */
	UniMolecularReaction dr(1,red >> 0);

	const double iterations = end_time/dt;
	const double num_out = 100;
	const int it_per_out = int(iterations/num_out);

	/*
	 * Run simulation until end_time with timestep dt
	 */
	run(red, end_time, dt, bd, fb, jb1, jb2, jb3, jb4, jb5, rb, dr);

	std::vector<int> bins;
	make_histogram(bins,red,100, 0,1);

	std::ofstream f;
	f.open(("simpleReactWithJumpCorrection_"+std::string(argv[1])+".dat").c_str());
	f << end_time << ' ';
	BOOST_FOREACH(int x,bins) {
		f << x <<' ';
	}
	f << std::endl;
	f.close();


	return EXIT_SUCCESS;
}
Example #4
0
int main(int argc, char* argv[]) {

   if (argc < 3) {
      std::cout << "Usage: simpleBD num_particles h" << std::endl;
      exit(-1);
   }

   const unsigned int num_particles = atoi(argv[1]);
   const double h_in = atof(argv[2]);

   std::ofstream tf;
   tf.open(("output/time_simpleBD_couple_"+std::string(argv[1])+"_"+std::string(argv[2])+".dat").c_str());
   boost::progress_timer t(tf);

   const double end_time = 1.0;
   //const double dt = 0.0001;
   const double dt = h_in*h_in/PI;

   /*
    * Create Compartments (a structured grid from r = (0,0,0) to r = (1,0.1,0.1). resolution = h = h_in for all dimensions)
    */
   StructuredGrid grid(Vect3d(0.25-h_in,0,0), Vect3d(0.75+h_in,1,1), Vect3d(h_in,h_in,h_in));

   /*
    * Create "red" species with D=1. Fill with a uniform distribution of particles
    */
   Species red(1, grid);
   red.fill_uniform(Vect3d(0,0,0),Vect3d(0.25,1,1),num_particles/4.0);
   red.fill_uniform(Vect3d(0.75,0,0),Vect3d(1.0,1,1),num_particles/4.0);
   //red.fill_uniform(Vect3d(0.75,0,0),Vect3d(1.0,0.1,0.1),1);
   red.fill_uniform(num_particles/2.0);

   /*
    * create diffusion operator and apply to red species
    */
   Diffusion bd; bd.add_species(red);

   /*
    * Create simulation boundary planes
    */
   xplane xlow(0,1),xhigh(1,-1);
   yplane ylow(0,1),yhigh(1,-1);
   zplane zlow(0,1),zhigh(1,-1);
   xplane couple_C_to_M_1(0.25,-1);
   xplane couple_C_to_M_2(0.75,1);

   xplane first_line_of_compartments(0.25-0.5*h_in,1);
   xplane second_line_of_compartments(0.25+0.5*h_in,1);
   xplane second_last_line_of_compartments(0.75-0.5*h_in,1);
   xplane last_line_of_compartments(0.75+0.5*h_in,1);



   /*
    * Create jump (periodic) boundaries
    */
   JumpBoundaryWithCorrection<xplane> jb1(xlow,xhigh-xlow); jb1.add_species(red);
   JumpBoundary<yplane> jb2(ylow,yhigh-ylow); jb2.add_species(red);
   JumpBoundary<zplane> jb3(zlow,zhigh-zlow); jb3.add_species(red);
   JumpBoundary<yplane> jb4(yhigh,ylow-yhigh); jb4.add_species(red);
   JumpBoundary<zplane> jb5(zhigh,zlow-zhigh); jb5.add_species(red);

   /*
    * Create reflective boundary
    */
   ReflectiveBoundary<xplane> rb(xhigh); rb.add_species(red);

   /*
    * create Next Subvolume Method operator. The constructor takes the compartment grid as input
    */
   NextSubvolumeMethod nsm(red.grid);

   // add diffusion equations to nsm
   //nsm.add_diffusion(red);
   nsm.add_diffusion(red, red.D/pow(h_in,2));


   /*
    * Setup reactions for coupling
    */

//   nsm.remove_diffusion_between(red, second_line_of_compartments, first_line_of_compartments);
//   nsm.remove_diffusion_between(red, second_last_line_of_compartments, last_line_of_compartments);
//   nsm.add_diffusion_between(red, (2.0*h_in/sqrt(PI*red.D*dt))*(red.D/pow(h_in,2)), second_line_of_compartments, first_line_of_compartments);
//   nsm.add_diffusion_between(red, (2.0*h_in/sqrt(PI*red.D*dt))*(red.D/pow(h_in,2)), second_last_line_of_compartments, last_line_of_compartments);

   nsm.add_diffusion_between(red,red.D/pow(h_in,2),ylow, yhigh);
   nsm.add_diffusion_between(red,red.D/pow(h_in,2),yhigh, ylow);
   nsm.add_diffusion_between(red,red.D/pow(h_in,2),zlow, zhigh);
   nsm.add_diffusion_between(red,red.D/pow(h_in,2),zhigh, zlow);

//   nsm.clear_reactions(first_line_of_compartments);
//   nsm.clear_reactions(last_line_of_compartments);

   nsm.list_reactions();

   // calculate next reaction times for all compartments
   nsm.reset_all_priorities();

   /*
    * Create coupling boundaries between compartments and free-space
    */
   Intersection<xplane,xplane> compartment_volume(couple_C_to_M_1, couple_C_to_M_2);
   CouplingBoundary_C_to_M<Intersection<xplane,xplane> > c_to_m(compartment_volume, nsm); c_to_m.add_species(red, dt);
   CouplingBoundary_M_to_C<Intersection<xplane,xplane> > m_to_c(compartment_volume, nsm); m_to_c.add_species(red);


   /*
    * Run simulation until end_time with timestep dt
    */
   run(red, end_time, dt,  nsm, bd, jb1, jb2, jb3, jb4, jb5, rb,  m_to_c, c_to_m);
   //nsm(dt/2);
   //run(red, end_time, dt, nsm, c_to_m, bd, jb1, jb2, jb3, jb4, jb5, rb,  m_to_c, output);

   std::vector<int> bins;
   make_histogram(bins,red,100, 0,1);

   std::ofstream f;
   f.open(("output/simpleBD_couple_mols_"+std::string(argv[1])+"_"+std::string(argv[2])+".dat").c_str());
   f << end_time << ' ';
   int count = 0;
   BOOST_FOREACH(int x,bins) {
      f << x <<' ';
      count += x;
   }
Example #5
0
int main(int argc, char* argv[]) {

	if (argc < 2) {
		std::cout << "Usage: simpleReact num_particles" << std::endl;
		exit(-1);
	}

	const unsigned int num_particles = atoi(argv[1]);

	std::ofstream tf;
	tf.open(("output/time_simpleReact_"+std::string(argv[1])+".dat").c_str());
	boost::progress_timer t(tf);

	const double end_time = 5.0;
	const double dt = 0.001;

	/*
	 * Create "red" species with D=1
	 */
	Species red(0.1);
	//red.fill_uniform(0.0,1.0,1);

	/*
	 * create diffusion operator and apply to red species
	 */
	Diffusion bd; bd.add_species(red);

	/*
	 * Create simulation boundary planes
	 */
	AxisAlignedPlane<0> xlow(0,1);
	AxisAlignedPlane<0> xhigh(1,-1);
	AxisAlignedPlane<1> ylow(0,1),yhigh(1,-1);
	AxisAlignedPlane<2> zlow(0,1),zhigh(1,-1);

	/*
	 * Create input flux boundary
	 */
	FluxBoundary fb(Vect3d(1,0,0),Vect3d(0,1,0),Vect3d(0,0,1),num_particles); fb.add_species(red);

	/*
	 * Create jump (periodic) boundaries
	 */
	//JumpBoundary<AxisAlignedPlane<0> > jb1(xlow,Vect3d(1,0,0)); jb1.add_species(red);
	JumpBoundary<AxisAlignedPlane<1> > jb2(ylow,Vect3d(0,1,0)); jb2.add_species(red);
	JumpBoundary<AxisAlignedPlane<2> > jb3(zlow,Vect3d(0,0,1)); jb3.add_species(red);
	JumpBoundary<AxisAlignedPlane<1> > jb4(yhigh,Vect3d(0,-1,0)); jb4.add_species(red);
	JumpBoundary<AxisAlignedPlane<2> > jb5(zhigh,Vect3d(0,0,-1)); jb5.add_species(red);

	/*
	 * Create reflective boundary
	 */
	ReflectiveBoundary<AxisAlignedPlane<0> > rb(xhigh); rb.add_species(red);

	/*
	 * Create unimolecular reaction operator (red -> 0)
	 */
	UniMolecularReaction dr(1,red >> 0);

	const double h_in = 0.1;
	StructuredGrid out_grid(Vect3d(0,0,0), Vect3d(1,1,1), Vect3d(h_in,1.0,1.0));
	OutputConcentrations out_concen_1d("output/simpleReact_",end_time/100.0, out_grid);out_concen_1d.add_species(red);

	OutputCompareWithFunction<rmsError<MyFunction> > out_compare("output/simpleReact_error",end_time/100.0,0,1,out_grid,
			rmsError<MyFunction>(MyFunction(num_particles,red.D))); out_compare.add_species(red);

	Plot2d plot(0,out_concen_1d.get_data("x"),out_concen_1d.get_data("Concentration"),"x","Concentration","My first plot");

	Visualisation vis(0);vis.add_species(red);
	   vis.add_geometry(xlow);
	   vis.add_geometry(xhigh);
	   vis.add_geometry(ylow);
	   vis.add_geometry(yhigh);
	   vis.add_geometry(zlow);
	   vis.add_geometry(zhigh);

	/*
	 * Run simulation until end_time with timestep dt
	 */
	run(red, end_time, dt, bd, fb, jb2, jb3, jb4, jb5, rb, dr, out_concen_1d, out_compare, vis,plot);

	std::vector<int> bins;
	make_histogram(bins,red,100, 0,1);

	std::ofstream f;
	f.open(("output/simpleReact_"+std::string(argv[1])+".dat").c_str());
	f << end_time << ' ';
	BOOST_FOREACH(int x,bins) {
		f << x <<' ';
	}
	f << std::endl;
	f.close();


	return EXIT_SUCCESS;
}