%matplotlib inline

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from simmit import smartplus as sim
from simmit import identify as iden
import os

dir = os.path.dirname(os.path.realpath('__file__'))

"""
Explanation: Orientation density functions
End of explanation
"""


x = np.arange(0,182,2)
path_data = dir + '/data/'
peak_file = 'Npeaks0.dat'

y = sim.get_densities(x, path_data, peak_file, False)
fig = plt.figure()

plt.grid(True)
plt.plot(x,y, c='black')

"""
Explanation: In this Python Notebook we will show how to properly run a simulation of a composite material, providing the ODF (orientation density function) of the reinforcments.
Such identification procedure require:
1. Proper ODF peak data
1. Proper composite properties
2. A proper numerical model (here a composite model for laminate constitutive model)
End of explanation
"""


NPhases_file = dir + '/data/Nellipsoids0.dat'
NPhases = pd.read_csv(NPhases_file, delimiter=r'\s+', index_col=False, engine='python')
NPhases[::]

umat_name = 'MIMTN' #This is the 5 character code for the Mori-Tanaka homogenization for composites with a matrix and ellipsoidal reinforcments
nstatev = 0

nphases = 2 #The number of phases
num_file = 0 #The num of the file that contains the subphases
int1 = 20 
int2 = 20

psi_rve = 0.
theta_rve = 0.
phi_rve = 0.

props = np.array([nphases, num_file, int1, int2, 0])

path_data = 'data'
path_results = 'results'
Nfile_init = 'Nellipsoids0.dat'
Nfile_disc = 'Nellipsoids1.dat'

nphases_rve = 36
num_phase_disc = 1

sim.ODF_discretization(nphases_rve, num_phase_disc, 0., 180., umat_name, props, path_data, peak_file, Nfile_init, Nfile_disc, 1)

NPhases_file = dir + '/data/Nellipsoids1.dat'
NPhases = pd.read_csv(NPhases_file, delimiter=r'\s+', index_col=False, engine='python')

#We plot here the five first phases 
NPhases[:5]

#Plot the concentration and the angle
c, angle = np.loadtxt(NPhases_file, usecols=(4,5), skiprows=2, unpack=True)

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()

# the histogram of the data
xs = np.arange(0,180,5)
rects1 = ax1.bar(xs, c, width=5, color='r', align='center')

ax2.plot(x, y, 'b-')

ax1.set_xlabel('X data')
ax1.set_ylabel('Y1 data', color='g')
ax2.set_ylabel('Y2 data', color='b')

ax1.set_ylim([0,0.025])
ax2.set_ylim([0,0.25])

plt.show()

#plt.grid(True)
#plt.plot(angle,c, c='black')
plt.show()

#Run the simulation
pathfile = 'path.txt'
nphases = 37 #The number of phases
num_file = 1 #The num of the file that contains the subphases
props = np.array([nphases, num_file, int1, int2])
outputfile = 'results_MTN.txt'
sim.solver(umat_name, props, nstatev, psi_rve, theta_rve, phi_rve, path_data, path_results, pathfile, outputfile)

fig = plt.figure()
outputfile_macro =  dir + '/' + path_results + '/results_MTN_global-0.txt'
e11, e22, e33, e12, e13, e23, s11, s22, s33, s12, s13, s23 = np.loadtxt(outputfile_macro, usecols=(8,9,10,11,12,13,14,15,16,17,18,19), unpack=True)
plt.grid(True)
plt.plot(e11,s11, c='black')

for i in range(8,12):
    outputfile_micro =  dir + '/' + path_results + '/results_MTN_global-0-' + str(i) + '.txt'
    e11, e22, e33, e12, e13, e23, s11, s22, s33, s12, s13, s23 = np.loadtxt(outputfile_micro, usecols=(8,9,10,11,12,13,14,15,16,17,18,19), unpack=True)
    plt.grid(True)
    plt.plot(e11,s11, c='red')

plt.xlabel('Strain')
plt.ylabel('Stress (MPa)')


plt.show()

"""
Explanation: In the previous graph we can see a multi-peak ODF (peaks are modeled using PEARSONVII functions). It actually represent quite well the microstructure of injected plates.
The next step is to discretize the ODF into phases.
The file containing the initial 2-phase microstructure contains the following informations
End of explanation
"""


