### author: F. Trillaud <ftrillaudp@gmail.com>
### 03/24/2020

import os
import shutil as su
import matplotlib as mpl
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
### Latex font:
from matplotlib import rc

### Paths:
myPath_case = "/media/psf/Codes/Multiphysics/Channel"
myPath_salome = myPath_case+"/Salome/MedFiles"
myPath_saturne = myPath_case+"/CodeSaturne"
cmdPath_saturne = myPath_saturne+"/Case/SCRIPTS"
myPost_saturne = myPath_saturne+"/Case/RESU"
myMeshPath_saturne = myPath_saturne+"/MESH"
myPath_aster = myPath_case+"/CodeAster"
myPathCase_aster = myPath_aster+"/Case"
myMeshPath_aster = myPath_aster+"/Case/MESH"
cmd_aster = '/opt/Aster14.4/bin/as_run environment.export'

su.copy(myPath_salome+"/fluid.med", myMeshPath_saturne)
su.copy(myPath_salome+"/fluid.med", myMeshPath_saturne+"/fluid_0.med")
su.copy(myPath_salome+"/fluid.med", myMeshPath_aster)
su.copy(myPath_salome+"/fluid.med", myMeshPath_aster+"/fluid_0.med")
su.copy(myPath_salome+"/structure.med", myMeshPath_aster)
su.copy(myPath_salome+"/structure.med", myMeshPath_aster+"/structure_0.med")

maxIter = 50 ## check that it is at least iqual to the number of samples in "inputs.py"
rel_tol = 0.01
rel_er = 1000*rel_tol
ii = 0
data = list()
data_file = myPathCase_aster+"/RESU/elasticEnergy.dat"
iter_list, rel_er_list = list(), list()

### Figure #######
# plot with various axes scales
plt.figure(3, figsize = (7,6))
plt.style.use('grayscale')
plt.rc('xtick', labelsize = 10)    # fontsize of the tick labels
plt.rc('ytick', labelsize = 10)    # fontsize of the tick labels
plt.grid(True)
#plt.ylim((0, 2*rel_er))
plt.xlabel(r'Number of iterations (-)', fontsize = 12)
plt.ylabel(r'$e^\mathrm{rel}_\mathrm{r}$ ($\%$)', fontsize = 12)

while ((rel_er >= rel_tol) and (ii <= maxIter)):
  ### Run Code_saturne
  os.chdir(cmdPath_saturne); os.system('./runcase')
  ### Copy fiels from Code_saturne to Code_Aster:
  listOfFolders = os.listdir(myPost_saturne)
  su.copy(myPost_saturne+"/"+listOfFolders[-1]+"/postprocessing/results_pressure_surface.med", myMeshPath_aster)
  ### Backup file to see evolution:
  su.copy(myMeshPath_saturne+"/fluid.med", myMeshPath_saturne+"/fluid_"+str(ii+1)+".med")

  ### Run Code_aster
  os.chdir(myPathCase_aster)
  os.system(cmd_aster)
  ### Copy deformed meshes to new nesh for Code_Saturne:
  su.copy(myMeshPath_aster+"/deformedFluid.med", myMeshPath_saturne+"/fluid.med")
  ### Change the meshes for new computation in Code_Aster:
  su.copy(myMeshPath_aster+"/deformedFluid.med", myMeshPath_aster+"/fluid.med")
  su.copy(myMeshPath_aster+"/deformedStructure.med", myMeshPath_aster+"/structure.med")
  ### Backup files to see evolution:
  su.copy(myMeshPath_aster+"/deformedStructure.med", myMeshPath_aster+"/structure_"+str(ii+1)+".med")
  
  if (ii > 0):
    ### Read in lines in file:
    inFile = open(data_file, 'r')
    data = [float(line) for line in inFile.readlines()]
    inFile.close()
    ### Relative error on elastic energy:
    rel_er = abs((data[-2]-data[-1])/data[-2])
  else:
    rel_er = 1000*rel_tol
  iter_list.append(ii)
  rel_er_list.append(rel_er)
  ii += 1
  
  plt.semilogy(iter_list, rel_er_list, "-ro")
  plt.pause(0.01)

plt.savefig(myPath_case+"/convergence.png", format = "png", dpi = 300, bbox_inches = "tight")
plt.show()
  
print("\nNumber of iterations = {0:d}".format(ii))
print("\n***** END OF FSC ****\n")
