ECHO=/bin/echo
DLLS = $(addprefix -reference:,$(filter %.dll,$^))
CODE = $(filter %.cs,$^)
MKEXE = mcs -target:exe -out:$@ $(DLLS) $(CODE)
MKLIB = mcs -target:library -out:$@ $(DLLS) $(CODE)

default: Plots.svg log

R1 = 2
R2 = 3
R3 = 4
R4 = 5
R5 = 10
data log: main.exe Makefile
	cat /dev/null | tee data log
	for R in $(R1) $(R2) $(R3) $(R4) $(R5); do \
	mono main.exe -rmax:$$R 1>>data 2>>log; \
	echo | tee --append data log; \
	echo | tee --append data log; \
	done

Plots.svg: data Makefile
	$(ECHO) '\
	set term $(subst .,,$(suffix $@)); \
	set out "$@"; \
	set multiplot layout 2,1; \
	set title "Hydrogen s-wave shooting to F_E(r_{max})=0"; \
	set tics out; \
	set xtics 2; \
	set mxtics 2; \
	set xlabel "r [a_{B}]"; \
	set ylabel "F_E(r)"; \
	set size 0.7,0.5 ;\
	plot [][0:]\
	 "$<" index 1 with lines title "r_{max}=$(R1)" \
	,"$<" index 3 with lines title "r_{max}=$(R2)" \
	,"$<" index 5 with lines title "r_{max}=$(R3)" \
	,"$<" index 9 with lines title "r_{max}=$(R5)" \
	,"$<" index 9 using 1:3 with lines linetype 2 title "exact"; \
	set mytics 2; \
set title "Calculated energy {/:Italic E} as function of {/:Italic r_{max}}"; \
set xlabel "{/:Italic r_{max} [a_{B}]}"; \
	set ylabel "E [Hatree]"; \
	set size 0.7,0.5 ;\
	plot [$(R1)-1:$(R5)+1][-0.6:0]\
 "$<" index 0 with points pointtype 7 linecolor rgb "blue" title "calculated"\
,"$<" index 2 with points pointtype 7 linecolor rgb "blue" notitle \
,"$<" index 4 with points pointtype 7 linecolor rgb "blue" notitle \
,"$<" index 6 with points pointtype 7 linecolor rgb "blue" notitle \
,"$<" index 8 with points pointtype 7 linecolor rgb "blue" notitle \
	,-0.5 with lines linetype 2 title "exact"\
	'| tee log.gpi | gnuplot

main.exe: main.cs hydrogen.cs matlib.dll ; $(MKEXE)

LIBDIR=../../matlib
SRC=\
	$(LIBDIR)/matrix/vector.cs\
	$(LIBDIR)/matrix/matrix.cs\
	$(LIBDIR)/ODE/driver.cs\
	$(LIBDIR)/ODE/rkstep12.cs\
	$(LIBDIR)/ODE/rkstep23.cs\
	$(LIBDIR)/QR/givensqr.cs\
	$(LIBDIR)/roots/jacobian.cs\
	$(LIBDIR)/roots/newton.cs

matlib.dll: $(SRC) ; $(MKLIB)

.PHONEY:clean
clean:
	$(RM) log* data *.exe *.dll *.png *.svg
