refactor Q1 tests to be faster and more flexible

parent ee8b2fdb
......@@ -31,6 +31,16 @@ base_config = {
}
def assert_results(results, expected_results):
errors = {}
for kk, vv in expected_results.items():
if not np.allclose(results[kk], vv):
errors[kk] = {}
errors[kk]['expected'] = vv
errors[kk]['actual'] = results[kk]
assert len(errors) == 0, errors
def compute_on_single_refinemet(cfg, make_pou, pou_id):
logger = getLogger(f'{FILENAME}.compute_on_single_refinemet')
def init_dd():
......@@ -71,17 +81,18 @@ def compute_on_single_refinemet(cfg, make_pou, pou_id):
def run_fixed_H_refined_h(
num_refs=(1, 2, 3, 4, 5, 6, 7, 8),
num_h_refs=3,
macro_grid=[3, 3],
make_pou=lambda dd: ContinuousLagrangePartitionOfUnity(dd),
pou_id='Q1PoU',
DATASET_ID=FILENAME + '.fixed_H_refined_h'):
config = dict(base_config)
config['num_macro_elements'] = [2, 2]
config['num_refs'] = num_refs
config['num_macro_elements'] = macro_grid
config['num_h_refs'] = num_h_refs
new_dataset(DATASET_ID, **config)
results = {'num_subdomains': [], 'num_DoFs': [], 'error': [], 'estimate': []}
for num_refs in config['num_refs']:
for num_refs in np.arange(num_h_refs + 1):
cfg = dict(config)
cfg['num_refinements_per_subdomain'] = num_refs
nsd, nD, er, es = compute_on_single_refinemet(cfg, make_pou, pou_id)
......@@ -95,84 +106,83 @@ def run_fixed_H_refined_h(
def run_fixed_h_refined_H(
num_H_refs=3, # the fourth already takes 15 minutes and all ram on my notebook
global_grid_refines=3,
num_H_refs_less=0,
initial_macro_grid=[1, 1],
make_pou=lambda dd: ContinuousLagrangePartitionOfUnity(dd),
pou_id='Q1PoU',
DATASET_ID=FILENAME + '.fixed_h_refined_H'):
config = dict(base_config)
macro_grid = [4, 4]
num_refs = 7
config['macro_grid'] = macro_grid
config['num_refs'] = num_refs
macro_grid = list(initial_macro_grid)
config['initial_macro_grid'] = initial_macro_grid
config['global_grid_refines'] = global_grid_refines
new_dataset(DATASET_ID, **config)
results = {'num_subdomains': [], 'num_DoFs': [], 'error': [], 'estimate': []}
for _ in np.arange(num_H_refs):
for _ in np.arange(global_grid_refines - num_H_refs_less + 1):
cfg = dict(config)
cfg['num_macro_elements'] = macro_grid
cfg['num_refinements_per_subdomain'] = num_refs
cfg['num_refinements_per_subdomain'] = global_grid_refines
nsd, nD, er, es = compute_on_single_refinemet(cfg, make_pou, pou_id)
results['num_subdomains'].append(nsd)
results['num_DoFs'].append(nD)
results['error'].append(er)
results['estimate'].append(es)
print('')
num_refs -= 1
global_grid_refines -= 1
macro_grid[0] *= 2
macro_grid[1] *= 2
return results
def expected_results_fixed_H_refined_h():
return {'num_subdomains': [4, 4],
'num_DoFs': [36, 100],
'error': [0.09257416550107829, 0.10496923582575818],
'estimate': [0.3981174179206744, 0.4426426990913186]}
# For the above estimates, we determine the patch DoFs where the PoU is zero and set the rows/columns of the patch
# product to unit rows/columns for these DoFs. Not doing so results in a weak enforcing of zero values (as usual
# with DG) and gives the following estimates:
# 'estimate': [0.34881486665462186, 0.3872477001988354]}
def expected_results_fixed_H_refined_h(num_h_refs, macro_grid):
if num_h_refs == 3 and macro_grid == [1, 1]:
return {'num_subdomains': [1, 1, 1, 1],
'num_DoFs': [4, 9, 25, 81],
'error': [2.2827713033690556e-17, 0.17829090592609537, 0.18394776571648075, 0.18812850466547035],
'estimate': [1.379885348450665e-16, 0.7255116728902438, 0.7943998932316314, 0.8043649753996472]}
elif num_h_refs == 3 and macro_grid == [3, 3]:
return {'num_subdomains': [9, 9, 9, 9],
'num_DoFs': [36, 81, 225, 729],
'error': [2.437083428529774e-16, 0.06342823397024529, 0.07209377002856301, 0.07411333407007065],
'estimate': [1.2525548119768789e-15, 0.28110033383280547, 0.30527579252513726, 0.3023629356335546]}
else:
assert False, f'missing expected results for num_h_refs={num_h_refs}, macro_grid={macro_grid}'
def expected_results_fixed_h_refined_H():
return {'num_subdomains': [16, 64],
'num_DoFs': [266256, 270400],
'error': [0.0563280016520381, 0.028490976924392477],
'estimate': [0.2107764081996715, 0.10725686912260987]}
# For the above estimates, we determine the patch DoFs where the PoU is zero and set the rows/columns of the patch
# product to unit rows/columns for these DoFs. Not doing so results in a weak enforcing of zero values (as usual
# with DG) and gives the following estimates:
# 'estimate': [0.21169043667547635, 0.10599950190581235]}
def expected_results_fixed_h_refined_H(global_grid_refines, num_H_refs_less, initial_macro_grid):
if global_grid_refines == 3 and num_H_refs_less == 0 and initial_macro_grid == [1, 1]:
return {'num_subdomains': [1, 4, 16, 64],
'num_DoFs': [81, 100, 144, 256],
'error': [0.18812850466547035, 0.10496923582575818, 0.04812058367396117, 1.1495276927521529e-15],
'estimate': [0.8043649753996472, 0.4426426990913186, 0.21654695989615283, 4.156337533325682e-15]}
else:
assert False, f'missing expected results for global_grid_refines={global_grid_refines}, num_H_refs_less={num_H_refs_less}, initial_macro_grid={initial_macro_grid}'
def test_fixed_H_refined_h():
results = run_fixed_H_refined_h(num_refs=(1, 2))
expected_results = expected_results_fixed_H_refined_h()
errors = {}
for kk, vv in expected_results.items():
if not np.allclose(results[kk], vv):
errors[kk] = {}
errors[kk]['expected'] = vv
errors[kk]['actual'] = results[kk]
assert len(errors) == 0, errors
def test_fixed_H_refined_h_single_subdomain():
kwargs = {'num_h_refs': 3, 'macro_grid': [1, 1]}
assert_results(run_fixed_H_refined_h(**kwargs), expected_results_fixed_H_refined_h(**kwargs))
def test_fixed_H_refined_h_truely_inner_subdomain():
kwargs = {'num_h_refs': 3, 'macro_grid': [3, 3]}
assert_results(run_fixed_H_refined_h(**kwargs), expected_results_fixed_H_refined_h(**kwargs))
def test_fixed_h_refined_H():
results = run_fixed_h_refined_H(num_H_refs=2)
expected_results = expected_results_fixed_h_refined_H()
errors = {}
for kk, vv in expected_results.items():
if not np.allclose(results[kk], vv):
errors[kk] = {}
errors[kk]['expected'] = vv
errors[kk]['actual'] = results[kk]
assert len(errors) == 0, errors
kwargs = {'global_grid_refines': 3, 'num_H_refs_less': 0, 'initial_macro_grid': [1, 1]}
assert_results(run_fixed_h_refined_H(**kwargs), expected_results_fixed_h_refined_H(**kwargs))
if __name__ == '__main__':
r = run_fixed_H_refined_h((1, 2))
r = run_fixed_H_refined_h()
for kk, vv in r.items():
print(kk, vv)
print('')
print('')
r = run_fixed_h_refined_H(2)
r = run_fixed_h_refined_H()
for kk, vv in r.items():
print(kk, vv)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment