static void runForever(const uavcan_linux::NodePtr& node) { /* * Subscribing to the UAVCAN logging topic */ auto log_handler = [](const uavcan::ReceivedDataStructure<uavcan::protocol::debug::LogMessage>& msg) { std::cout << msg << std::endl; }; auto log_sub = node->makeSubscriber<uavcan::protocol::debug::LogMessage>(log_handler); /* * Printing when other nodes enter the network or change status */ struct NodeStatusMonitor : public uavcan::NodeStatusMonitor { explicit NodeStatusMonitor(uavcan::INode& node) : uavcan::NodeStatusMonitor(node) { } void handleNodeStatusChange(const NodeStatusChangeEvent& event) override { std::cout << "Remote node NID " << int(event.node_id.get()) << " changed status: " << int(event.old_status.status_code) << " --> " << int(event.status.status_code) << std::endl; } }; NodeStatusMonitor nsm(*node); ENFORCE(0 == nsm.start()); /* * Adding a stupid timer that does nothing once a minute */ auto do_nothing_once_a_minute = [&node](const uavcan::TimerEvent&) { node->logInfo("timer", "Another minute passed..."); // coverity[dont_call] node->setVendorSpecificStatusCode(static_cast<std::uint16_t>(std::rand())); // Setting to an arbitrary value }; auto timer = node->makeTimer(uavcan::MonotonicDuration::fromMSec(60000), do_nothing_once_a_minute); /* * Spinning forever */ while (true) { const int res = node->spin(uavcan::MonotonicDuration::getInfinite()); if (res < 0) { node->logError("spin", "Error %*", res); } } }
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; }
static void runSubNode(const uavcan_linux::SubNodePtr& node) { std::cout << "Running sub node" << std::endl; /* * Log subscriber */ auto log_sub = node->makeSubscriber<uavcan::protocol::debug::LogMessage>( [](const uavcan::ReceivedDataStructure<uavcan::protocol::debug::LogMessage>& msg) { std::cout << msg << std::endl; }); /* * Node status monitor */ struct NodeStatusMonitor : public uavcan::NodeStatusMonitor { explicit NodeStatusMonitor(uavcan::INode& node) : uavcan::NodeStatusMonitor(node) { } virtual void handleNodeStatusChange(const NodeStatusChangeEvent& event) override { std::cout << "Remote node NID " << int(event.node_id.get()) << " changed status: " << int(event.old_status.status_code) << " --> " << int(event.status.status_code) << std::endl; } }; NodeStatusMonitor nsm(*node); ENFORCE(0 == nsm.start()); /* * KV subscriber */ auto kv_sub = node->makeSubscriber<uavcan::protocol::debug::KeyValue>( [](const uavcan::ReceivedDataStructure<uavcan::protocol::debug::KeyValue>& msg) { std::cout << msg << std::endl; }); /* * KV publisher */ unsigned kv_value = 0; auto kv_pub = node->makePublisher<uavcan::protocol::debug::KeyValue>(); auto timer = node->makeTimer(uavcan::MonotonicDuration::fromMSec(5000), [&](const uavcan::TimerEvent&) { uavcan::protocol::debug::KeyValue kv; kv.key = "five_seconds"; kv.value = kv_value++; const int res = kv_pub->broadcast(kv); if (res < 0) { std::cerr << "Sub KV pub err " << res << std::endl; } }); while (true) { const int res = node->spin(uavcan::MonotonicDuration::fromMSec(1000)); if (res < 0) { std::cerr << "SubNode spin error: " << res << std::endl; } } }
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; }