forked from mcellteam/libMCellPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcell_simple_count.cpp
More file actions
137 lines (112 loc) · 3.82 KB
/
Copy pathmcell_simple_count.cpp
File metadata and controls
137 lines (112 loc) · 3.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
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <math.h>
#include "libMCell.h"
using namespace std;
// This is a "user-land" iteration callback class
class time_ticker : public MCellTimerEvent {
public:
long t = 0;
// This constructor adds this event's handler to the timer events list
time_ticker(MCellSimulation *sim) {
t = 0;
sim->timer_event_handlers.push_back ( this );
}
// This is the callback that's called whenever the iteration is changed
void execute() {
cout << "Time Ticker = " << t << endl;
t += 1;
}
};
// This is a "user-land" molecule counting callback class
class mol_counter : public MCellMolCreationEvent {
public:
long count = 0;
string name = "";
bool named = false;
// This constructor will count all molecules
mol_counter(MCellSimulation *sim) {
time = 0.0;
count = 0;
name = "";
named = false;
sim->mol_creation_event_handlers.push_back ( this );
}
// This constructor will only count the molecule species specified
mol_counter(MCellSimulation *sim, MCellMoleculeSpecies *mol) {
time = 0.0;
count = 0;
name = mol->name;
named = true;
sim->mol_creation_event_handlers.push_back ( this );
}
// This is the callback function that libMCell calls whenever it creates any molecule
void execute(MCellMoleculeInstance *mol) {
// Just print out the counts (they could go to a file or for any other use)
if ( !named ) {
count += 1;
cout << "Unnamed Mol Count = " << count << endl;
} else if (mol->molecule_species->name == this->name) {
count += 1;
cout << "Mol " << this->name << " Count = " << count << endl;
}
}
};
int main ( int argc, char *argv[] ) {
cout << "\n\n" << endl;
cout << "*********************************************" << endl;
cout << "* MCell C++ Test Program using libMCell *" << endl;
cout << "*********************************************" << endl;
cout << "\n" << endl;
//This is a hard-coded simulation as a simple example of the API
MCellSimulation *mcell = new MCellSimulation();
// Create a time ticker
time_ticker *my_ticker = new time_ticker ( mcell );
// Create Molecule A Species
MCellMoleculeSpecies *mol_a = new MCellMoleculeSpecies();
mol_a->name = "A";
mol_a->diffusion_constant = 1e-6;
mcell->add_molecule_species( mol_a );
// Create Molecule B Species
MCellMoleculeSpecies *mol_b = new MCellMoleculeSpecies();
mol_b->name = "B";
mol_b->diffusion_constant = 2e-5;
mcell->add_molecule_species( mol_b );
// Set up an initial release of A molecules
MCellReleaseSite *rel_a = new MCellReleaseSite();
rel_a->x = 0.0;
rel_a->y = 0.0;
rel_a->z = 0.0;
rel_a->molecule_species = mol_a;
rel_a->quantity = 300;
mcell->add_molecule_release_site ( rel_a );
// Set up an initial release of B molecules
MCellReleaseSite *rel_b = new MCellReleaseSite();
rel_b->x = 0.3;
rel_b->y = 0.2;
rel_b->z = 0.1;
rel_b->molecule_species = mol_b;
rel_b->quantity = 700;
mcell->add_molecule_release_site ( rel_b );
// Add counters to count all, A, and B
mol_counter *all_counter = new mol_counter ( mcell );
mol_counter *a_counter = new mol_counter ( mcell, mol_a );
mol_counter *b_counter = new mol_counter ( mcell, mol_b );
// Set up the iterations and time step (maybe this should be done earlier?)
mcell->num_iterations = 200;
mcell->time_step = 1e-7;
// Run the simulation in the current "." directory
mcell->run_simulation(".");
// Print out the final counts
cout << endl << endl << "After simulation:" << endl;
cout << " Count[ALL] = " << all_counter->count << endl;
cout << " Count[A] = " << a_counter->count << endl;
cout << " Count[B] = " << b_counter->count << endl;
return ( 0 );
}