<commit_before># file: openfoamSimulation.py
# author: <NAME> (<EMAIL>)
# description: Implementation of the class `OpenFOAMSimulation`.


import os

import numpy
from scipy import signal
from matplotlib import pyplot

from ..simulation import Simulation
from ..force import Force


class OpenFOAMSimulation(Simulation):
  """Contains info about a OpenFOAM simulation.
  Inherits from class Simulation.
  """
  def __init__(self, description=None, directory=os.getcwd(), **kwargs):
    """Initializes by calling the parent constructor.

    Parameters
    ----------
    description: string, optional
      Description of the simulation;
      default: None.
    directory: string, optional
      Directory of the simulation;
      default: present working directory.
    """
    super(OpenFOAMSimulation, self).__init__(description=description, 
                                             directory=directory, 
                                             software='openfoam', 
                                             **kwargs)

  def read_forces(self, display_coefficients=False, labels=None):
    """Reads forces from files.

    Parameters
    ----------
    display_coefficients: boolean, optional
      Set to 'True' if force coefficients are required; 
      default: False (i.e. forces).
    labels: list of strings, optional
      Label of each force to read;
      default: None.
    """
    if display_coefficients:
      info = {'directory': '{}/postProcessing/forceCoeffs'.format(self.directory),
              'description': 'force-coefficients'}
      if not labels:
        labels = ['$C_d$', '$C_l$']
    else:
      info = {'directory': '{}/postProcessing/forces'.format(self.directory),
              'description': 'forces'}
      if not labels:
        labels = ['$F_x$', '$F_y$']
    info['usecols'] = (0, 2, 3)
    info['labels'] = labels
    # backward compatibility from 2.2.2 to 2.0.1
    if not os.path.isdir(info['directory']):
      info['directory'] = '{}/forces'.format(self.directory)
      info['usecols'] = (0, 1, 2)
    # end of backward compatibility
    print('[info] reading {} in {} ...'.format(info['description'], info['directory']))
    subdirectories = sorted(os.listdir(info['directory']))
    times = numpy.empty(0)
    force_x, force_y = numpy.empty(0), numpy.empty(0)
    for subdirectory in subdirectories:
      forces_path = '{}/{}/{}.dat'.format(info['directory'], subdirectory, os.path.basename(info['directory']))
      with open(forces_path, 'r') as infile:
        t, fx, fy = numpy.loadtxt(infile, dtype=float, comments='#', 
                                  usecols=info['usecols'], unpack=True)
      times = numpy.append(times, t)
      force_x, force_y = numpy.append(force_x, fx), numpy.append(force_y, fy)
    # set Force objects
    self.forces.append(Force(times, force_x, label=labels[0]))
    self.forces.append(Force(times, force_y, label=labels[1]))

  def read_maximum_cfl(self, file_path):
    """Reads the instantaneous maximum CFL number from a given log file.
<commit_msg>Add argument to specify the forces folder<commit_after># file: openfoamSimulation.py
# author: <NAME> (<EMAIL>)
# description: Implementation of the class `OpenFOAMSimulation`.


import os

import numpy
from scipy import signal
from matplotlib import pyplot

from ..simulation import Simulation
from ..force import Force


class OpenFOAMSimulation(Simulation):
  """Contains info about a OpenFOAM simulation.
  Inherits from class Simulation.
  """
  def __init__(self, description=None, directory=os.getcwd(), **kwargs):
    """Initializes by calling the parent constructor.

    Parameters
    ----------
    description: string, optional
      Description of the simulation;
      default: None.
    directory: string, optional
      Directory of the simulation;
      default: present working directory.
    """
    super(OpenFOAMSimulation, self).__init__(description=description, 
                                             directory=directory, 
                                             software='openfoam', 
                                             **kwargs)

  def read_forces(self, 
                  display_coefficients=False, 
                  labels=None,
                  forces_folder='postProcessing/forces',
                  force_coefficients_folder='postProcessing/forceCoeffs',
                  usecols=(0, 2, 3)):
    """Reads forces from files.

    Parameters
    ----------
    display_coefficients: boolean, optional
      Set to 'True' if force coefficients are required; 
      default: False (i.e. forces).
    labels: list of strings, optional
      Label of each force to read;
      default: None.
    forces_folder: string, optional
      Relative path from the simulation directory to the folder containing
      the forces;
      default: 'postProcessing/forces'.
    force_coefficients_folder: string, optional
      Relative path from the simulation directory to the folder containing
      the force coefficients;
      default: 'postProcessing/forceCoeffs'.
    usecols: tuple of integers, optional
      Index of columns to read from file, including the time-column index;
      default: (0, 2, 3).
    """
    if display_coefficients:
      info = {'directory': os.path.join(self.directory, 
                                        force_coefficients_folder),
              'file-name': 'forceCoeffs.dat',
              'description': 'force-coefficients'}
      if not labels:
        labels = ['$C_d$', '$C_l$']
    else:
      info = {'directory': os.path.join(self.directory,
                                        forces_folder),
              'file-name': 'forces.dat',
              'description': 'forces'}
      if not labels:
        labels = ['$F_x$', '$F_y$']
    info['usecols'] = usecols
    info['labels'] = labels
    # backward compatibility from 2.2.2 to 2.0.1
    if not os.path.isdir(info['directory']):
      info['directory'] = '{}/forces'.format(self.directory)
      info['usecols'] = (0, 1, 2)
    # end of backward compatibility
    print('[info] reading {} in {} ...'.format(info['description'], info['directory']))
    subdirectories = sorted(os.listdir(info['directory']))
    times = numpy.empty(0)
    force_x, force_y = numpy.empty(0), numpy.empty(0)
    for subdirectory in subdirectories:
      forces_path = os.path.join(info['directory'], subdirectory, info['file-name'])
      with open(forces_path, 'r') as infile:
        t, fx, fy = numpy.loadtxt(infile, dtype=float, comments='#', 
                                  usecols=info['usecols'], unpack=True)
      times = numpy.append(times, t)
      force_x, force_y = numpy.append(force_x, fx), numpy.append(force_y, fy)
    # set Force objects
    self.forces.append(Force(times, force_x, label=labels[0]))
    self.forces.append(Force(times, force_y, label=labels[1]))

  def read_maximum_cfl(self, file_path):
    """Reads the instantaneous maximum CFL number from a given log file.

{'fraction_non_alphanumeric': 0.12143777745954461, 'fraction_numerical': 0.00529858227123013, 'mean_word_length': 2.5578196637799286, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6691710', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1906, 'n_words': 538}