From a51fb2f9315ba3499c1456fafd2f182202a5dcd6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ren=C3=A9=20He=C3=9F?= <rene.hess@iwr.uni-heidelberg.de>
Date: Wed, 15 May 2019 11:46:53 +0200
Subject: [PATCH] Read default options from scheme and fix some ini file errors

---
 python/dune/codegen/options.py               | 33 +++++++++++++++-----
 python/dune/codegen/options_form.yaml        | 22 ++++++-------
 test/nonlinear/diffusivewave.mini            |  2 +-
 test/poisson/opcount_poisson_dg_symdiff.mini |  2 ++
 4 files changed, 40 insertions(+), 19 deletions(-)

diff --git a/python/dune/codegen/options.py b/python/dune/codegen/options.py
index 11af17ae..49eec12b 100644
--- a/python/dune/codegen/options.py
+++ b/python/dune/codegen/options.py
@@ -42,7 +42,15 @@ class CodegenOption(ImmutableRecord):
 class CodegenGlobalOptionsArray(ImmutableRecord):
     """ A collection of form compiler arguments """
     def __init__(self, **kwargs):
-        opts = {k: v.default for k, v in CodegenGlobalOptionsArray.__dict__.items() if isinstance(v, CodegenOption)}
+        # Set the default values from the yaml scheme as defaults
+        resource_package = __name__
+        resource_path = 'options_global.yaml'
+        yaml_stream = pkg_resources.resource_string(resource_package, resource_path)
+        try:
+            scheme = yaml.safe_load(yaml_stream)
+        except Exception as e:
+            raise e
+        opts = {k: v['default'] for k, v in scheme.items()}
         opts.update(**kwargs)
         ImmutableRecord.__init__(self, **opts)
 
@@ -80,7 +88,18 @@ class CodegenGlobalOptionsArray(ImmutableRecord):
 class CodegenFormOptionsArray(ImmutableRecord):
     """ A collection of form-specific form compiler arguments """
     def __init__(self, **kwargs):
-        opts = {k: v.default for k, v in CodegenFormOptionsArray.__dict__.items() if isinstance(v, CodegenOption)}
+        # opts = {k: v.default for k, v in CodegenFormOptionsArray.__dict__.items() if isinstance(v, CodegenOption)}
+
+        # Set the default values from the yaml scheme as defaults
+        resource_package = __name__
+        resource_path = 'options_form.yaml'
+        yaml_stream = pkg_resources.resource_string(resource_package, resource_path)
+        try:
+            scheme = yaml.safe_load(yaml_stream)
+        except Exception as e:
+            raise e
+        opts = {k: v['default'] for k, v in scheme.items()}
+
         opts.update(**kwargs)
         ImmutableRecord.__init__(self, **opts)
 
@@ -156,7 +175,7 @@ def initialize_options():
         scheme_global = yaml.safe_load(yaml_stream)
     except Exception as e:
         raise e
-    validator_global = CodegenOptionsValidator(scheme_global)
+    validator_global = CodegenOptionsValidator(scheme_global, require_all=True)
     if not validator_global.validate(_global_options.__dict__):
         raise RuntimeError("Global options validation failed: {}".format(validator_global.errors))
 
@@ -167,7 +186,7 @@ def initialize_options():
         scheme_form = yaml.safe_load(yaml_stream)
     except Exception as e:
         raise e
-    validator_form = CodegenOptionsValidator(scheme_form)
+    validator_form = CodegenOptionsValidator(scheme_form, require_all=True)
     for form in [i.strip() for i in _global_options.operators.split(",")]:
         if not validator_form.validate(_form_options[form].__dict__):
             raise RuntimeError("Form options validation failed: {}".format(validator_form.errors))
@@ -259,13 +278,13 @@ def process_form_options(opt, form):
     if opt.numerical_jacobian:
         opt = opt.copy(generate_jacobians=False, generate_jacobian_apply=False)
 
-    if opt.form is None:
+    if opt.form == '':
         opt = opt.copy(form=form)
 
-    if opt.classname is None:
+    if opt.classname == '':
         opt = opt.copy(classname="{}Operator".format(form))
 
-    if opt.filename is None:
+    if opt.filename == '':
         opt = opt.copy(filename="{}_{}_file.hh".format(get_option("target_name"), opt.classname))
 
     if opt.block_preconditioner_diagonal or opt.block_preconditioner_offdiagonal:
diff --git a/python/dune/codegen/options_form.yaml b/python/dune/codegen/options_form.yaml
index 2399ab67..511e3e56 100644
--- a/python/dune/codegen/options_form.yaml
+++ b/python/dune/codegen/options_form.yaml
@@ -24,7 +24,7 @@ blockstructured:
     helpstr: "Use block structure"
 classname:
     type: string
