Package hedge :: Package timestep :: Package multirate_ab :: Module processors
[hide private]
[frames] | no frames]

Source Code for Module hedge.timestep.multirate_ab.processors

  1  """Multirate-AB ODE solver.""" 
  2   
  3  from __future__ import division 
  4   
  5  __copyright__ = "Copyright (C) 2009 Andreas Stock, Andreas Kloeckner" 
  6   
  7  __license__ = """ 
  8  This program is free software: you can redistribute it and/or modify 
  9  it under the terms of the GNU General Public License as published by 
 10  the Free Software Foundation, either version 3 of the License, or 
 11  (at your option) any later version. 
 12   
 13  This program is distributed in the hope that it will be useful, 
 14  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  GNU General Public License for more details. 
 17   
 18  You should have received a copy of the GNU General Public License 
 19  along with this program.  If not, see U{http://www.gnu.org/licenses/}. 
 20  """ 
 21   
 22   
 23   
 24   
25 -class MRABProcessor:
26 - def __init__(self, method, substep_count):
27 self.method = method 28 self.substep_count = substep_count 29 self.substep_loop_start = 0
30
31 - def integrate_in_time(self, insn):
32 self.insn_counter += 1
33
34 - def history_update(self, insn):
35 self.insn_counter += 1
36
37 - def start_substep_loop(self, insn):
38 self.insn_counter += 1 39 self.substep_loop_start = self.insn_counter
40
41 - def eval_expr(self, expr):
42 from pymbolic import evaluate_kw 43 return evaluate_kw(expr, 44 substep_index=self.substep_index, 45 substep_count=self.substep_count)
46
47 - def end_substep_loop(self, insn):
48 self.substep_index += 1 49 if self.substep_index >= self.eval_expr(insn.loop_end): 50 self.insn_counter += 1 51 else: 52 self.insn_counter = self.substep_loop_start
53
54 - def run(self):
55 self.insn_counter = 0 56 self.substep_index = 0 57 58 while not self.insn_counter >= len(self.method.steps): 59 self.method.steps[self.insn_counter].visit(self)
60 61 62 63
64 -class MRABToTeXProcessor(MRABProcessor):
65 - def __init__(self, method, substep_count):
66 MRABProcessor.__init__(self, method, substep_count) 67 68 self.result = [] 69 70 self.s2s_hist_head = 0 71 self.s2f_hist_head = 0 72 self.f2s_hist_head = 0 73 self.f2f_hist_head = 0 74 75 self.last_assigned_at = {}
76
77 - def integrate_in_time(self, insn):
78 from hedge.timestep.multirate_ab.methods import CO_FAST 79 80 if insn.component == CO_FAST: 81 self_name = "fast" 82 src_self_speed = r"\smallstep" 83 src_self_where = self.f2f_hist_head 84 85 if self.method.s2f_hist_is_fast: 86 src_other_speed = r"\smallstep" 87 else: 88 src_other_speed = r"\bigstep" 89 src_other_where = self.s2f_hist_head 90 else: 91 self_name = "slow" 92 src_self_speed = r"\bigstep" 93 src_self_where = self.s2s_hist_head 94 95 src_other_speed = r"\bigstep" 96 src_other_where = self.f2s_hist_head 97 98 self.last_assigned_at[insn.result_name] = len(self.result) 99 100 self.result.append( 101 "\integrate {%s}{%f}{%f}{%s} {%f}{%s} {%f}{%s}" 102 % (insn.result_name, 103 self.eval_expr(insn.start)/self.substep_count, 104 self.eval_expr(insn.end)/self.substep_count, 105 self_name, 106 src_self_where, src_self_speed, 107 src_other_where, src_other_speed)) 108 109 MRABProcessor.integrate_in_time(self, insn)
110
111 - def history_update(self, insn):
112 from hedge.timestep.multirate_ab.methods import \ 113 HIST_F2F, HIST_S2F, \ 114 HIST_F2S, HIST_S2S 115 116 if insn.which == HIST_F2F: 117 step_size = 1/self.substep_count 118 name = "f2f" 119 elif insn.which == HIST_S2F: 120 if self.method.s2f_hist_is_fast: 121 step_size = 1/self.substep_count 122 else: 123 step_size = 1 124 name = "s2f" 125 elif insn.which == HIST_F2S: 126 step_size = 1 127 name = "f2s" 128 elif insn.which == HIST_S2S: 129 step_size = 1 130 name = "s2s" 131 132 hist_head_name = name+"_hist_head" 133 where = getattr(self, hist_head_name) 134 where += step_size 135 setattr(self, hist_head_name, where) 136 137 self.result.append("\histupdate {%s}{%s} {%f} {%s,%s}" 138 % (name.replace("2", "t"), name, where, 139 insn.slow_arg, insn.fast_arg)) 140 141 MRABProcessor.history_update(self, insn)
142
143 - def get_result(self):
144 if self.method.s2f_hist_is_fast: 145 which_hist = "faststfhist" 146 else: 147 which_hist = "slowstfhist" 148 149 result_generators = [ 150 self.last_assigned_at[self.method.result_fast], 151 self.last_assigned_at[self.method.result_slow], 152 ] 153 154 texed_instructions = [] 155 for i, insn_tex in enumerate(self.result): 156 if insn_tex.startswith("\integrate"): 157 if i in result_generators: 158 insn_tex += " {isresult}" 159 else: 160 insn_tex += " {}" 161 162 texed_instructions.append(insn_tex) 163 164 return "\n".join( 165 [ 166 "{" 167 r"\setcounter{step}{0}", 168 r"\def\smallstepcount{%d}" % self.substep_count, 169 r"\def\smallstep{%f}" % (1/self.substep_count), 170 r"\begin{tikzpicture}[mrabpic]" 171 r"\%s{0}" % which_hist, 172 ]+texed_instructions+[ 173 r"\%s{1}" % which_hist, 174 r"\colorlegend", 175 r"\makeaxis", 176 r"\end{tikzpicture}", 177 "}"])
178