1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
// -*- C++ -*-
#ifndef __HomogeneousHistogramsDirect_ipp__
#error This file is an implementation detail.
#endif
#ifdef STOCHASTIC_CUSTOM_PROPENSITIES
#include "Propensities.h"
#endif
#include "stochastic/HistogramsDirect.h"
#include "stochastic/ReactionSet.h"
#include "stochastic/reactionPropensityInfluence.h"
#include "ads/timer/Timer.h"
#include "ads/utility/ParseOptionsArguments.h"
#include <iostream>
#include <iterator>
#include <cassert>
namespace {
//
// Global variables.
//
//! The program name.
static std::string programName;
//
// Local functions.
//
//! Exit with an error message.
void
exitOnError() {
std::cerr
<< "Bad arguments. Usage:\n"
<< programName << "\n\n"
<< "This program reads the model and simulations parameters from\n"
<< "stdin and writes the trajectories to stdout.\n";
// CONTINUE
exit(1);
}
}
//! The main loop.
int
main(int argc, char* argv[]) {
typedef ExponentialGenerator::DiscreteUniformGenerator
DiscreteUniformGenerator;
#ifdef STOCHASTIC_CUSTOM_PROPENSITIES
typedef Propensities<true> PropensitiesFunctor;
#else
// If we use the reaction influence array, we will compute the propensities
// one at a time.
typedef stochastic::PropensitiesSingle<true> PropensitiesFunctor;
#endif
typedef PropensitiesFunctor::ReactionSetType ReactionSet;
typedef stochastic::HistogramsDirect<DiscreteGenerator, ExponentialGenerator,
PropensitiesFunctor> Solver;
typedef stochastic::State State;
#define __input_ipp__
#include "input.ipp"
#undef __input_ipp__
// Check the number of solver parameters.
// CONTINUE: Error message.
assert(solverParameters.size() == 0);
// Number of frames and frame times.
std::vector<double> frameTimes;
std::cin >> frameTimes;
// Number of bins in the histograms.
std::size_t numberOfBins;
std::cin >> numberOfBins;
// Histogram multiplicity.
std::size_t histogramMultiplicity;
std::cin >> histogramMultiplicity;
//
// Build the array of reaction influences.
//
array::StaticArrayOfArrays<std::size_t> reactionInfluence;
stochastic::computeReactionPropensityInfluence
(initialPopulations.size(), reactions.getBeginning(), reactions.getEnd(),
&reactionInfluence, true);
//
// Construct the simulation class.
//
Solver solver(State(initialPopulations, reactions.getBeginning(),
reactions.getEnd()),
PropensitiesFunctor(reactions), reactionInfluence,
frameTimes, recordedSpecies, numberOfBins,
histogramMultiplicity, maximumAllowedSteps);
//
// Read the Mersenne twister state.
//
std::cin >> solver.getDiscreteUniformGenerator();
#ifdef STOCHASTIC_USE_INFLUENCE_IN_GENERATOR
solver.getDiscreteGenerator().setInfluence(&reactionInfluence);
#endif
// There should be no more options.
if (! parser.areOptionsEmpty()) {
std::cerr << "Error. Unmatched options:\n";
parser.printOptions(std::cerr);
exitOnError();
}
//
// Run the simulation.
//
double totalReactionCount = 0;
std::size_t numberOfTrajectories = 0;
ads::Timer timer;
double elapsedTime = 0;
// Loop until there are no more tasks.
while (true) {
// The number of trajectories to generate in this task.
std::size_t trajectoriesInTask = 0;
std::cin >> trajectoriesInTask;
numberOfTrajectories += trajectoriesInTask;
if (trajectoriesInTask == 0) {
break;
}
for (std::size_t n = 0; n != trajectoriesInTask; ++n) {
timer.tic();
// Run the simulation.
solver.initialize(initialPopulations, startTime);
solver.simulate();
elapsedTime += timer.toc();
totalReactionCount += solver.getState().getReactionCount();
}
// Write the number of trajectories in this task to indicate that the
// simulations have completed.
std::cout << trajectoriesInTask << '\n';
std::cout.flush();
}
// Synchronize the histograms.
solver.synchronize();
// Empty line for the dictionary of information.
std::cout << '\n';
if (! solver.getError().empty()) {
std::cout << solver.getError() << '\n';
}
else {
// No errors.
std::cout << '\n';
// The number of trajectories generated.
std::cout << numberOfTrajectories << '\n';
// Write the histograms.
std::cout << solver.getHistograms();
}
// Write the final Mersenne twister state.
std::cout << solver.getDiscreteUniformGenerator() << '\n';
if (arePrintingPerformance) {
// Restore the default precision.
std::cout.precision(defaultPrecision);
// Performance message.
std::cout << "Simulation time = " << elapsedTime << "\n"
<< "The ensemble of simulations took "
<< totalReactionCount << " steps.\n"
<< "Reactions per second = "
<< totalReactionCount / elapsedTime << ".\n"
<< "Time per reaction = "
<< elapsedTime / totalReactionCount * 1e9 << " nanoseconds\n"
<< elapsedTime << "\n";
}
return 0;
}
|