"""
    Example of a simultaneous fit model for REFL1D, as generated by the application.
"""
import numpy
import os
from refl1d.names import *
from math import *
import warnings
import matplotlib.cbook
warnings.filterwarnings("ignore",category=matplotlib.cbook.mplDeprecation)
warnings.simplefilter('ignore', UserWarning)

# run ref_l/160601 #############################################################
# Maximum Q-value ##############################################################
q_min = 0.0
q_max = 1.0

reduced_file = "/SNS/users/m2d/__data725.txt"

Q, R, dR, dQ = numpy.loadtxt(reduced_file).T
i_min = min([i for i in range(len(Q)) if Q[i]>q_min])
i_max = max([i for i in range(len(Q)) if Q[i]<q_max])+1

# SNS data is FWHM
dQ_std = dQ/2.35
probe725 = QProbe(Q[i_min:i_max], dQ_std[i_min:i_max], data=(R[i_min:i_max], dR[i_min:i_max]))

# Materials ####################################################################
Si = SLD(name='Si', rho=2.07, irho=0.0)
Electrolyte = SLD(name='Electrolyte', rho=5.48256, irho=0.0)
SiOx = SLD(name='SiOx', rho=1.67867, irho=0.0)
Cu = SLD(name='Cu', rho=6.42039, irho=0.0)
aSi = SLD(name='aSi', rho=1.40821, irho=0.0)
PAA = SLD(name='PAA', rho=3.0, irho=0.0)


# Film definition ##############################################################
sample725 = (  Electrolyte(0, 14.5834) | PAA(361.52, 98.88) | aSi(512.311, 11.917) | Cu(181.161, 1.0) | SiOx(5.0, 2.23098) | Si )

sample725['SiOx'].thickness.range(5.0, 33.0)
sample725['SiOx'].material.rho.range(1.0, 4.0)
sample725['SiOx'].interface.range(1.0, 5.0)
sample725['Cu'].thickness.range(99.0, 222.0)
sample725['Cu'].material.rho.range(6.0, 7.0)
sample725['Cu'].interface.range(1.0, 10.0)
sample725['aSi'].thickness.range(10.0, 700.0)
sample725['aSi'].material.rho.range(1.0, 2.1)
sample725['aSi'].interface.range(1.0, 33.0)
sample725['PAA'].thickness.range(10.0, 444.0)
sample725['PAA'].material.rho.range(1.0, 3.0)
sample725['PAA'].interface.range(1.0, 111.0)



probe725.intensity=Parameter(value=1.0,name='normalization')
probe725.background.range(0.0, 1e-06)
sample725['Electrolyte'].material.rho.range(0.0, 6.0)
sample725['Electrolyte'].interface.range(1.0, 66.0)

################################################################################

expt725 = Experiment(probe=probe725, sample=sample725)

# run m2d/243027 #############################################################
# Maximum Q-value ##############################################################
q_min = 0.0
q_max = 1.0

reduced_file = "/SNS/users/m2d/__data723.txt"

Q, R, dR, dQ = numpy.loadtxt(reduced_file).T
i_min = min([i for i in range(len(Q)) if Q[i]>q_min])
i_max = max([i for i in range(len(Q)) if Q[i]<q_max])+1

# SNS data is FWHM
dQ_std = dQ/2.35
probe723 = QProbe(Q[i_min:i_max], dQ_std[i_min:i_max], data=(R[i_min:i_max], dR[i_min:i_max]))

# Materials ####################################################################
air = SLD(name='air', rho=0.0, irho=0.0)
Si = SLD(name='Si', rho=2.07, irho=0.0)
aSi = SLD(name='aSi', rho=1.89237, irho=0.0)
Cu = SLD(name='Cu', rho=6.47662, irho=0.0)
SiOx = SLD(name='SiOx', rho=2.63338, irho=0.0)


# Film definition ##############################################################
sample723 = (  Si(0, 5.0) | SiOx(19.4988, 1.00002) | Cu(177.157, 10.7316) | aSi(549.002, 14.5254) | air )

