get_parameter

  • full name: tenpy.tools.params.get_parameter

  • parent module: tenpy.tools.params

  • type: function

tenpy.tools.params.get_parameter(params, key, default, descr, asarray=False)[source]

Read out a parameter from the dictionary and/or provide default values.

This function provides a similar functionality as params.get(key, default). Unlike dict.get this function writes the default value into the dictionary (i.e. in other words it’s more similar to params.setdefault(key, default)).

This allows the user to save the modified dictionary as meta-data, which gives a concrete record of the actually used parameters and simplifies reproducing the results and restarting simulations.

Moreover, a special entry with the key 'verbose' in the params can trigger this function to also print the used value. A higer verbose level implies more output. If verbose >= 100, it is printed every time it’s used. If verbose >= 2., its printed for the first time time its used. and for verbose >= 1, non-default values are printed the first time they are used. otherwise only for the first use.

Internally, whether a parameter was used is saved in the set params['_used_param']. This is used in unused_parameters() to print a warning if the key wasn’t used at the end of the algorithm, to detect mis-spelled parameters.

Parameters
  • params (dict) – A dicionary of the parameters as provided by the user. If key is not a valid key, params[key] is set to default.

  • key (string) – The key for the parameter which should be read out from the dictionary.

  • default – The default value for the parameter.

  • descr (str) – A short description for verbose output, like ‘TEBD’, ‘XXZ_model’, ‘truncation’.

  • asarray (bool) – If True, convert the result to a numpy array with np.asarray(...) before returning.

Returns

params[key] if the key is in params, otherwise default. Converted to a numpy array, if asarray.

Return type

value

Examples

In the algorithm Engine gets a dictionary of parameters. Beside doing other stuff, it calls tenpy.models.model.NearestNeighborModel.calc_U_bond() with the dictionary as argument, which looks similar like:

>>> from tenpy.tools.params import get_parameter
>>> def model_calc_U(params):
...    dt = get_parameter(params, 'dt', 0.01, 'TEBD')
...    order = get_parameter(params, 'order', 1, 'TEBD')
...    print("calc U with dt =", dt, "and order =", order )
...    # ... calculate exp(-i * dt* H) ....

Then, when you call it without any parameters, it just uses the default value:

>>> model_calc_U(dict())
calc U with dt = 0.01 and order = 1

Of course you can also provide the parameter to use a non-default value:

>>> model_calc_U(dict(dt=0.02))
calc U with dt = 0.02 and order = 1

Increasing the special keyword 'verbose' generally prints more:

>>> model_calc_U(dict(dt=0.02, verbose=1))
parameter 'dt'=0.02 for TEBD
calc U with dt = 0.02 and order = 1
>>> model_calc_U(dict(dt=0.02, verbose=2))
parameter 'dt'=0.02 for TEBD
parameter 'order'=1 (default) for TEBD
calc U with dt = 0.02 and order = 1