Skip to content

Commit 425cd47

Browse files
Some fine tuning done
-Allowing to specify a ma number of iterations to consider as a command line argument - Fixed one issue with negative integral quantities, these could not converge before
1 parent f77f1c2 commit 425cd47

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

optimalStoppingCriterion/findOptimalIteration.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get_start_of_monotonic_convergence(self):
2727
def get_iteration_with_highest_acceptable_error(self, average, threshold):
2828
for iteration in range(len(self.values) - self.window_size - 1, 0, -1):
2929
residual = fabs(self.averages[iteration] - average)
30-
residual /= average
30+
residual /= fabs(average)
3131

3232
if residual > threshold:
3333
return iteration

optimalStoppingCriterion/handleCLAs.py

+28-18
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ def __init__(self, args):
88
self.min_window = 10
99
self.max_window = 100
1010
self.increments = 5
11-
self.convergence_threshold = 0.01
11+
self.asymptotic_convergence_threshold = 0.01
12+
self.max_iterations = 0
1213
self._process_cla()
1314

1415
def _process_cla(self):
@@ -39,14 +40,19 @@ def _process_cla(self):
3940
print('If you want to change these defaults, see the help message with -h or --help\n')
4041

4142

42-
if '-ct' in self.args:
43-
self.convergence_threshold = float(self.args[int(self.args.index('-ct') + 1)])
44-
elif'--convergence-threshold' in self.args:
45-
self.convergence_threshold = float(self.args[int(self.args.index('--convergence-threshold') + 1)])
43+
if '-act' in self.args:
44+
self.asymptotic_convergence_threshold = float(self.args[int(self.args.index('-act') + 1)])
45+
elif'--asymptotic-convergence-threshold' in self.args:
46+
self.asymptotic_convergence_threshold = float(self.args[int(self.args.index('--asymptotic-convergence-threshold') + 1)])
4647
else:
4748
print('No asymptotic convergence threshold specified.')
4849
print('Using default asymptotic convergence threshold of 0.01 (1%)\n')
4950

51+
if '-mi' in self.args:
52+
self.max_iterations = int(self.args[int(self.args.index('-mi') + 1)])
53+
elif '--max-iterations' in self.args:
54+
self.max_iterations = int(self.args[int(self.args.index('--max-iterations') + 1)])
55+
5056
if '-h' in self.args or '--help' in self.args:
5157
self._print_help()
5258

@@ -56,25 +62,29 @@ def get_case(self):
5662
def get_window_sizes(self):
5763
return self.min_window, self.max_window, self.increments
5864

59-
def get_convergence_threshold(self):
60-
return self.convergence_threshold
65+
def get_asymptotic_convergence_threshold(self):
66+
return self.asymptotic_convergence_threshold
67+
68+
def get_max_iterations(self):
69+
return self.max_iterations
6170

6271

6372
def _print_help(self):
6473
if '-h' in self.args or '--help' in self.args:
6574
print('Usage: python3 pyOSC.py -c <case>\n')
66-
print(' -c, --case <case> Case to run')
67-
print(' -h, --help Show this help message and exit')
68-
print(' -w, --window Window size to use for convergence analysis')
69-
print(' Specified as a list of integers separated by spaces')
70-
print(' Use the format <smallest window> <largest window> <increments>')
71-
print(' Example: -w 10 100 10. Smallest window = 10')
72-
print(' largest window = 100, and window increments = 10')
73-
print(' If no window size is specified, the default 10 100 5 is used')
75+
print(' -c, --case <case> Case to run')
76+
print(' -h, --help Show this help message and exit')
77+
print(' -w, --window Window size to use for convergence analysis')
78+
print(' Specified as a list of integers separated by spaces')
79+
print(' Use the format <smallest window> <largest window> <increments>')
80+
print(' Example: -w 10 100 10. Smallest window = 10')
81+
print(' largest window = 100, and window increments = 10')
82+
print(' If no window size is specified, the default 10 100 5 is used')
7483
print(' -act, --asymptotic-convergence-threshold')
75-
print(' Asymptotic convergence threshold to use for convergence analysis.')
76-
print(' This value will be used to check if coefficients have converged')
77-
print(' to the asymptotic value at the end of the simulation.')
84+
print(' Asymptotic convergence threshold to use for convergence analysis.')
85+
print(' This value will be used to check if coefficients have converged')
86+
print(' to the asymptotic value at the end of the simulation.')
87+
print(' -mi, --max-iteration Max number of iterations to consider in convergence analysis')
7888

7989
exit()
8090

optimalStoppingCriterion/windowAveraging.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ def determine_best_residual_threshold(self):
4545
window_residual[index] = fabs(averages[index + 1] - averages[index])
4646
window_residual[index] /= fabs(averages[index])
4747

48-
residual_at_optimum = window_residual[self.earliest_iteration_to_stop]
48+
assert self.earliest_iteration_to_stop > window_size, \
49+
'optimal iteration to stop reached before largest window size. Reduce your window sizes!\n' \
50+
f'Largest window: {self.window_sizes[-1]}\n' \
51+
f'Optimal iteration to stop: {self.earliest_iteration_to_stop}\n' \
52+
f'Max window size possible: {self.earliest_iteration_to_stop}\n'
53+
residual_at_optimum = window_residual[self.earliest_iteration_to_stop - window_size]
4954

5055
has_lower_residual_before_optimum = False
5156
iteration_before_optimum = 0

pycfd-osc.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def main():
1010

1111
case = cla.get_case()
1212
min_window_size, max_window_size, window_increments = cla.get_window_sizes()
13-
asymptotic_convergence_threshold = cla.get_convergence_threshold()
13+
asymptotic_convergence_threshold = cla.get_asymptotic_convergence_threshold()
1414

1515
# initialise dictionary holding convergence results
1616
convergence_data = dict()
@@ -35,6 +35,9 @@ def main():
3535

3636
# read values from file
3737
values = osc.fluent_file_reader(path_and_filename).get_values()
38+
max_iterations = cla.get_max_iterations()
39+
if max_iterations > 0:
40+
values = values[:max_iterations]
3841

3942
# determine the averaged (asymptotic) coefficient value at the end of the simulation
4043
asymptotic_values = osc.find_asymptotic_values(values)

0 commit comments

Comments
 (0)