sample723['aSi'].thickness.range(250.0, 650.0)
sample723['aSi'].material.rho.range(1.0, 2.0)
sample723['aSi'].interface.range(1.0, 33.0)
sample723['Cu'].thickness.range(100.0, 200.0)
sample723['Cu'].material.rho.range(1.0, 7.0)
sample723['Cu'].interface.range(1.0, 33.0)
sample723['SiOx'].thickness.range(10.0, 33.0)
sample723['SiOx'].material.rho.range(1.0, 4.0)
sample723['SiOx'].interface.range(1.0, 10.0)



probe723.intensity=Parameter(value=1.0,name='normalization')
probe723.background=Parameter(value=0.0,name='background')
sample723['Si'].interface.range(1.0, 5.0)

################################################################################

expt723 = Experiment(probe=probe723, sample=sample723)

# run ref_l/160617 #############################################################
# Maximum Q-value ##############################################################
q_min = 0.0
q_max = 1.0

reduced_file = "/SNS/users/m2d/__data727.txt"

Q, R, dR, dQ = numpy.loadtxt(reduced_file).T
i_min = min([i for i in range(len(Q)) if Q[i]>q_min])
i_max = max([i for i in range(len(Q)) if Q[i]<q_max])+1

# SNS data is FWHM
dQ_std = dQ/2.35
probe727 = QProbe(Q[i_min:i_max], dQ_std[i_min:i_max], data=(R[i_min:i_max], dR[i_min:i_max]))

# Materials ####################################################################
Si = SLD(name='Si', rho=2.07, irho=0.0)
Electrolyte = SLD(name='Electrolyte', rho=5.48, irho=0.0)
SiOx = SLD(name='SiOx', rho=2.18805, irho=0.0)
Cu = SLD(name='Cu', rho=6.63232, irho=0.0)
aSi = SLD(name='aSi', rho=1.79408, irho=0.0)
SEI = SLD(name='SEI', rho=1.19976, irho=0.0)
PAA = SLD(name='PAA', rho=1.99995, irho=0.0)


# Film definition ##############################################################
sample727 = (  Electrolyte(0, 17.2561) | PAA(137.796, 39.271) | SEI(42.0405, 2.62061) | aSi(695.848, 10.9552) | Cu(178.377, 2.76232) | SiOx(5.16418, 2.5141) | Si )

sample727['SiOx'].thickness.range(5.0, 33.0)
sample727['SiOx'].material.rho.range(1.0, 4.0)
sample727['SiOx'].interface.range(1.0, 5.0)
sample727['Cu'].thickness.range(99.0, 222.0)
sample727['Cu'].material.rho.range(6.0, 7.0)
sample727['Cu'].interface.range(1.0, 10.0)
sample727['aSi'].thickness.range(10.0, 700.0)
sample727['aSi'].material.rho.range(1.0, 2.1)
sample727['aSi'].interface.range(1.0, 33.0)
sample727['SEI'].thickness.range(10.0, 100.0)
sample727['SEI'].material.rho.range(1.0, 4.0)
sample727['SEI'].interface.range(1.0, 10.0)
sample727['PAA'].thickness.range(10.0, 200.0)
sample727['PAA'].material.rho.range(1.0, 2.0)
sample727['PAA'].interface.range(1.0, 40.0)



probe727.intensity=Parameter(value=1.0003,name='normalization')
probe727.background.range(0.0, 8e-06)
sample727['Electrolyte'].interface.range(1.0, 40.0)

################################################################################

expt727 = Experiment(probe=probe727, sample=sample727)

# Constraints ##################################################################
sample723['aSi'].thickness = sample725['aSi'].thickness
sample723['Cu'].thickness = sample725['Cu'].thickness
sample723['SiOx'].thickness = sample725['SiOx'].thickness
sample723['Cu'].material.rho = sample725['Cu'].material.rho
sample723['SiOx'].material.rho = sample725['SiOx'].material.rho
sample723['aSi'].material.rho = sample725['aSi'].material.rho
sample723['Si'].interface = sample725['SiOx'].interface
sample723['SiOx'].interface = sample725['Cu'].interface


problem = FitProblem([expt725,expt723,expt727])
