-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop_conditions.py
167 lines (117 loc) · 4.99 KB
/
stop_conditions.py
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
160
161
162
163
164
165
166
167
import numpy as np
def time_step(time, time_max):
"""time_step : function to define if the simmulation
has converged, but only in case it has
reached the maximum time specified.
PARAMETERS
----------
time : last moment in which the calculation has been carried out
time_max : maximum time over which the simmulation cannot continue.
It has to be specified by the user if this converging
criteria is active.
OUTPUTS
-------
stop : indicates wheter the calculation must stop or continue.
Returns 0 if the caltulation must continue
Returns 1 if the calculation must stop
time : Returns the simmulation time. It is the same value as the input.
"""
stop = 0
if time >= time_max:
stop = 1
return stop, time
def max_iterations(iteration_number, iteration_max):
"""max_iterations : time_step : function to define if the simmulation
has converged, but only in case it has
reached the maximum time specified.
PARAMETERS
----------
iteration_number : last iteration in which the calculation has been
carried out.
iteration_max : maximum number of iterations over which the
simmulation cannot continue. It has to be specified by the user
if this converging criteria is active.
OUTPUTS
-------
stop : indicates wheter the calculation must stop or continue.
Returns 0 if the caltulation must continue.
Returns 1 if the calculation must stop.
iteration_number :Returns the simmulation time. It is the same
value as the input.
"""
stop = 0
if iteration_number >= iteration_max:
stop = 1
return stop, iteration_number
def mean_error(temperatures, error_mean_limit,
num_iterations):
"""
mean_error : function to define if the simmulation
has converged, but only using the criterion of the mean error
allowed in the last iterations specified.
PARAMETERS
----------
temperatures : solution matrix of the problem, containig in each column
the state vector correspongding to each iteration.
error_mean_limit : mean error allowed in the last ierations.
It has to be specified by the user if the criteria is active.
num_iterations : number of iterations where the maximum error is evaluated.
It has to be specified by the user if the criteria is active.
OUTPUTS
-------
stop : indicates wheter the calculation must stop or continue
Returns 0 if the caltulation must continue
Returns 1 if the calculation must stop
error_mean : mean error of the calculations in the last iterations
specified by the user
"""
b = np.shape(temperatures)[1]
if b < num_iterations:
stop = 0
error_mean = 1
else:
mean_matrix = temperatures[:, (b - int(num_iterations)):b]
maximum_terms = np.max(mean_matrix, axis=1)
minimum_terms = np.min(mean_matrix, axis=1)
error_mean = np.mean(np.abs(maximum_terms - minimum_terms)/np.abs(minimum_terms))
if error_mean <= error_mean_limit:
stop = 1 #Stop calculation is mean error is low enougth
else:
stop = 0 #Continue if mean error is still large
return stop, error_mean
def max_error(temperatures, error_max_limit,
num_iterations):
"""
max_error : function to define if the simmulation
has converged, but only using the criterion of the maximum error
allowed in the last iterations specified.
PARAMETERS
----------
temperatures : solution matrix of the problem, containig in each column
the state vector correspongding to each iteration.
error_max_limit : maximum error allowed in the last ierations.
It has to be specified by the user if the criteria is active.
num_iterations : number of iterations where the mean error is evaluated.
It has to be specified by the user if the criteria is active.
OUTPUTS
-------
stop : indicates wheter the calculation must stop or continue
Returns 0 if the caltulation must continue
Returns 1 if the calculation must stop
error_max : maximum error of the calculations in the last iterations
specified by the user
"""
b = np.shape(temperatures)[1]
if b < num_iterations:
stop = 0
error_max = 1
else:
max_error_matrix = temperatures[:, (b - int(num_iterations)):b]
maximum_terms = np.max(max_error_matrix, axis=1)
minimum_terms = np.min(max_error_matrix, axis=1)
error_max = np.max(np.abs(maximum_terms - minimum_terms)/np.abs(minimum_terms))
if error_max <= error_max_limit:
stop = 1 #Stop calculation is maximum error is low enougth
else:
stop = 0 #Continue if maximum error is still large
return stop, error_max