-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvessel.h
More file actions
159 lines (133 loc) · 4.82 KB
/
vessel.h
File metadata and controls
159 lines (133 loc) · 4.82 KB
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
//
// Created by jskad on 10-06-2024.
//
#pragma once
#include <string>
#include <utility>
#include <vector>
#include <unordered_map>
#include <stdexcept>
#include "reaction.h"
#include <random>
#include <algorithm>
#include <coro/coro.hpp>
#include <set>
#include <ranges>
#ifndef STOCHASTIC_SIMULATION_VESSEL_H
#define STOCHASTIC_SIMULATION_VESSEL_H
#endif //STOCHASTIC_SIMULATION_VESSEL_H
namespace stochastic {
// Requirement 3
template<typename K, typename V>
class Symbol_table {
public:
const K &add(const K &key, const V &value) {
if (symbols.find(key) != symbols.end()) { //Failure case B from requirement 3
throw std::runtime_error("Symbol already exists");
}
symbols[key] = value;
return key;
}
const V &get(const K &key) const {
if (!exists(key)) { //Failure case A from requirement 3
throw std::runtime_error("Symbol not found");
}
return symbols.at(key);
}
void update(const K &key, const V &new_value) {
if (!exists(key)) {
throw std::runtime_error("Symbol not found");
}
symbols[key] = new_value;
}
bool exists(const K &key) const {
return symbols.find(key) != symbols.end();
}
std::vector<K> get_all_symbols() const {
std::vector<K> keys;
keys.reserve(symbols.size());
for (const auto &pair: symbols) {
keys.push_back(pair.first);
}
return keys;
}
private:
std::unordered_map<K, V> symbols;
};
struct TrajectoryPoint {
double time{};
Symbol_table<std::string, double> reactants;
};
class Vessel {
public:
std::vector<Reaction> reactions;
std::string const name;
explicit Vessel(std::string name) : name(std::move(name)) {}
const std::string &add(const std::string &key, double value) {
return reactants.add(key, value);
}
std::string environment() {
return reactants.add("env", 0);
}
void add(const Reaction &reaction) {
for (const auto &reactant: reaction.inputs) {
if (!reactants.exists(reactant)) {
throw std::runtime_error("Input reactant not found");
}
}
for (const auto &reactant: reaction.products) {
if (!reactants.exists(reactant)) {
throw std::runtime_error("Product reactant not found");
}
}
reactions.push_back(reaction);
}
const double &get(const std::string &key) const {
return reactants.get(key);
}
std::vector<std::string> get_all_reactants() const {
return reactants.get_all_symbols();
}
// Requirement 4
coro::generator<TrajectoryPoint> simulate(const double &end_time) {
double current_time = 0;
while (current_time <= end_time) {
for (Reaction &reaction: reactions) {
compute_delay(reaction);
}
auto &reaction = *std::min_element(reactions.begin(), reactions.end(),
[](const auto a, const auto b) { return a.delay < b.delay; });
current_time += reaction.delay;
if (std::none_of(reaction.inputs.begin(), reaction.inputs.end(),
[&reactants = reactants](const auto &reactant) {
return reactants.get(reactant) == 0;
})) {
for (const auto &reactant: reaction.inputs) {
reactants.update(reactant, reactants.get(reactant) - 1);
}
for (const auto &reactant: reaction.products) {
reactants.update(reactant, reactants.get(reactant) + 1);
}
}
co_yield {current_time, reactants};
}
}
private:
void compute_delay(Reaction &reaction) const {
std::random_device rd;
std::mt19937 gen(rd());
double input_product = 1;
for (const auto &input: reaction.inputs) {
const auto &input_amount = reactants.get(input);
if (input_amount == 0) {
reaction.delay = std::numeric_limits<double>::infinity();
return;
}
input_product *= input_amount;
}
std::exponential_distribution d(reaction.rate * input_product);
reaction.delay = d(gen);
}
Symbol_table<std::string, double> reactants;
};
}