def test_convergence_evaluation_specification_file_unfixedvar_mutableparam(): ceval_class = cb._class_import(ceval_unfixedvar_mutableparam_str) ceval = ceval_class() import idaes.core.util.convergence.tests.conv_eval_classes as cev assert ceval.__class__ == cev.ConvEvalUnfixedVarMutableParam().__class__ spec = ceval.get_specification() fname = os.path.join(wrtdir, 'ceval_unfixedvar_mutableparam.3.42.json') cb.write_sample_file(spec, fname, ceval_unfixedvar_mutableparam_str, n_points=3, seed=42) baseline_fname = os.path.join( currdir, 'ceval_unfixedvar_mutableparam.3.42.baseline.json') with open(fname) as FILE: test = json.load(FILE) with open(baseline_fname) as FILE: baseline = json.load(FILE) assertStructuredAlmostEqual(baseline, test, abstol=1e-8) # expect an exception because var is not fixed with pytest.raises(ValueError): inputs, samples, global_results = \ cb.run_convergence_evaluation_from_sample_file(fname) if os.path.exists(fname): os.remove(fname)
def test_convergence_evaluation_specification_file_fixedvar_immutableparam(): ceval_class = cb._class_import(ceval_fixedvar_immutableparam_str) ceval = ceval_class() import idaes.core.util.convergence.tests.conv_eval_classes as cev assert ceval.__class__ == cev.ConvEvalFixedVarImmutableParam().__class__ spec = ceval.get_specification() fname = os.path.join(currdir, 'ceval_fixedvar_immutableparam.3.42.json') cb.write_sample_file(spec, fname, ceval_fixedvar_immutableparam_str, n_points=3, seed=42) baseline_fname = os.path.join( currdir, 'ceval_fixedvar_immutableparam.3.42.baseline.json') compare_json_files(baseline_fname=baseline_fname, output_fname=fname, tolerance=1e-8) # expect an exception because param is not mutable with pytest.raises(ValueError): inputs, samples, global_results = \ cb.run_convergence_evaluation_from_sample_file(fname) if os.path.exists(fname): os.remove(fname)
def test_convergence_evaluation_fixedvar_mutableparam(): ceval_class = cb._class_import(ceval_fixedvar_mutableparam_str) ceval = ceval_class() import idaes.core.util.convergence.tests.conv_eval_classes as cev assert ceval.__class__ == cev.ConvEvalFixedVarMutableParam().__class__ spec = ceval.get_specification() fname = os.path.join(currdir, 'ceval_fixedvar_mutableparam.3.43.json') cb.write_sample_file(spec, fname, ceval_fixedvar_mutableparam_str, n_points=3, seed=43) inputs, samples, global_results = \ cb.run_convergence_evaluation_from_sample_file(fname) # put the results into a json file for comparison # jsondict = dict(inputs=inputs, samples=samples, # global_results=global_results) # results_fname = os.path.join( # currdir, 'ceval_fixedvar_mutableparam.3.43.results.json') # with open(results_fname, 'w') as fd: # json.dump(jsondict, fd, indent=3) # compare results assert global_results[0]['name'] == 'Sample-1' assert global_results[0]['solved'] # This should take 14 iterations to converge with the IDAES solver # distribution, but due to various solver factors the number of iterations # could vary. assert global_results[0]['iters'] == pytest.approx(14, abs=2) assert global_results[1]['name'] == 'Sample-2' assert global_results[1]['solved'] # This should take 15 iterations to converge with the IDAES solver # distribution, but due to various solver factors the number of iterations # could vary. assert global_results[1]['iters'] == pytest.approx(15, abs=2) assert global_results[2]['name'] == 'Sample-3' assert global_results[2]['solved'] # This should take 12 iterations to converge with the IDAES solver # distribution, but due to various solver factors the number of iterations # could vary. assert global_results[2]['iters'] == pytest.approx(12, abs=2) if os.path.exists(fname): os.remove(fname)
def main(): args = _parse_arguments() if args.vb > 1: _log.setLevel(logging.DEBUG) elif args.vb > 0: _log.setLevel(logging.INFO) else: _log.setLevel(logging.WARN) if args.command == 'create-sample-file': assert args.number_samples is not None assert args.sample_file is not None assert args.convergence_evaluation_class_str is not None try: conv_eval_class = cb._class_import( args.convergence_evaluation_class_str) conv_eval = conv_eval_class() except Exception as e: print('Failed to find the specified convergence_evaluation_class ' 'with error: {}'.format(str(e))) raise ValueError( 'Invalid convergence_evaluation_class specified (-e).') spec = conv_eval.get_specification() cb.write_sample_file(eval_spec=spec, filename=args.sample_file, convergence_evaluation_class_str=args. convergence_evaluation_class_str, n_points=args.number_samples, seed=args.seed) else: if args.dmfcfg is None: dmf = None else: try: dmf = DMF(args.dmfcfg) except DMFError as err: _log.error('Unable to init DMF: {}'.format(err)) return -1 (inputs, samples, results) = \ cb.run_convergence_evaluation_from_sample_file( sample_file=args.sample_file) if results is not None: cb.save_convergence_statistics(inputs, results, dmf=dmf) return 0
def test_convergence_evaluation_stats_from_to(): ceval_class = cb._class_import(ceval_fixedvar_mutableparam_str) ceval = ceval_class() import idaes.core.util.convergence.tests.conv_eval_classes as cev assert ceval.__class__ == cev.ConvEvalFixedVarMutableParam().__class__ spec = ceval.get_specification() fname = os.path.join(wrtdir, 'ceval_fixedvar_mutableparam.3.43.json') cb.write_sample_file(spec, fname, ceval_fixedvar_mutableparam_str, n_points=3, seed=43) inputs, samples, results = \ cb.run_convergence_evaluation_from_sample_file(fname) s = cb.save_convergence_statistics(inputs, results) d = s.to_dict() s2 = cb.Stats(from_dict=d) fname1 = os.path.join(wrtdir, 'stats1.json') fname2 = os.path.join(wrtdir, 'stats2.json') fname3 = os.path.join(wrtdir, 'stats3.json') with open(fname1, "w") as f: s.to_json(f) with open(fname2, "w") as f: s2.to_json(f) compare_json_files(baseline_fname=fname1, output_fname=fname2, tolerance=1e-8) s3 = cb.Stats(from_json=fname1) with open(fname3, "w") as f: s3.to_json(f) compare_json_files(baseline_fname=fname1, output_fname=fname3, tolerance=1e-8) if os.path.exists(fname): os.remove(fname) if os.path.exists(fname1): os.remove(fname1) if os.path.exists(fname2): os.remove(fname2) if os.path.exists(fname3): os.remove(fname3)
def test_convergence_evaluation_fixedvar_mutableparam(): ceval_class = cb._class_import(ceval_fixedvar_mutableparam_str) ceval = ceval_class() import idaes.core.util.convergence.tests.conv_eval_classes as cev assert ceval.__class__ == cev.ConvEvalFixedVarMutableParam().__class__ spec = ceval.get_specification() fname = os.path.join(currdir, 'ceval_fixedvar_mutableparam.3.43.json') cb.write_sample_file(spec, fname, ceval_fixedvar_mutableparam_str, n_points=3, seed=43) inputs, samples, global_results = \ cb.run_convergence_evaluation_from_sample_file(fname) # put the results into a json file for comparison # jsondict = dict(inputs=inputs, samples=samples, # global_results=global_results) # results_fname = os.path.join( # currdir, 'ceval_fixedvar_mutableparam.3.43.results.json') # with open(results_fname, 'w') as fd: # json.dump(jsondict, fd, indent=3) # compare results assert global_results[0]['name'] == 'Sample-1' assert global_results[0]['solved'] assert global_results[0]['iters'] == 14 assert global_results[1]['name'] == 'Sample-2' assert global_results[1]['solved'] assert global_results[1]['iters'] == 15 assert global_results[2]['name'] == 'Sample-3' assert global_results[2]['solved'] assert global_results[2]['iters'] == 12 if os.path.exists(fname): os.remove(fname)
def test_convergence_evaluation_stats_from_to(): ceval_class = cb._class_import(ceval_fixedvar_mutableparam_str) ceval = ceval_class() import idaes.core.util.convergence.tests.conv_eval_classes as cev assert ceval.__class__ == cev.ConvEvalFixedVarMutableParam().__class__ spec = ceval.get_specification() fname = os.path.join(wrtdir, 'ceval_fixedvar_mutableparam.3.43.json') cb.write_sample_file(spec, fname, ceval_fixedvar_mutableparam_str, n_points=3, seed=43) inputs, samples, results = \ cb.run_convergence_evaluation_from_sample_file(fname) s = cb.save_convergence_statistics(inputs, results) d = s.to_dict() s2 = cb.Stats(from_dict=d) fname1 = os.path.join(wrtdir, 'stats1.json') with open(fname1, "w") as f: s.to_json(f) with open(fname1) as FILE: baseline = json.load(FILE) buf = io.StringIO() s2.to_json(buf) test = json.loads(buf.getvalue()) assertStructuredAlmostEqual(baseline, test, abstol=1e-8) s3 = cb.Stats(from_json=fname1) buf = io.StringIO() s3.to_json(buf) test = json.loads(buf.getvalue()) assertStructuredAlmostEqual(baseline, test, abstol=1e-8) if os.path.exists(fname): os.remove(fname) if os.path.exists(fname1): os.remove(fname1)