Module SortFiles

@author: Alexandre Sac–Morane alexandre.sac-morane@uclouvain.be

Sort the files.

Expand source code

#-------------------------------------------------------------------------------
# Librairies
#-------------------------------------------------------------------------------

import os
from pathlib import Path

#-------------------------------------------------------------------------------
# Functions
#-------------------------------------------------------------------------------

def index_to_str(j):
  '''
  An integer is converted to a float with 3 components
  '''
  if j < 10:
      j_str = '00'+str(j)
  elif 10 <= j and j < 100:
      j_str = '0'+str(j)
  else :
      j_str = str(j)
  return j_str

#-------------------------------------------------------------------------------

def Sort_vtk(dict_pp, dict_user):
    '''
    Sort .vtk files generated by MOOSE.
    '''
    j = 0
    j_str = index_to_str(j)
    filepath = Path('PF_Cement_Solidification_other_'+j_str+'.pvtu')
    while filepath.exists():
        for i_proc in range(dict_user['n_proc']):
            os.rename('PF_Cement_Solidification_other_'+j_str+'_'+str(i_proc)+'.vtu','vtk/PF_Cement_Solidification_other_'+j_str+'_'+str(i_proc)+'.vtu')
        os.rename('PF_Cement_Solidification_other_'+j_str+'.pvtu','vtk/PF_Cement_Solidification_other_'+j_str+'.pvtu')
        j = j + 1
        j_str = index_to_str(j)
        filepath = Path('PF_Cement_Solidification_other_'+j_str+'.pvtu')

    # save the last iteration
    dict_pp['last_j_str'] = index_to_str(j-1)
    dict_pp['last_j'] = j-1

#-------------------------------------------------------------------------------

def Sort_vtk_reduced(dict_pp, dict_user):
    '''
    Sort .vtk files generated by MOOSE.
    Only a part of the files are saved (other deleted).
    '''
    # initialization
    L_time_pp_extracted = []
    L_hyd_pp_extracted = []

    # search the total number of files
    j = 0
    j_str = index_to_str(j)
    filepath = Path('PF_Cement_Solidification_other_'+j_str+'.pvtu')
    while filepath.exists():
        j = j + 1
        j_str = index_to_str(j)
        filepath = Path('PF_Cement_Solidification_other_'+j_str+'.pvtu')

    # consider the criteria on the maximum number of save
    if j-1 > dict_user['n_vtk_max']:
        f_save = (j-1)/dict_user['n_vtk_max']
    else :
        f_save = 1
    # save index
    i_save = 0

    # sort or delete
    j = 0
    j_str = index_to_str(j)
    filepath = Path('PF_Cement_Solidification_other_'+j_str+'.pvtu')
    j_save = 0
    j_save_str = index_to_str(j_save)
    while filepath.exists():
        # save
        if j >= f_save*i_save:
            for i_proc in range(dict_user['n_proc']):
                os.rename('PF_Cement_Solidification_other_'+j_str+'_'+str(i_proc)+'.vtu','vtk/PF_Cement_Solidification_other_'+j_save_str+'_'+str(i_proc)+'.vtu')
            # delete ...
            os.remove('PF_Cement_Solidification_other_'+j_str+'.pvtu')
            # ... and rewrite
            file_to_write = open('vtk/PF_Cement_Solidification_other_'+j_save_str+'.pvtu','w')
            line = '\n'+\
                   '\n'+\
                   '\t\n'+\
                   '\t\t\n'+\
                   '\t\t\t\n'+\
                   '\t\t\t\n'+\
                   '\t\t\t\n'+\
                   '\t\t\t\n'+\
                   '\t\t\n'+\
                   '\t\t\n'+\
                   '\t\t\t\n'+\
                   '\t\t\t\n'+\
                   '\t\t\t\n'+\
                   '\t\t\n'+\
                   '\t\t\n'+\
                   '\t\t\t\n'+\
                   '\t\t\n'
            file_to_write.write(line)
            for i_proc in range(dict_user['n_proc']):
                line = '\t\t\n'
                file_to_write.write(line)
            line = '\t\n\n'
            file_to_write.write(line)
            file_to_write.close()
            # next save
            j_save = j_save + 1
            j_save_str = index_to_str(j_save)
            # extract time and hydratation
            L_time_pp_extracted.append(dict_pp['time_pp'][j])
            L_hyd_pp_extracted.append(dict_pp['hyd_pp'][j])
            # iterate
            i_save = i_save + 1
        # delete
        else:
            for i_proc in range(dict_user['n_proc']):
                os.remove('PF_Cement_Solidification_other_'+j_str+'_'+str(i_proc)+'.vtu')
            os.remove('PF_Cement_Solidification_other_'+j_str+'.pvtu')
        # next one
        j = j + 1
        j_str = index_to_str(j)
        filepath = Path('PF_Cement_Solidification_other_'+j_str+'.pvtu')
    # save the last iteration
    dict_pp['last_j_str'] = index_to_str(j_save-1)
    dict_pp['last_j'] = j_save-1
    # save extraction
    dict_pp['L_time_pp_extracted'] = L_time_pp_extracted
    dict_pp['L_hyd_pp_extracted'] = L_hyd_pp_extracted

Functions

def index_to_str()

An integer is converted to a float with 3 components.

Expand source code

def index_to_str(j):
  '''
  An integer is converted to a float with 3 components
  '''
  if j < 10:
      j_str = '00'+str(j)
  elif 10 <= j and j < 100:
      j_str = '0'+str(j)
  else :
      j_str = str(j)
  return j_str
def Sort_vtk()

Sort .vtk files generated by MOOSE.

Expand source code

def Sort_vtk(dict_pp, dict_user):
    '''
    Sort .vtk files generated by MOOSE.
    '''
    j = 0
    j_str = index_to_str(j)
    filepath = Path('PF_Cement_Solidification_other_'+j_str+'.pvtu')
    while filepath.exists():
        for i_proc in range(dict_user['n_proc']):
            os.rename('PF_Cement_Solidification_other_'+j_str+'_'+str(i_proc)+'.vtu','vtk/PF_Cement_Solidification_other_'+j_str+'_'+str(i_proc)+'.vtu')
        os.rename('PF_Cement_Solidification_other_'+j_str+'.pvtu','vtk/PF_Cement_Solidification_other_'+j_str+'.pvtu')
        j = j + 1
        j_str = index_to_str(j)
        filepath = Path('PF_Cement_Solidification_other_'+j_str+'.pvtu')

    # save the last iteration
    dict_pp['last_j_str'] = index_to_str(j-1)
    dict_pp['last_j'] = j-1