ViennaSHE 1.3.0
Free open-source semiconductor device simulator using spherical harmonics expansions techniques.
ssa.hpp
Go to the documentation of this file.
1#ifndef VIENNASHE_MODELS_MARKOVCHAIN_SSA_HPP
2#define VIENNASHE_MODELS_MARKOVCHAIN_SSA_HPP
3/* ============================================================================
4 Copyright (c) 2011-2022, Institute for Microelectronics,
5 Institute for Analysis and Scientific Computing,
6 TU Wien.
7
8 -----------------
9 ViennaSHE - The Vienna Spherical Harmonics Expansion Boltzmann Solver
10 -----------------
11
12 http://viennashe.sourceforge.net/
13
14 License: MIT (X11), see file LICENSE in the base directory
15=============================================================================== */
16
17#include "viennashe/forwards.h"
18
21
25
28
33namespace viennashe
34{
35 namespace models
36 {
37
45 {
46 public:
48
50 ssa_solver(viennashe::models::chain & c) : chain_(c), dt_(0) { }
51
53 double delta_t() const { return dt_; }
54
60 {
61 // This should fill all the occupancies with values between 0 and 1 where sum_i f_i = 1 !!
63 // Debug
64 //chain_.print_occupancies();
65
66 const double random_value = rnd(); // [0,1) ...
67
68 double sum = 0;
69 index_type i = 0;
70 for(i = 0; i < chain_.size1(); ++i)
71 {
72 sum += chain_.get_state(i).occupancy(); // Now this should be the probability 0 <= p_i <= 1 !!
73 if (random_value <= sum)
74 break;
75 }
76 // Set all occupancies to zero
77 for(index_type j = 0; j < chain_.size1(); ++j)
78 chain_.get_state(j).occupancy(0.0);
79 // Set the lucky occupancy to 1
80 chain_.get_state(i).occupancy(1.0);
81 }
82
89 {
90 double dt = 0;
91 const double a0 = this->sum_rates();
92
93 if (a0 <= 0.0)
94 throw viennashe::models::model_evaluation_exception("ssa.solve(): The sum of rates is zero or negative!");
95
96 double r1 = 1.0 - rnd();
97 { while (!r1) r1 = 1.0 - rnd(); } // Exclude 0.0
98 const double r2 = rnd();
99
100 dt = 1.0/a0 * std::log( 1.0/r1 );
101
102 double sum = 0;
103 index_type i = 0;
104 index_type j = 0;
105 for (i = 0; i < chain_.size1(); ++i)
106 {
107 if(chain_.get_state(i).occupancy() != 1.0) continue;
108
109 for (j = 0; j < chain_.size2(); ++j)
110 {
111 const double kij = chain_.get_rate(i, j);
112
113 if (kij <= 0.0) continue; // skip non-existent rates
114
115 const double pij = kij / a0;
116 sum += pij;
117 if (r2 <= sum) break; // we found the transition
118 }
119 if (r2 <= sum) break; // we found the transition
120 }
121
122 if (i >= chain_.size1())
123 throw viennashe::models::model_evaluation_exception("ssa.solve(): The SSA algorithm failed to find the next state!");
124
125 chain_.get_state(j).occupancy(1.0);
126 chain_.get_state(i).occupancy(0.0);
127
128 this->dt_ = dt; // Cache delta time
129 return this->delta_t();
130 }
131
132 viennashe::models::chain const & chain() const { return this->chain_; }
133
134 private:
135
136 double sum_rates() const
137 {
138 double sum = 0;
139 for (index_type i = 0; i < chain_.size1(); ++i)
140 {
141 if(chain_.get_state(i).occupancy() != 1.0) continue;
142
143 for (index_type j = 0; j < chain_.size2(); ++j)
144 {
145 const double kij = chain_.get_rate(i, j);
146 if (kij <= 0.0) continue;
147 sum += kij;
148 }
149 }
150 return sum;
151 }
152
154 double dt_;
155 };
156
157
158
159 } // namespace models
160} // namespace viennashe
161
162
163#endif /* VIENNASHE_MODELS_MARKOVCHAIN_SSA_HPP */
164
Implements the concept of a state (in a finite-state diagram) and markov chains.
Implementation of Markov-Chains.
Definition: chain.hpp:85
index_type size2() const
Returns the column size of the rate-matrix.
Definition: chain.hpp:148
state_base & get_state(index_type idx)
Definition: chain.hpp:249
state_base::index_type index_type
Definition: chain.hpp:87
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
Exception for the case that the evaluation of a model fails.
Definition: exception.hpp:89
The SSA (Monte Carlo) solver for Markov-Chains The given chain is being referenced and the occupancie...
Definition: ssa.hpp:45
void set_equilibrium(viennashe::math::rand_generator_base< double > &rnd)
Sets the equilibrium occupancies.
Definition: ssa.hpp:59
double solve(viennashe::math::rand_generator_base< double > &rnd)
Determines the time until the next transition event and advances the system to this state.
Definition: ssa.hpp:88
ssa_solver(viennashe::models::chain &c)
CTOR.
Definition: ssa.hpp:50
viennashe::models::chain const & chain() const
Definition: ssa.hpp:132
double delta_t() const
Returns the last time step dt in seconds.
Definition: ssa.hpp:53
viennashe::models::chain::index_type index_type
Definition: ssa.hpp:47
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
Contains forward declarations and definition of small classes that must be defined at an early stage.
Implementation of various utilities related to linear algebra.
Contains exceptions for viennashe::models.
Contains exceptions for markov-models in viennashe::models.
void set_chain_to_equilibrium_expectation(chain &c)
Solves the time averaged set of equilibrium (dt->infty) equations for the given Markov-Chain and sets...
Definition: chain.hpp:303
The main ViennaSHE namespace. All functionality resides inside this namespace.
Definition: accessors.hpp:40
Implementation of pseudo-random number generators.
Contains the basic reaction rate interface.
Forward declarations for a generic solver layer, providing bindings to a native Gauss solver,...