# Advanced topics

This section covers several advanced topics related to OVITO’s scripting interface:

> - Saving and loading pipelines

> - Specifying the number of processor cores

## Saving and loading pipelines

The `ovito.io.export_file()` function lets you to save the output data computed by a pipeline to disk. But how do you save the definition of the pipeline itself, including all the modifiers, to a file? The current version of OVITO can save the entire scene to a .ovito state file using the `Scene.save()` method. Thus, in order to save a `Pipeline` you need to first make it part of the scene using its `add_to_scene()` method:

    import ovito
    from ovito.io import import_file
    from ovito.modifiers import CoordinationAnalysisModifier

    pipeline = import_file("input/simulation.dump")
    pipeline.modifiers.append(CoordinationAnalysisModifier(cutoff = 3.4))
    # ...
    pipeline.add_to_scene()
    ovito.scene.save("output/mypipeline.ovito")

Unfortunately, there currently exists no corresponding Python function for loading a scene back into memory from a .ovito state file. The only way to restore the scene state is to preload the .ovito file when executing a batch script. This is done by using the -o command line option of the ovitos script interpreter:

    ovitos -o mypipeline.ovito script.py

The code in `script.py` will now be executed in a context where the `Scene` was already initialized with the state loaded from the .ovito scene file. Instead of setting up a completely new pipeline, the script can therefore work with the existing pipeline that was restored from the state file:

    import ovito

    pipeline = ovito.scene.pipelines[0]
    pipeline.source.load("input/second_file.dump") # Replace pipeline input.
    pipeline.modifiers[0].cutoff = 3.1             # Adjust modifier params.
    data = pipeline.compute()

## Specifying the number of processor cores

Some computation functions of OVITO have been parallelized in order to make use of all available processor cores. This includes, for example, computationally expensive modifiers such as `PolyhedralTemplateMatchingModifier`, `ClusterAnalysisModifier` or `ComputePropertyModifier`, and the software-based rendering engines `TachyonRenderer` and `OSPRayRenderer`.

By default, these parallelized algorithms will make use of all available cores of your CPU. This default number is determined by OVITO using the function QThread.idealThreadCount(), which can be queried as follows:

    >>> from PySide2.QtCore import QThread
    >>> print(QThread.idealThreadCount())
    4

Sometimes it is desirable to restrict OVITO to a single CPU core only, for example when running multiple instances of OVITO in parallel. This can be achieved in two ways. The graphical application ovito and the script interpreter ovitos both support the command line parameter --nthreads, which allows overriding the number of CPU cores used by parallel algorithms:

    ovitos --nthreads 1 script.py

The second option is to set the `OVITO_THREAD_COUNT` environment variable prior to invoking or importing OVITO. This approach always works: for the GUI application, the script interpreter ovitos, but also for scripts running in an external Python interpreter that imports the `ovito` module:

    export OVITO_THREAD_COUNT=1
    python script.py