# Tutorial 3: Creating htc and PBS files

The Wind Energy Toolbox has a workflow for automatically running design load 
bases (DLBs) on Gorm.
This workflow has the following steps:
1. [Create a master Excel sheet defining each case in the DLB](https://gitlab.windenergy.dtu.dk/toolbox/WindEnergyToolbox/blob/master/docs/tutorials/1-creating-master-excel.md)
2. [Create subordinate Excel sheets from each tab in the master Excel sheet](https://gitlab.windenergy.dtu.dk/toolbox/WindEnergyToolbox/blob/master/docs/tutorials/2-creating-subordinate-excels.md)
3. Create htc files and PBS job scripts for each requisite simulation using 
the subordinate Excel files and a master htc file.
4. Submit all PBS job scripts to the cluster
5. Post-process results
6. Visualize results

This tutorial presents how to accomplish Step 3.

Note that it is possible to customize your simulations by skipping/modifying 
steps.
Such a procedure will be discussed in a later tutorial.

If there are any problems with this tutorial, please [submit an issue](
https://gitlab.windenergy.dtu.dk/toolbox/WindEnergyToolbox/issues).

## 1. Background: htc and PBS file creation

The main function used in this tutorial is [dlctemplate.py](https://gitlab.windenergy.dtu.dk/toolbox/WindEnergyToolbox/blob/master/wetb/prepost/dlctemplate.py),
which creates all htc and PBS job scripts for the cases specified in the 
subordinate Excel file folder.

The htc files are the main input files for HAWC2 simulations.
They are created by copying the master htc file in the ```_master/``` folder in 
your htc directory and replacing all of the tags with the values specified in 
the subordinate Excel files.
All of htc files for a single case are saved in a case-specific folder in your 
htc folder.
Thus, if you were running a standard DLB calculation for IEC 61400-1, your 
folder structure after generating your htc files might look like this:
```
|-- $TURB_NAME/
|   |-- $SET_ID/
|   |   |-- DLCs.xlsx
|   |   |-- _master/
|   |   |   |-- $MASTER_NAME.htc
|   |   |-- DLCs/
|   |   |-- htc/
|   |   |   |-- dlc12_iec61400-1ed3/
|   |   |   |   |-- dlc12_wsp04_wdir000_s2001.htc
|   |   |   |   |-- dlc12_wsp04_wdir000_s2002.htc
|   |   |   |   |-- ...
|   |   |   |-- dlc13_iec61400-1ed3/
|   |   |   |-- ...
```

The PBS job scripts are a series of text files that are used to tell the job 
scheduler on the high-performance computing (HPC) cluster how to run each job.
These files end with ".p", and are saved to a folder ```pbs_in/``` that is 
created in the main set ID folder on Gorm.


## 2. Tutorial

There are two ways to call ```dlctemplate.py```.
The first is to call the function directly.
The second is to wrap it in a job scheduler to submit the job to the HPC cluster.
The first option is fine if you have only a few htc files or if the job 
scheduler is not working for some reason.
The second option is generally preferred.

### 2.1 Directly generate htc files

The htc and PBS files can be directly generated by running the following 
commands from the set ID directory:
```
export PATH=/home/python/miniconda3/bin:$PATH
source activate wetb_py3
python /home/MET/repositories/toolbox/WindEnergyToolbox/wetb/prepost/dlctemplate.py --prep
source deactivate
```

The ```export PATH``` command adds the miniconda bin directory to the path, 
which is necessary for the toolbox.
The ```source activate wetb_py3``` and ```source deactivate``` are 
Gorm-specific commands to activate the Wind Energy Toolbox Python environment.
The ```--prep``` option tells the script to run in preparation mode, in which 
case it creates the htc and pbs files.

After running the commands in the above box on Gorm, you should have all of your 
PBS input files in ```pbs_in/``` and all of your htc files in ```htc```.

### 2.2 Generate files using job scheduler

From the set ID folder, run the following code:
```
qsub-wrap.py -f /home/MET/repositories/toolbox/WindEnergyToolbox/wetb/prepost/dlctemplate.py --prep
```


## 3. Issues

If there are any problems with this tutorial, please [submit an issue](
https://gitlab.windenergy.dtu.dk/toolbox/WindEnergyToolbox/issues).
We will try to fix it as soon as possible.

