ViennaSHE 1.3.0
Free open-source semiconductor device simulator using spherical harmonics expansions techniques.
markov_chains.cpp
Go to the documentation of this file.
1/* ============================================================================
2 Copyright (c) 2011-2022, Institute for Microelectronics,
3 Institute for Analysis and Scientific Computing,
4 TU Wien.
5
6 -----------------
7 ViennaSHE - The Vienna Spherical Harmonics Expansion Boltzmann Solver
8 -----------------
9
10 http://viennashe.sourceforge.net/
11
12 License: MIT (X11), see file LICENSE in the base directory
13=============================================================================== */
14
15#include <cstdlib>
16
17#include "tests/src/common.hpp"
18
23
29int main()
30{
31 const double tol = 1e-9;
32
33 /*
34 * Expected output: "
35
36 | to 0 | to 1 |
37from 0 | 0 | 12 |
38from 1 | 21 | 0 |
39
40
41 States 0 | 1 |
42Occupancy 1 | 0 |
43
44
45dt = 0.0786663
46 States 0 | 1 |
47Occupancy 0 | 1 |
48"
49 */
50
51
52 // Random number generator
54 rnd.seed(10);
55
56 // Rates
59
60 // States
61 viennashe::models::state_base s1(0, "state 1");
62 viennashe::models::state_base s2(1, "state 2");
63
64 // Chain
66 // Allocate chain
67 mychain.add_rate(s1, s2, r12);
68 mychain.add_rate(s2, s1, r21);
69
70 // Print the rate matrix
71 std::cout << std::endl;
72 mychain.print_rate_matrix();
73 std::cout << std::endl;
74
75 const std::size_t size = mychain.size1();
76 if (size != 2 ) throw viennashe::invalid_value_exception("markov_chains-test: Size-test -> should 2, but is not ", static_cast<double>(size));
77
78 if (std::fabs(mychain.get_rate(0,1) - r12.value()) > 1e-10)
79 throw viennashe::invalid_value_exception("markov_chains-test: rate test failed. Invalid rate: ", mychain.get_rate(0, 1));
80 if (std::fabs(mychain.get_rate(1,0) - r21.value()) > 1e-10)
81 throw viennashe::invalid_value_exception("markov_chains-test: rate test failed. Invalid rate: ", mychain.get_rate(1, 0));
82 if (std::fabs(mychain.get_rate(0,0)) > 1e-10)
83 throw viennashe::invalid_value_exception("markov_chains-test: rate test failed. Invalid rate: ", mychain.get_rate(0, 0));
84 if (std::fabs(mychain.get_rate(1,1)) > 1e-10)
85 throw viennashe::invalid_value_exception("markov_chains-test: rate test failed. Invalid rate: ", mychain.get_rate(1, 1));
86
87
88 // Solver (SSA))
90 // Set the equilibrium !!
91 ssa.set_equilibrium(rnd);
92 // Print occupancies
93 std::cout << std::endl;
94 mychain.print_occupancies();
95 std::cout << std::endl;
96
97 if (std::fabs(mychain.get_state(0).occupancy() - 1.0) > 1e-10)
98 throw viennashe::invalid_value_exception("markov_chains-test: The occupancy of state 0 should be 1. ", mychain.get_state(0).occupancy());
99 if (std::fabs(mychain.get_state(1).occupancy()) > 1e-10)
100 throw viennashe::invalid_value_exception("markov_chains-test: The occupancy of state 1 should be 0. ", mychain.get_state(1).occupancy());
101
102 // Solve per SSA and print occupancies
103 double dt = ssa.solve(rnd);
104 std::cout << std::endl << "dt = " << std::setprecision(10) << dt << std::endl;
105 if (!viennashe::testing::fuzzy_equal(dt, 0.07866631598, tol))
106 throw viennashe::invalid_value_exception("markov_chains-test: dt should be '0.07866631598' (tol = 1e-10) ", dt);
107
108 mychain.print_occupancies();
109
110 if (std::fabs(mychain.get_state(1).occupancy() - 1.0) > 1e-10)
111 throw viennashe::invalid_value_exception("markov_chains-test: The occupancy of state 1 should be 1. ", mychain.get_state(1).occupancy());
112 if (std::fabs(mychain.get_state(0).occupancy()) > 1e-10)
113 throw viennashe::invalid_value_exception("markov_chains-test: The occupancy of state 0 should be 0. ", mychain.get_state(0).occupancy());
114
115 std::cout << std::endl;
116 std::cout << "Test finished successfully!" << std::endl;
117
118 return (EXIT_SUCCESS);
119}
120
Implements the concept of a state (in a finite-state diagram) and markov chains.
Exception for the case that an invalid value (depends on the method called) is encountered.
Definition: exception.hpp:76
A pseudo-random number generator implementation based on std::rand()
Definition: random.hpp:54
void seed(unsigned int s)
Sets the seed of the pseudo-random number generator (std::srand)
Definition: random.hpp:60
Implementation of Markov-Chains.
Definition: chain.hpp:85
void add_rate(state_base const &from, state_base const &to, rate_base const &r)
Adds a rate between to states. Additionally adds the given states if necessary.
Definition: chain.hpp:113
void print_occupancies() const
Prints the occupancies of each state in the chain onto screen (log::info). Usefull for debugging.
Definition: chain.hpp:227
void print_rate_matrix() const
Prints the rate matrix onto screen (log::info). Usefull for debugging.
Definition: chain.hpp:164
state_base & get_state(index_type idx)
Definition: chain.hpp:249
double get_rate(state_base const &from, state_base const &to) const
Returns the actual rate (double value!) between to states.
Definition: chain.hpp:140
index_type size1() const
Returns the row size of the rate-matrix.
Definition: chain.hpp:146
The SSA (Monte Carlo) solver for Markov-Chains The given chain is being referenced and the occupancie...
Definition: ssa.hpp:45
The basic concept of a state in Markov-Chains.
Definition: chain.hpp:40
double occupancy() const
Returns the occupancy f or p of the state. It has to be a number in [0,1].
Definition: chain.hpp:56
bool fuzzy_equal(double is, double should, double tol=1e-1)
Performs a fuzzy (up to a tolerance) equal. Returns true if is and should are equal within the tolera...
Definition: common.hpp:39
Implementation of pseudo-random number generators.
Contains the basic reaction rate interface.
int main()
Definition: resistor1d-c.c:108
Contains an impelemtation of the SSA algorithm for Markov-Chains (cf. viennashe/models/markovchain/ch...
A simple constant rate.
Contains common functions, functors and other classes often needed by the tests.