{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using the Simulators\n",
    "\n",
    "The bmtk is capable of running a number of different neuronal simulators across various levels of resolutions. \n",
    "\n",
    "BioNet - Biophysical neuronal simulation using BioNet  \n",
    "PointNet - Point neuron simulation using NEST  \n",
    "FilterNet - Filtering based models using LGNModel  \n",
    "PopNet - Population rate models using DiPDE  \n",
    "<s>MintNet - Convolutional neural nets with TensorFlow</s>\n",
    "\n",
    "Different simulators will have different features and will require different parameters and options to run. But the bmtk uses the same framework for running the different types of simulators and producing output - utilizing the SONATA framework. The following will in general describe how to go about setting up and running one of the various simulators. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Building a network\n",
    "\n",
    "A main philosophy of the bmtk is to separate the processes of building a network model and running a simulation versus other simulators which use the same script to build a network and then immedietly run a simulation. The main advantages of doing so is so that:\n",
    "* Different parts of a network can be built independently by different teams.\n",
    "* Allows models to be shared.\n",
    "* Models can be readily ported from one simulator to another.\n",
    "* Faster to run a large pre-built network across multiple simulations without having to recalculate the connection matricies every time.\n",
    "* Easier to adjust and update parameters between simulations.\n",
    "\n",
    "The bmtk uses the SONATA format to store networks. See the [previous tutorial for building networks](NetworkBuilder_Intro.ipynb) for more information."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setting up a configuration file\n",
    "\n",
    "To create the a configuration file and simulation enviornment you can either download and copy a simulator [environment template directory](../environments). Alternatively you can build it using the following:\n",
    "\n",
    "```bash\n",
    "$ python -m bmtk.utils.sim_setup -n network_dir/ <options> [bionet|pointnet|popnet|...]\n",
    "```\n",
    "\n",
    "All the parameters to setup and run a simulation is stored in the file *config.json*. More detail about the configuration file can be found in the [sonata documentation](https://github.com/AllenInstitute/sonata/blob/master/docs/SONATA_DEVELOPER_GUIDE.md#tying-it-all-together---the-networkcircuit-config-file), but we will provide a brief overview of the various sections:\n",
    "\n",
    "### The mainfest\n",
    "\n",
    "```json\n",
    "\"manifest\": {\n",
    "    \"$BASE_DIR\": \"${configdir}\",\n",
    "    \"$OUTPUT_DIR\": \"$BASE_DIR/output\",\n",
    "    \"$INPUT_DIR\": \"$BASE_DIR/../nwb_files\",\n",
    "    ...\n",
    "},\n",
    "```\n",
    "\n",
    "An optional section that allows users to create variables and references that can be used in other parts of the config.\n",
    "\n",
    "\n",
    "### Run-time and conditions\n",
    "```json\n",
    "\"run\": {\n",
    "    \"tstop\": 3000.0,\n",
    "    \"dt\": 0.1,\n",
    "    \"spike_threshold\": -15,\n",
    "    \"nsteps_block\": 5000,\n",
    "    \"dL\": 20.0\n",
    "},\n",
    "\n",
    "\"conditions\": {\n",
    "    \"celsius\": 34.0,\n",
    "    \"v_init\": -80\n",
    "},\n",
    "```\n",
    "\n",
    "Parameters for how long a simulation will run (in milliseconds) and other simulation parameters and conditions - depending on the type of simulator.\n",
    "\n",
    "### Components\n",
    "```json\n",
    "\"components\": {\n",
    "    \"morphologies_dir\": \"$COMPONENT_DIR/morphologies\",\n",
    "    \"synaptic_models_dir\": \"$COMPONENT_DIR/synaptic_models\",\n",
    "    \"mechanisms_dir\":\"$COMPONENT_DIR/mechanisms\",\n",
    "    \"biophysical_neuron_models_dir\": \"$COMPONENT_DIR/biophysical_neuron_templates\",\n",
    "    \"point_neuron_models_dir\": \"$COMPONENT_DIR/point_neuron_templates\"\n",
    "},\n",
    "```\n",
    "\n",
    "List of directories contains model files for individual cells and synapses.\n",
    "\n",
    "### Inputs\n",
    "\n",
    "```json\n",
    "\"inputs\": {\n",
    "    \"spike_trains\": {\n",
    "        \"input_type\": \"spikes\",\n",
    "        \"module\": \"nwb\",\n",
    "        \"input_file\": \"$INPUT_DIR/lgn_spikes.nwb\",\n",
    "        \"node_set\": \"lgn\"\n",
    "    },\n",
    " \t\"current_clamp\": {\n",
    "        \"input_type\": \"current_clamp\",\n",
    "        \"module\": \"IClamp\",\n",
    "        \"node_set\": [0, 1, 2, 3, 4],\n",
    "        \"amp\": 200.1178,\n",
    "        \"delay\": 100.0,\n",
    "        \"duration\": 2000.0\n",
    "    }\n",
    "},\n",
    "``` \n",
    "\n",
    "Specifying the types of inputs the network will receive during the simulation. Including spike trains/rates for virtual or external cells, current clamp stimulation, extracellular stimulation, etc.\n",
    "\n",
    "### Output and reports\n",
    "\n",
    "```json\n",
    "\"output\":{ \n",
    "    \"log_file\": \"log.txt\",\n",
    "    \"output_dir\": \"$OUTPUT_DIR\",\n",
    "    \"spikes_file\": \"spikes.h5\",\n",
    "    \"overwrite_output_dir\": true\n",
    "},\n",
    "\"reports\": {\n",
    "    \"calcium_concentration\": {\n",
    "        \"cells\": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n",
    "        \"variable_name\": \"cai\",\n",
    "        \"module\": \"membrane_report\",\n",
    "        \"file_name\": \"cell_vars.h5\",\n",
    "        \"sections\": \"soma\"\n",
    "    },\n",
    "    \"ecp\": {\n",
    "        \"cells\": [0],\n",
    "        \"variable_name\": \"v\",\n",
    "        \"module\": \"extracellular\",\n",
    "        \"electrode_positions\": \"$COMPONENT_DIR/recXelectrodes/linear_electrode.csv\",\n",
    "        \"ecp_file\": \"ecp.h5\",\n",
    "        \"electrode_channels\": \"all\",\n",
    "        \"contributions_dir\": \"ecp_contributions\"\n",
    "    }\n",
    "},\n",
    "```\n",
    "\n",
    "The output section is used to specify the main directory where output is written to, as well as log file location and default outputs. Unless absolute paths are specified any file created during the simulation will be written into the directory specified in \"output/output_dir\".\n",
    "\n",
    "The reports allows user to specify more advanced types of simulation reporting like, depending on the simulator, calcium concentration or local field potentials\n",
    "\n",
    "### Networks\n",
    "```json\n",
    "\"networks\": {\n",
    "    \"nodes\": [\n",
    "    {\n",
    "        \"nodes_file\": \"$NETWORK_DIR/v1_nodes.h5\",\n",
    "        \"node_types_file\": \"$NETWORK_DIR/v1_node_types.csv\"\n",
    "    },\n",
    "    {\n",
    "        \"nodes_file\": \"$NETWORK_DIR/lgn_nodes.h5\",\n",
    "        \"node_types_file\": \"$NETWORK_DIR/lgn_node_types.csv\"\n",
    "    }\n",
    "    ],\n",
    "\n",
    "    \"edges\": [\n",
    "    {\n",
    "        \"edges_file\": \"$NETWORK_DIR/v1_v1_edges.h5\",\n",
    "        \"edge_types_file\": \"$NETWORK_DIR/v1_v1_edge_types.csv\"\n",
    "    },\n",
    "    {\n",
    "        \"edges_file\": \"$NETWORK_DIR/lgn_v1_edges.h5\",\n",
    "        \"edge_types_file\": \"$NETWORK_DIR/lgn_v1_edge_types.csv\"\n",
    "    }\n",
    "    ]\n",
    "}\n",
    "```\n",
    "\n",
    "Section to specify the different nodes and edges files used during the simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Running the simulation file\n",
    "\n",
    "Once you have setup your configuration and put the network and model files in their correct places the running a simulation can be done without any extra programming. \n",
    "\n",
    "```bash\n",
    "$ python run_simulator.py config.json\n",
    "```\n",
    "\n",
    "The simulation will run and save the results in the files specified in the output and reports sections of the config. The bmtk also includes a small but growing list of tools for analyzing and graphing the output describe in future chapters.\n",
    "\n",
    "The bmtk also allows for users to develop custom python code that will executed during the simulation runtime. Including code to tell the simulator to load customized cell and synaptic models, or how to adjust connection weights before and during simulations. Theses options will vary from simulator to simulator and will be futher describe in each simulators documention.\n",
    "\n",
    "#### Running in Parallel mode\n",
    "\n",
    "Some of the simulation engines, BioNet and PointNet, allow you to run a network across multple machines/nodes using MPI, but typically require slightly different commands:\n",
    "\n",
    "#### BioNet (NEURON)\n",
    "```bash\n",
    "$ mpirun -n $NNODES nrniv nrniv -mpi -python run_bionet.py config.json\n",
    "```\n",
    "\n",
    "#### PointNet (NEST)\n",
    "```bash\n",
    "$ mpirun -n $NNODES python run_pointnet.py config.json\n",
    "```\n",
    "\n",
    "If you're running the simulations on a HPC cluster make sure to follow the instructions as required by the workload scheduler being used."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
