# Last edited on 2021-01-11 12:41:31 by jstolfi

SHELL := /bin/bash

# Simulator:
PROGDIR := ${HOME}/programs/c/NEUROMAT/nmsim_elem_net_simulate
PROG := nmsim_elem_net_simulate

# Creator:
GEN_PROGDIR := ${HOME}/programs/c/NEUROMAT/nmsim_elem_net_create_test
GEN_PROG := nmsim_elem_net_create_test

all: compile delete create simulate plot

NUM_BANDS := 11
NUM_LAYERS := 2
BAND_WIDTH := 1
CLOSED := 1

NNE := ${shell echo "${NUM_BANDS}*${NUM_LAYERS}*${BAND_WIDTH}" | bc -lq}
NNE1 := ${shell echo "${NNE}-1" | bc -lq}

# Tags of this run and of a comparison run.
TAG=A
CMP=A

# Trace/plot time ranges 
TTOT := 10000
TRACE_TMIN := 1800
TRACE_TMAX := 9800
PLOT_TMIN := 1800
PLOT_TMAX := 2200

# Range of input currents for full-scale test:
   SUBTAG := FU
IEVEN := +35.0
IODD := +35.0
IWIDTH := 1
PLOT_IMIN := -5.0
PLOT_IMAX := +40.0

# Times for the ON and OFF pulses:
TIME_ON_0  := 1810
TIME_ON_1  := 1909
#TIME_ON_2  := 2008

# Range of activity to plot:
RHO_MAX := 1.000

TAGSUB := ${TAG}_${SUBTAG}
CMPSUB := ${CMP}_${SUBTAG}
ifneq "/${TAG}" "/${CMP}"
  TAGSUBS := ${CMPSUB} ${TAGSUB}
else
  TAGSUBS := ${TAGSUB}
endif

include ${TAG}.make

NMSIM_FILE := data_nmsim_${TAG}.txt
GEN_PREFIX := out/sim
OUT_PREFIX := ${GEN_PREFIX}_${TAGSUB}

INPUT_OPTS := ${shell make_input_options.sh \
    ${NUM_BANDS} ${NUM_LAYERS} ${BAND_WIDTH} \
    ${TTOT} ${IEVEN} ${IODD} ${IWIDTH} \
    ${TIME_ON_0} ${TIME_ON_1} \
  }

TRACE_OPTS := ${shell make_trace_options.sh  \
    ${NUM_BANDS} ${NUM_LAYERS} ${BAND_WIDTH} \
    ${TRACE_TMIN} ${TRACE_TMAX} \
  }
          
compile:
	cd ${PROGDIR} && make all
	cd ${GEN_PROGDIR} && make all

delete:
	rm -fv ${NMSIM_FILE}

create: ${NMSIM_FILE}

${NMSIM_FILE}: compile 
	@echo "${NUM_LAYERS} ${NNE} ${NNE1}"
	${GEN_PROG} \
            -numBands ${NUM_BANDS} \
	    -numLayers ${NUM_LAYERS} \
	    -bandWidth ${BAND_WIDTH} \
            -closed ${CLOSED} \
	    -phiType ${PHI} \
            -V_tau ${V_tau} \
            -M_R ${M_R} \
            -M_tau ${M_tau} \
            -WAvg ${WMIN} ${WMAX} \
            -WDev ${WDEV} \
          > ${NMSIM_FILE}

simulate: compile ${NMSIM_FILE}
	-mkdir -p out
	-rm -f out/*.{txt,png}
	time ${PROG} \
          -netFile ${NMSIM_FILE} \
          -timeStep 1.0 \
          -nSteps ${TTOT} \
          ${INPUT_OPTS} \
          ${TRACE_OPTS} \
          -outPrefix ${OUT_PREFIX}

plot:
	plot_traces.sh 1 \
          0 ${NNE} \
          ${OUT_PREFIX} \
          ${PLOT_TMIN} ${PLOT_TMAX} \
          ${PLOT_IMIN} ${PLOT_IMAX} \
          ${RHO_MAX}