## How to use self-defined model in Simulation's yml file?

How do I use this algorithm? What does that parameter do?
JerryChen97
Posts: 2
Joined: 27 Jul 2021, 18:17

### How to use self-defined model in Simulation's yml file?

Hi guys,

Recently I've started using the new Simulation module to help avoid re-invent lots of wheels. But I found that it's not clear how to import my self-defined models without messing with the source code of TeNPy. Does anyone have some suggestions on this?
Johannes
Posts: 282
Joined: 21 Jul 2018, 12:52
Location: UC Berkeley

### Re: How to use self-defined model in Simulation's yml file?

You just need to import the file in python, which contains the model definition; then you can just use the "name" of the model in the simulation parameters. To import that file with the definition, you have two options:
1. Either you use the command line option -1 of tenpy-run or python -m tenpy specifying the module you want to import. Note that this requires the python file with the model to be in PYTHONPATH.
2. You can use a small wrapper python script that imports the corresponding file, and then calls tenpy.console_main or tenpy.run_simulation as a python function, depending on whether you want to parse command line arguments yourself or not.
So the total wrapper script can be as simple as

Code: Select all

import my_model_definitions

if __name__ == "__main__":
import tenpy
tenpy.console_main()

If it is in the same folder as my_model_definitions.py, you can then call it as python tenpy_wrapper.py my_config.yml

If you want, you could even add the tenpy.console_main() at the end of the model file to make that executable.
JerryChen97
Posts: 2
Joined: 27 Jul 2021, 18:17

### Re: How to use self-defined model in Simulation's yml file?

Johannes wrote: 11 Aug 2021, 17:14 You just need to import the file in python, which contains the model definition; then you can just use the "name" of the model in the simulation parameters. To import that file with the definition, you have two options:
1. Either you use the command line option -1 of tenpy-run or python -m tenpy specifying the module you want to import. Note that this requires the python file with the model to be in PYTHONPATH.
2. You can use a small wrapper python script that imports the corresponding file, and then calls tenpy.console_main or tenpy.run_simulation as a python function, depending on whether you want to parse command line arguments yourself or not.
So the total wrapper script can be as simple as

Code: Select all

import my_model_definitions

if __name__ == "__main__":
import tenpy
tenpy.console_main()

If it is in the same folder as my_model_definitions.py, you can then call it as python tenpy_wrapper.py my_config.yml

If you want, you could even add the tenpy.console_main() at the end of the model file to make that executable.
Thanks a lot! I will try it out