Skip to content

Add static constraints for input string parameters of algorithms

If we mistype some parameter value, for example in the example velocities.vm add (distribution: 'boltzmann'):

Traceback (most recent call last):
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/errors.py", line 303, in wrapper
    return func(*args, **kwargs)
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/cli/run_model.py", line 123, in evaluate_prog
    print(f'program output: >>>\n{prog.value}\n<<<')
  File "/usr/lib/python3.10/functools.py", line 981, in __get__
    val = self.func(instance)
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/errors.py", line 212, in wrapper
    raise TextXError(str(err), **get_location_context(obj)) from err
textx.exceptions.TextXError: /mnt/data/ubuntu/work/vre-language/examples/velocities.vm:2:1:  => 'struct = Structure from file '/home/ubuntu/work/vre-language/examples/ar_box.cif'
algo_vel = Algorithm VelocityDistribution ((temperature_K: 100.) [K])
prop_vel = Property ((algorithm: algo_vel), (structure: struct))
print(prop_vel.output_structure.temperature)

algo_vel_1 = Algorithm VelocityDistribution (
                (distribution: 'boltzmann'),
                (temperature_K: 300.) [K],
                (stationary: false),
                (zero_rotation: false))
prop_vel_1 = Property vdf, velocity ((algorithm: algo_vel_1), (structure: struct))
print(prop_vel_1.velocity [bohr/atomic_unit_of_time], prop_vel_1.vdf)
print(prop_vel_1.output_structure.temperature)'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/errors.py", line 303, in wrapper
    return func(*args, **kwargs)
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/cli/__init__.py", line 38, in texts
    clargs.func(clargs)
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/cli/run_model.py", line 163, in main
    evaluate_prog(prog)
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/errors.py", line 305, in wrapper
    process_error(err)
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/errors.py", line 293, in process_error
    raise err.__cause__
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/errors.py", line 208, in wrapper
    return func(*args, **kwargs)
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/typemap.py", line 116, in wrapper
    return checktype_(func(obj), obj.type_)
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/interpreter/instant_executor.py", line 60, in program_value
    vals = [p.value for p in get_children_of_type('Print', self) if p.value]
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/interpreter/instant_executor.py", line 60, in <listcomp>
    vals = [p.value for p in get_children_of_type('Print', self) if p.value]
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/errors.py", line 305, in wrapper
    process_error(err)
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/errors.py", line 293, in process_error
    raise err.__cause__
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/errors.py", line 208, in wrapper
    return func(*args, **kwargs)
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/typemap.py", line 116, in wrapper
    return checktype_(func(obj), obj.type_)
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/interpreter/instant_executor.py", line 575, in amml_property_value
    return amml.Property(self.names, self.struct.value,
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/amml.py", line 519, in __init__
    self.results = results if results is not None else self.get_results()
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/amml.py", line 630, in get_results
    df['results'] = df.apply(lambda x: apply_func(x, apply_algo), axis=1)
  File "/mnt/data/ubuntu/work/python-3.10.12/lib/python3.10/site-packages/pandas/core/frame.py", line 10374, in apply
    return op.apply().__finalize__(self, method="apply")
  File "/mnt/data/ubuntu/work/python-3.10.12/lib/python3.10/site-packages/pandas/core/apply.py", line 916, in apply
    return self.apply_standard()
  File "/mnt/data/ubuntu/work/python-3.10.12/lib/python3.10/site-packages/pandas/core/apply.py", line 1063, in apply_standard
    results, res_index = self.apply_series_generator()
  File "/mnt/data/ubuntu/work/python-3.10.12/lib/python3.10/site-packages/pandas/core/apply.py", line 1081, in apply_series_generator
    results[i] = self.func(v, *self.args, **self.kwargs)
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/amml.py", line 630, in <lambda>
    df['results'] = df.apply(lambda x: apply_func(x, apply_algo), axis=1)
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/amml.py", line 621, in apply_func
    return func(df.structure, df.calculator, df.algorithm)
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/amml.py", line 615, in apply_algo
    return algo.run(struct, constrs=constrs)
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/amml.py", line 471, in run
    with self._class(struct_ase, **cls_params) as algo_obj:
  File "/mnt/data/ubuntu/work/vre-language/src/virtmat/language/utilities/ase_wrappers.py", line 92, in __init__
    assert distribution == 'maxwell-boltzmann'
AssertionError

In this Algorithm the only valid value is maxwell-boltzmann. In other algorithms, there are more than one valid string-type values but they also raise assertion error. This should be converted to user error message. Maybe by applying constraints based on additional specs in the parameters spec.