Running simulations#

Running a nextnano simulation from Python takes two lines: load the input file, then execute it. Everything the simulator needs — the executable, the license, the database and where to write — comes from your configuration, so none of it has to be repeated at the call site.

import nextnanopy as nn

my_input = nn.InputFile(r'C:\Users\Your_User\Documents\nextnano\input files\example.in')
my_input.execute()

print(my_input.folder_output)  # where the results ended up

execute() blocks until the simulation has finished and prints the log as it runs. Use execute(show_log=False) to keep the console quiet; the .log file is written to the output folder either way.

Changing a variable and running the modified file#

The input variables are parsed when the file is loaded, and are reachable by name:

for var in my_input.variables:
    print(var.name, '=', var.value)   # or var.text for the input file line

my_input['xmax'].value                # same as my_input.variables['xmax'].value

set_variable() gives one a new value, and can rewrite its comment at the same time:

my_input.set_variable('xmax', value=120, comment='widened for this run')

Save the file before executing it. The order matters: execute() runs the file that is on disk at fullpath, so a variable changed in memory alone never reaches the simulator.

my_input.save()
my_input.execute()

save() does not overwrite by default — example.in is written as example_0.in, then example_1.in, and so on — and it points fullpath at the file it just wrote, which is the one execute() picks up. Pass overwrite=True to write back over the original, or give a path of your own:

my_input.save(r'C:\Users\Your_User\Documents\nextnano\input files\example_xmax_120.in')

To keep the modified files out of your folders altogether, save(temp=True) writes to a temporary location that is cleaned up when the process exits. The simulation itself still runs in the folder the input file came from.

Where the output goes#

By default the results land in <outputdirectory>/<input file name>/, where outputdirectory comes from the configuration. Two arguments change that:

overwrite=False (the default)

Never write into the output of an earlier run: when the directory is already taken, an index is appended — example_0, example_1, and so on. With overwrite=True the existing directory is used as it is. Nothing is deleted in either case; the new run simply writes next to what is there.

create_subdirectory=False

Write into outputdirectory itself instead of a subfolder named after the input file. overwrite then has no effect.

Use folder_output to locate the output folder of the last run and read its files with nextnanopy:

from pathlib import Path

my_input.execute()
data = nn.DataFile(Path(my_input.folder_output) / 'bias_00000' / 'bandedges.dat')

Overriding the configuration for one run#

Any command line argument of the nextnano product can be passed straight to execute(), and takes precedence over the configuration for that run only:

my_input.execute(outputdirectory=r'C:\Users\Your_User\Downloads', threads=4)

The same works for exe, license and database — useful to run one file against a different nextnano version without touching your configuration. See Set up the configuration for what each option means, and InputFile for the InputFile reference.

Using an old style txt license file#

nextnanopy can be used with the old style txt license. Besides pointing the license option of your configuration at the .txt file, pass an extra command line argument when you execute:

input_file = nn.InputFile("path_to_your_input_file")
input_file.execute(no_file_options="--old")

It works the same way for sweeps:

sweep = nn.Sweep(sweep_parameters, input_file_path)
sweep.save()
sweep.execute(no_file_options="--old")