-    default: None
+    default: ""
     helpstr: "The name of the C++ class to generate"
 constant_transformation_matrix:
     type: boolean
@@ -36,7 +36,7 @@ control:
     helpstr: "Generate operator of derivative w.r.t. the control variable"
 control_variable:
     type: string
-    default: None
+    default: ""
     helpstr: "Name of control variable in UFL file"
 diagonal_transformation_matrix:
     type: boolean
@@ -60,11 +60,11 @@ fastdg:
     helpstr: "Use FastDGGridOperator from PDELab."
 filename:
     type: string
-    default: None
+    default: ""
     helpstr: "The filename to use for this LocalOperator"
 form:
     type: string
-    default: None
+    default: ""
     helpstr: "The name of the UFL object representing the form in the UFL file"
 generate_jacobian_apply:
     type: boolean
@@ -96,7 +96,7 @@ numerical_jacobian:
     helpstr: "use numerical jacobians (only makes sense, if uflpdelab for some reason fails to generate analytic jacobians)"
 objective_function:
     type: string
-    default: None
+    default: ""
     helpstr: "Name of form representing the objective function in UFL file"
 print_transformations:
     type: boolean
@@ -112,7 +112,7 @@ quadrature_mixins:
     helpstr: "A comma separated list of mixin identifiers to use for quadrature. Currently implemented: generic, sumfact"
 quadrature_order:
     type: string
-    default: ''
+    default: ""
     helpstr: "Quadrature order used for all integrals."
 simplify:
     type: boolean
@@ -160,11 +160,11 @@ vectorization_blockstructured_tail:
     helpstr: "Try to fully vectorize block structuring even when 'nunmber_of_blocks' is not divisible by vector length"
 vectorization_blockstructured_tail_ordering:
     type: string
-    default: 'consecutive'
+    default: "consecutive"
     helpstr: "Ordering of the tail w.r.t the vectorized loop. Possible values: consecutive|blocked"
 vectorization_horizontal:
     type: string
-    default: None
+    default: ""
     helpstr: "an explicit value for horizontal vectorization read by the 'explicit' strategy"
 vectorization_jacobians:
     type: boolean
@@ -172,7 +172,7 @@ vectorization_jacobians:
     helpstr: "Whether to attempt to vectorize jacobians (takes time, often not needed)"
 vectorization_list_index:
     type: string
-    default: None
+    default: ""
     helpstr: "Which vectorization to pick from a list (only valid with vectorization_strategy=fromlist)."
 vectorization_not_fully_vectorized_error:
     type: boolean
@@ -180,7 +180,7 @@ vectorization_not_fully_vectorized_error:
     helpstr: "throw an error if nonquadloop vectorization did not fully vectorize"
 vectorization_padding:
     type: string
-    default: None
+    default: ""
     helpstr: "an explicit value for the allowed padding in vectorization"
 vectorization_quadloop:
     type: boolean
@@ -196,5 +196,5 @@ vectorization_target:
     helpstr: "The cost function target for the 'target' cost model. Only needed to verify the cost model itself, do not use light-heartedly!!!"
 vectorization_vertical:
     type: string
-    default: None
+    default: ""
     helpstr: "an explicit value for vertical vectorization read by the 'explicit' strategy"
\ No newline at end of file
diff --git a/test/nonlinear/diffusivewave.mini b/test/nonlinear/diffusivewave.mini
index e02ec956..f7c67fe5 100644
--- a/test/nonlinear/diffusivewave.mini
+++ b/test/nonlinear/diffusivewave.mini
@@ -21,7 +21,7 @@ geometry_mixins = equidistant, sumfact_equidistant | expand sf
 sumfact = 0, 1 | expand sf
 fastdg = 0, 0 | expand sf
 
-[formcompiler.operator]
+[formcompiler.poisson]
 geometry_mixins = equidistant, sumfact_equidistant | expand sf
 sumfact = 0, 1 | expand sf
 fastdg = 0, 0 | expand sf
diff --git a/test/poisson/opcount_poisson_dg_symdiff.mini b/test/poisson/opcount_poisson_dg_symdiff.mini
index 186c4344..90df6bdc 100644
--- a/test/poisson/opcount_poisson_dg_symdiff.mini
+++ b/test/poisson/opcount_poisson_dg_symdiff.mini
@@ -10,4 +10,6 @@ extension = vtu
 [formcompiler]
 opcounter = 1
 instrumentation_level = 3
+
+[formcompiler.r]
 geometry_mixins = equidistant
-- 
GitLab