text:
/*
 * The MIT License
 *
 * Copyright (c) 2013-2021 The University of Utah
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "BCHelper.h"

//-- C++ Includes --//
#include <vector>
#include <iostream>

#include <boost/foreach.hpp>

//-- Uintah Includes --//
#include <Core/Grid/Patch.h>
#include <Core/Grid/Variables/Iterator.h>
#include <Core/Grid/Variables/CellIterator.h> // Uintah::Iterator
#include <Core/Exceptions/ProblemSetupException.h>
#include <Core/Grid/BoundaryConditions/BoundCond.h>
#include <Core/Grid/BoundaryConditions/BCGeomBase.h>
#include <Core/Grid/BoundaryConditions/BCDataArray.h>
#include <Core/Grid/BoundaryConditions/BoundCondBase.h>

//-- SpatialOps includes --//
#include <spatialops/OperatorDatabase.h>

//-- Wasatch Includes --//
#include <CCA/Components/Wasatch/FieldTypes.h>
#include <CCA/Components/Wasatch/FieldAdaptor.h>
#include <CCA/Components/Wasatch/ParseTools.h>
#include <CCA/Components/Wasatch/ParticlesHelper.h>
#include <CCA/Components/Wasatch/Expressions/Pressure.h>
#include <CCA/Components/Wasatch/Expressions/BoundaryConditions/BoundaryConditions.h>

/**
 * \file    BCHelper.cc
 * \author  Tony Saad
 */

namespace WasatchCore {
  // Given a string BC type (Dirichlet, Neumann,...), this function returns a BndCondTypeEnum
  // of supported boundary condition types
  BndCondTypeEnum select_bc_type_enum( const std::string& bcTypeStr )
  {
    if      ( bcTypeStr == "Dirichlet" )     return DIRICHLET;
    else if ( bcTypeStr == "Neumann" )       return NEUMANN;
    else                                     return UNSUPPORTED;
  }

  // Given a string boundary type (Wall, Velocity, Outflow,...), this function returns a BndTypeEnum
  // of supported boundary types
  BndTypeEnum select_bnd_type_enum( const std::string& bndTypeStr )
  {
    if      ( bndTypeStr == "Wall"     )  return WALL;
    else if ( bndTypeStr == "Velocity" )  return VELOCITY;
    else if ( bndTypeStr == "Open"     )  return OPEN;
    else if ( bndTypeStr == "Outflow"  )  return OUTFLOW;
    else if ( bndTypeStr == "Symmetry"  ) return SYMMETRY;
    else if ( bndTypeStr == "Interior"  ) return INTERIOR;
    else if ( bndTypeStr == "None"
              || bndTypeStr == "User"  )  return USER;
    else                                  return INVALID;
  }

  // Given a BndCondTypeEnum (DIRICHLET,...), this function returns a string
  // of supported boundary condition types
  std::string bc_type_enum_to_string( const BndCondTypeEnum bcTypeEnum )
  {
    switch (bcTypeEnum) {
      case DIRICHLET:
        return "Dirichlet";
        break;
      case NEUMANN:
        return "Neumann";
        break;
      default:
        return "Unsupported";
        break;
    }
  }

  // Given a BndTypeEnum (WALL, VELOCITY,...), this function returns a string
  // of supported boundary types
  const std::string bnd_type_enum_to_string( const BndTypeEnum bndTypeEnum )
  {
    switch (bndTypeEnum) {
      case WALL:
        return "Wall";
        break;
      case VELOCITY:
        return "Velocity";
        break;
      case OPEN:
        return "Open";
        break;
      case OUTFLOW:
        return "Outflow";
        break;
      case SYMMETRY:
        return "Symmetry";
        break;
      case INTERIOR:
        return "Interior";
        break;
      case USER:
        return "User";
        break;
      default:
        return "Invalid";
        break;
    }
  }

  template<typename OST>
  OST& operator<<( OST& os, const BndTypeEnum bndTypeEnum )
  {
    os << bnd_type_enum_to_string(bndTypeEnum);
    return os;
  }

  template<typename OST>
  OST& operator<<( OST& os, const BndCondTypeEnum bcTypeEnum )
  {
    os << bc_type_enum_to_string(bcTypeEnum);
    return os;
  }

  // This function returns true if the boundary condition is applied in the same direction
  // as the staggered field. For example, xminus/xplus on a XVOL field.
  template <typename FieldT>
  bool is_staggered_normal( const Uintah::Patch::FaceType face ){
    const Direction staggeredLocation = get_staggered_location<FieldT>();
    switch (staggeredLocation) {
      case XDIR:
        return ( (face==Uintah::Patch::xminus || face==Uintah::Patch::xplus)); break;
      case YDIR:
        return ( (face==Uintah::Patch::yminus || face==Uintah::Patch::yplus)); break;
      case ZDIR:
        return ( (face==Uintah::Patch::zminus || face==Uintah::Patch::zplus)); break;
      default: return false; break;
    }
    return false;
  }

  // This function returns true if the boundary condition is applied in the same direction
  // as the staggered field. For example, xminus/xplus on a XVOL field.
  template<typename FieldT>
  bool is_plus_side( const Uintah::Patch::FaceType face ){
    const Direction staggeredLocation = get_staggered_location<FieldT>();
    switch (staggeredLocation) {
      case XDIR: return (face==Uintah::Patch::xplus);  break;
      case YDIR: return (face==Uintah::Patch::yplus);  break;
      case ZDIR: return (face==Uintah::Patch::zplus);  break;
      default:   return false; break;
    }
    return false;
  }

  //============================================================================

  bool BndCondSpec::operator==(const BndCondSpec& l) const
  {
    return (   l.varName == varName
            && l.functorName == functorName
            && l.value == value
            && l.bcType == bcType
            && l.bcValType == bcValType);
  };

  bool BndCondSpec::operator==(const std::string& varNameNew) const
  {
    return ( varNameNew == varName);
  };

  void BndCondSpec::print() const
  {
    using namespace std;
    cout << "  var:     " << varName << endl
         << "  type:    " << bcType << endl
         << "  value:   " << value << endl;
    if( !functorName.empty() )
      cout << "  functor: " << functorName << endl;
  };

  bool BndCondSpec::is_functor() const
  {
    return (bcValType == FUNCTOR_TYPE);
  };

  //============================================================================

  // returns true if this Boundary has parts of it on patchID
  bool BndSpec::has_patch(const int& patchID) const
  {
    return std::find(patchIDs.begin(), patchIDs.end(), patchID) != patchIDs.end();
  }

  // find the BCSpec associated with a given variable name
  const BndCondSpec* BndSpec::find(const std::string& varName) const
  {
    std::vector<BndCondSpec>::const_iterator it = std::find(bcSpecVec.begin(), bcSpecVec.end(), varName);
    if (it != bcSpecVec.end()) {
      return &(*it);
    } else {
      return nullptr;
    }
  }

  // find all the BCSpec associated with a given variable name
  std::vector<BndCondSpec> BndSpec::find_all(const std::string& varName) const
  {
    std::vector<BndCondSpec> matches;
    for (auto i = bcSpecVec.begin(), toofar = bcSpecVec.end(); i != toofar; ++i)
      if (*i == varName)
        matches.push_back(*i);
    return matches;
  }

  // find the BCSpec associated with a given variable name - non-const version
  const BndCondSpec* BndSpec::find(const std::string& varName)
  {
    std::vector<BndCondSpec>::iterator it = std::find(bcSpecVec.begin(), bcSpecVec.end(), varName);
    if (it != bcSpecVec.end()) {
      return &(*it);
    } else {
      return nullptr;
    }
  }

  // check whether this boundary has any bcs specified for varName
  bool BndSpec::has_field(const std::string& varName) const
  {
    std::vector<BndCondSpec>::const_iterator it = std::find(bcSpecVec.begin(), bcSpecVec.end(), varName);
    if (it != bcSpecVec.end()) {
      return true;
    } else {
      return false;
    }
  }

  // print information about this boundary
  void BndSpec::print() const
  {
    using namespace std;
    cout << "Boundary: " << name << " face: " << face << " BndType: " << type << endl;
    for (vector<BndCondSpec>::const_iterator it=bcSpecVec.begin(); it != bcSpecVec.end(); ++it) {
      (*it).print();
    }
  }

  //============================================================================

  //****************************************************************************
  /**
   *
   *  \brief Helps with staggered fields.
   *
   */
  //****************************************************************************

  void pack_uintah_iterator_as_spatialops( const Uintah::Patch::FaceType& face,
                                           const Uintah::Patch* const patch,
                                           Uintah::Iterator& bndIter,
                                           BoundaryIterators& myBndIters )
  {
    using SpatialOps::IntVec;

    std::vector<IntVec>& extraBndSOIter        = myBndIters.extraBndCells;
    std::vector<IntVec>& intBndSOIter          = myBndIters.interiorBndCells;
    std::vector<IntVec>& extraPlusBndCells     = myBndIters.extraPlusBndCells;

    std::vector<IntVec> neboInteriorBndSOIter;
    std::vector<IntVec> neboExtraBndSOIter;

    std::vector<IntVec>& intEdgeSOIter         = myBndIters.interiorEdgeCells;

    bool plusEdge[3];
    bool minusEdge[3];

    minusEdge[0] = patch->getBCType(Uintah::Patch::xminus) != Uintah::Patch::Neighbor;
    plusEdge [0] = patch->getBCType(Uintah::Patch::xplus ) != Uintah::Patch::Neighbor;
    minusEdge[1] = patch->getBCType(Uintah::Patch::yminus) != Uintah::Patch::Neighbor;
    plusEdge [1] = patch->getBCType(Uintah::Patch::yplus ) != Uintah::Patch::Neighbor;
    minusEdge[2] = patch->getBCType(Uintah::Patch::zminus) != Uintah::Patch::Neighbor;
    plusEdge [2] = patch->getBCType(Uintah::Patch::zplus ) != Uintah::Patch::Neighbor;

    int i=-1, j=-1;
    switch (face) {
      case Uintah::Patch::xminus:
      case Uintah::Patch::xplus: i=1; j=2; break;
      case Uintah::Patch::yminus:
      case Uintah::Patch::yplus: i=0; j=2; break;
      case Uintah::Patch::zminus:
      case Uintah::Patch::zplus: i=0; j=1; break;
      default:{
        std::ostringstream msg;
        msg << "ERROR: invalid face specification encountered\n"
            << "\n\t" << __FILE__ << " : " << __LINE__ << std::endl;
        throw std::runtime_error( msg.str() );
      }
    }

    // save pointer to the Uintah iterator. This will be needed for expressions that require access to the
    // native uintah iterators, such as the pressure expression.
    myBndIters.extraBndCellsUintah = bndIter;

    DBGBC << "---------------------------------------------------\n";
    DBGBC << "Face = " << face << std::endl;

    // MAJOR WARNING HERE - WHEN WE MOVE TO RUNTIME GHOST CELLS, WE NEED TO USE THE APPROPRIATE PATCH OFFSET
    const Uintah::IntVector patchCellOffset = patch->getExtraCellLowIndex(1);
    const Uintah::IntVector interiorPatchCellOffset = patch->getCellLowIndex();
    Uintah::IntVector unitNormal = patch->faceDirection(face); // this is needed to construct interior cells
    Uintah::IntVector bcPointIJK;

    Uintah::IntVector edgePoint;
    const Uintah::IntVector idxHi = patch->getCellHighIndex() - Uintah::IntVector(1,1,1);// - patchCellOffset;
    const Uintah::IntVector idxLo = patch->getCellLowIndex();

    for( bndIter.reset(); !bndIter.done(); ++bndIter ){
      bcPointIJK = *bndIter - patchCellOffset;
      extraBndSOIter.push_back(IntVec(bcPointIJK.x(), bcPointIJK.y(), bcPointIJK.z()));

      edgePoint = *bndIter - unitNormal;
      if( ((edgePoint[i] == idxHi[i]) && plusEdge[i]  ) ||
          ((edgePoint[j] == idxHi[j]) && plusEdge[j]  ) ||
          ((edgePoint[i] == idxLo[i]) && minusEdge[i] ) ||
          ((edgePoint[j] == idxLo[j]) && minusEdge[j] ) )
      {
        edgePoint -= patchCellOffset;
        intEdgeSOIter.push_back( IntVec(bcPointIJK[0], bcPointIJK[1], bcPointIJK[2]) );
      }

      bcPointIJK -= unitNormal;
      intBndSOIter.push_back(IntVec(bcPointIJK.x(), bcPointIJK.y(), bcPointIJK.z()));

      bcPointIJK = *bndIter - interiorPatchCellOffset - unitNormal;
      neboInteriorBndSOIter.push_back(IntVec(bcPointIJK.x(), bcPointIJK.y(), bcPointIJK.z()));

      bcPointIJK = *bndIter - interiorPatchCellOffset;
      neboExtraBndSOIter.push_back(IntVec(bcPointIJK.x(), bcPointIJK.y(), bcPointIJK.z()));
    }

    // if we are on a plus face, we will most likely need a plus-face iterator for staggered fields
    if (face == Uintah::Patch::xplus || face == Uintah::Patch::yplus || face == Uintah::Patch::zplus ){
      for( bndIter.reset(); !bndIter.done(); ++bndIter ){
        bcPointIJK = *bndIter - patchCellOffset + unitNormal;
        extraPlusBndCells.push_back(IntVec(bcPointIJK.x(), bcPointIJK.y(), bcPointIJK.z()));
      }
    }

    // convert the svol extra cell boundary iterator to a spatial mask
    const int ng = get_n_ghost<SVolField>();
    SpatialOps::IntVec bcMinus, bcPlus;
    WasatchCore::get_bc_logicals( patch, bcMinus, bcPlus );
    const SpatialOps::BoundaryCellInfo bcInfo = SpatialOps::BoundaryCellInfo::build<SVolField>(bcMinus,bcPlus);
    const SpatialOps::GhostData gd(ng);
    const SpatialOps::MemoryWindow window = WasatchCore::get_memory_window_for_masks<SVolField>( patch );

    // pack the interior-cells spatial mask
    myBndIters.svolInteriorCellSpatialMask = new SpatialOps::SpatialMask<SVolField>(window, bcInfo, gd, neboInteriorBndSOIter);
#   ifdef HAVE_CUDA
    myBndIters.svolInteriorCellSpatialMask->add_consumer(GPU_INDEX);
#   endif

    // pack the extra-cells spatial mask
    myBndIters.svolExtraCellSpatialMask = new SpatialOps::SpatialMask<SVolField>(window, bcInfo, gd, neboExtraBndSOIter);
#   ifdef HAVE_CUDA
    myBndIters.svolExtraCellSpatialMask->add_consumer(GPU_INDEX);
#   endif

    const SpatialOps::MemoryWindow xwindow = WasatchCore::get_memory_window_for_masks<XVolField>( patch );
    const SpatialOps::BoundaryCellInfo xBCInfo = SpatialOps::BoundaryCellInfo::build<XVolField>(bcMinus,bcPlus);
    myBndIters.xvolExtraCellSpatialMask = new SpatialOps::SpatialMask<XVolField>(xwindow, xBCInfo, gd, neboExtraBndSOIter);
#   ifdef HAVE_CUDA
    myBndIters.xvolExtraCellSpatialMask->add_consumer(GPU_INDEX);
#   endif

    const SpatialOps::MemoryWindow ywindow = WasatchCore::get_memory_window_for_masks<YVolField>( patch );
    const SpatialOps::BoundaryCellInfo yBCInfo = SpatialOps::BoundaryCellInfo::build<YVolField>(bcMinus,bcPlus);
    myBndIters.yvolExtraCellSpatialMask = new SpatialOps::SpatialMask<YVolField>(ywindow, yBCInfo, gd, neboExtraBndSOIter);
#   ifdef HAVE_CUDA
    myBndIters.yvolExtraCellSpatialMask->add_consumer(GPU_INDEX);
#   endif

    const SpatialOps::MemoryWindow zwindow = WasatchCore::get_memory_window_for_masks<ZVolField>( patch );
    const SpatialOps::BoundaryCellInfo zBCInfo = SpatialOps::BoundaryCellInfo::build<ZVolField>(bcMinus,bcPlus);
    myBndIters.zvolExtraCellSpatialMask = new SpatialOps::SpatialMask<ZVolField>(zwindow, zBCInfo, gd, neboExtraBndSOIter);
#   ifdef HAVE_CUDA
    myBndIters.zvolExtraCellSpatialMask->add_consumer(GPU_INDEX);
#   endif
  }

  //============================================================================
  // Template specialization for the BoundaryIterators get_spatial_mask function
  template<typename FieldT>
  SpatialOps::SpatialMask<FieldT>*
  BoundaryIterators::get_spatial_mask(bool interior) const
  {
    // should never request a spatial_mask of an unsupported type. Supported types are SVol, X-Y-ZVol
    std::ostringstream msg;
    msg << "ERROR: It looks like you were trying to retrieve a spatial mask of an unsupported field type. "
    << " Supported types are: SVol, XVol, YVol, and ZVol." << std::endl;
    throw Uintah::ProblemSetupException( msg.str(), __FILE__, __LINE__ );
    return nullptr;
  }

  template<>
  SpatialOps::SpatialMask<SVolField>*
  BoundaryIterators::get_spatial_mask(bool interior) const
  {
    if (interior) return svolInteriorCellSpatialMask;
    return svolExtraCellSpatialMask;
  }

  template<>
  SpatialOps::SpatialMask<SpatialOps::SSurfXField>*
  BoundaryIterators::get_spatial_mask(bool interior) const
  {
    return nullptr;
  }

  template<>
  SpatialOps::SpatialMask<SpatialOps::SSurfYField>*
  BoundaryIterators::get_spatial_mask(bool interior) const
  {
    return nullptr;
  }

  template<>
  SpatialOps::SpatialMask<SpatialOps::SSurfZField>*
  BoundaryIterators::get_spatial_mask(bool interior) const
  {
    return nullptr;
  }

  template<>
  SpatialOps::SpatialMask<XVolField>*
  BoundaryIterators::get_spatial_mask(bool interior) const
  {
    return xvolExtraCellSpatialMask;
  }

  template<>
  SpatialOps::SpatialMask<YVolField>*
  BoundaryIterators::get_spatial_mask(bool interior) const
  {
    return yvolExtraCellSpatialMask;
  }

  template<>
  SpatialOps::SpatialMask<ZVolField>*
  BoundaryIterators::get_spatial_mask(bool interior) const
  {
    return zvolExtraCellSpatialMask;
  }

  template<>
  SpatialOps::SpatialMask<ParticleField>*
  BoundaryIterators::get_spatial_mask(bool interior) const
  {
    return nullptr;
  }

  //============================================================================

  //************************************************************************************************
  //
  //                          IMPLEMENTATION
  //
  //************************************************************************************************

  //------------------------------------------------------------------------------------------------

  BCHelper::BCHelper( const Uintah::LevelP& level,
                     Uintah::SchedulerP& sched,
                     const Uintah::MaterialSet* const materials )
  : materials_   (materials   )
  {
    const Uintah::PatchSet* const allPatches = sched->getLoadBalancer()->getPerProcessorPatchSet(level);
    const Uintah::PatchSubset* const localPatches = allPatches->getSubset( Uintah::Parallel::getMPIRank() );
    localPatches_ = new Uintah::PatchSet;
    localPatches_->addEach( localPatches->getVector() );
    parse_boundary_conditions();
  }

  //------------------------------------------------------------------------------------------------

  BCHelper::~BCHelper()
  {
    typedef MaskMapT::iterator it_type;
    typedef PatchIDBndItrMapT::iterator secondIt;
    for(it_type iterator = bndNamePatchIDMaskMap_.begin(); iterator != bndNamePatchIDMaskMap_.end(); iterator++) {
      PatchIDBndItrMapT pidBndItr = iterator->second;
      for(secondIt sit = pidBndItr.begin(); sit != pidBndItr.end(); sit++) {
        delete sit->second.svolExtraCellSpatialMask;
        delete sit->second.xvolExtraCellSpatialMask;
        delete sit->second.yvolExtraCellSpatialMask;
        delete sit->second.zvolExtraCellSpatialMask;
      }
    }
    delete localPatches_;
  }

  //------------------------------------------------------------------------------------------------

  void BCHelper::add_boundary_condition( const std::string& bndName,
                                         const BndCondSpec& bcSpec )
  {
    using namespace std;
    if ( bndNameBndSpecMap_.find(bndName) != bndNameBndSpecMap_.end() ) {
      BndSpec& existingBCSpec = (*bndNameBndSpecMap_.find(bndName)).second;
      vector<BndCondSpec>& bcSpecVec = existingBCSpec.bcSpecVec;
      vector<BndCondSpec>::iterator it = std::find(bcSpecVec.begin(), bcSpecVec.end(), bcSpec);
      if ( it == bcSpecVec.end() ) {
        DBGBC << "adding bc " << bcSpec.varName << " on " << bndName << " \n";
        bcSpecVec.push_back(bcSpec);
      } else {
        DBGBC << "bc " << bcSpec.varName << " already exists on " << bndName << ". skipping \n";
      }
    } else {
      DBGBC << " ERROR! boundary face " << bndName << " does not exist!!! \n";
    }
  }

  //------------------------------------------------------------------------------------------------

  void BCHelper::add_boundary_condition( const BndCondSpec& bcSpec )
  {
    using namespace std;
    BOOST_FOREACH( BndMapT::value_type& bndPair, bndNameBndSpecMap_){
      add_boundary_condition(bndPair.first, bcSpec);
    }
  }

  //------------------------------------------------------------------------------------------------

  void BCHelper::add_boundary( const std::string&      bndName,
                               Uintah::Patch::FaceType face,
                               const BndTypeEnum&      bndType,
                               const int               patchID,
                               const Uintah::BCGeomBase::ParticleBndSpec pBndSpec)
  {
    DBGBC << "adding boundary " << bndName << " of type " << bndType << " on patch " << patchID << std::endl;

    // if this boundary is a wall AND no particle boundaries have been specified, then default
    // the particle boundary to a fully elastic wall.
    Uintah::BCGeomBase::ParticleBndSpec myPBndSpec = pBndSpec;
    if (bndType == WALL && pBndSpec.bndType == Uintah::BCGeomBase::ParticleBndSpec::NOTSET) {
      myPBndSpec.bndType = Uintah::BCGeomBase::ParticleBndSpec::WALL;
      myPBndSpec.wallType = Uintah::BCGeomBase::ParticleBndSpec::ELASTIC;
      myPBndSpec.restitutionCoef = 1.0;
    }
    if ( bndNameBndSpecMap_.find(bndName) != bndNameBndSpecMap_.end() ) {
      DBGBC << " adding to existing \n";
      BndSpec& existingBndSpec = (*bndNameBndSpecMap_.find(bndName)).second;
      existingBndSpec.patchIDs.push_back(patchID);
    } else {
      DBGBC << " adding new \n";
      // this is the first time that we are adding this boundary. create the necessary info to store this
      BndSpec myBndSpec = {bndName, face, bndType, std::vector<int>(1, patchID), myPBndSpec };
      bndNameBndSpecMap_.insert( BndMapT::value_type(bndName, myBndSpec) );
    }
  }

  //------------------------------------------------------------------------------------------------

  void BCHelper::add_auxiliary_boundary_condition( const std::string& srcVarName,
                                                   const std::string& newVarName,
                                                   const double newValue,
                                                   const BndCondTypeEnum newBCType )
  {
    BndCondSpec newBCSpec = {newVarName, "none", newValue, newBCType, DOUBLE_TYPE};
    add_auxiliary_boundary_condition(srcVarName, newBCSpec);
  }

  //------------------------------------------------------------------------------------------------

  void BCHelper::add_auxiliary_boundary_condition(const std::string& srcVarName,
                                                  BndCondSpec bcSpec)
  {
    BOOST_FOREACH( BndMapT::value_type bndSpecPair, bndNameBndSpecMap_ ){
      BndSpec& myBndSpec = bndSpecPair.second;
      const BndCondSpec* myBndCondSpec = myBndSpec.find(srcVarName);
      if (myBndCondSpec) {
        add_boundary_condition(myBndSpec.name, bcSpec);
      }
    }
  }

  //------------------------------------------------------------------------------------------------

  void BCHelper::add_boundary_mask( const BoundaryIterators& myIters,
                                    const std::string& bndName,
                                    const int& patchID )
  {
    using namespace std;
    if ( bndNamePatchIDMaskMap_.find(bndName) != bndNamePatchIDMaskMap_.end() ) {
      DBGBC << "BC " << bndName << " already exists in list of Iterators. Adding new iterator for " << bndName << " on patchID " << patchID << std::endl;
      (*bndNamePatchIDMaskMap_.find(bndName)).second.insert(pair<int, BoundaryIterators>(patchID, myIters));
    } else {
      DBGBC << "BC " << bndName << " does NOT Exist in list of Iterators. Adding new iterator for " << bndName << " on patchID " << patchID << std::endl;
      PatchIDBndItrMapT patchIDIterMap;
      patchIDIterMap.insert(pair<int, BoundaryIterators>(patchID, myIters));
      bndNamePatchIDMaskMap_.insert( pair< string, PatchIDBndItrMapT >(bndName, patchIDIterMap ) );
    }
  }

  //------------------------------------------------------------------------------------------------

  const std::vector<SpatialOps::IntVec>*
  BCHelper::get_edge_mask( const BndSpec& myBndSpec, const int& patchID ) const
  {
    const std::string bndName = myBndSpec.name;
    if ( bndNamePatchIDMaskMap_.find(bndName) != bndNamePatchIDMaskMap_.end() ) {
      const PatchIDBndItrMapT& myMap = (*bndNamePatchIDMaskMap_.find(bndName)).second;
      if ( myMap.find(patchID) != myMap.end() ) {
        const BoundaryIterators& myIters = (*myMap.find(patchID)).second;
        return &(myIters.interiorEdgeCells);
      }
    }
    return nullptr;
  }

  //------------------------------------------------------------------------------------------------

  const std::vector<int>*
  BCHelper::get_particles_bnd_mask( const BndSpec& myBndSpec,
                                    const int& patchID ) const
  {
    const std::string bndName = myBndSpec.name;
    if ( bndNamePatchIDMaskMap_.find(bndName) != bndNamePatchIDMaskMap_.end() ) {
      const PatchIDBndItrMapT& myMap = (*bndNamePatchIDMaskMap_.find(bndName)).second;
      if ( myMap.find(patchID) != myMap.end() ) {
        const BoundaryIterators& myIters = (*myMap.find(patchID)).second;
        return myIters.particleIdx;
      }
    }
    return nullptr;
  }

  //------------------------------------------------------------------------------------------------

  template<typename FieldT>
  const std::vector<SpatialOps::IntVec>*
  BCHelper::get_extra_bnd_mask( const BndSpec& myBndSpec,
                               const int& patchID ) const
  {
    const std::string bndName = myBndSpec.name;
    const bool isStagNorm = is_staggered_normal<FieldT>(myBndSpec.face);
    const bool isPlusSide = is_plus_side<FieldT>(myBndSpec.face);
    if ( bndNamePatchIDMaskMap_.find(bndName) != bndNamePatchIDMaskMap_.end() ) {
      const PatchIDBndItrMapT& myMap = (*bndNamePatchIDMaskMap_.find(bndName)).second;
      if ( myMap.find(patchID) != myMap.end() ) {
        const BoundaryIterators& myIters = (*myMap.find(patchID)).second;
        if (isStagNorm && isPlusSide) {
          return &(myIters.extraPlusBndCells);
        } else {
          return &(myIters.extraBndCells);
        }
      }
    }
    return nullptr;
  }

  //------------------------------------------------------------------------------------------------

  template<>
  const std::vector<SpatialOps::IntVec>*
  BCHelper::get_extra_bnd_mask<ParticleField>( const BndSpec& myBndSpec,
                               const int& patchID ) const
  {
    return nullptr;
  }

  //------------------------------------------------------------------------------------------------

  template<typename FieldT>
  const std::vector<SpatialOps::IntVec>*
  BCHelper::get_interior_bnd_mask( const BndSpec& myBndSpec,
                                  const int& patchID ) const
  {
    const std::string bndName = myBndSpec.name;
    const bool isStagNorm = is_staggered_normal<FieldT>(myBndSpec.face);
    const bool isPlusSide = is_plus_side<FieldT>(myBndSpec.face);

    if ( bndNamePatchIDMaskMap_.find(bndName) != bndNamePatchIDMaskMap_.end() ) {
      const PatchIDBndItrMapT& myMap = (*bndNamePatchIDMaskMap_.find(bndName)).second;
      if ( myMap.find(patchID) != myMap.end() ) {
        const BoundaryIterators& myIters = (*myMap.find(patchID)).second;
        if (isStagNorm && isPlusSide) {
          return &(myIters.extraBndCells);
        } else {
          return &(myIters.interiorBndCells);
        }
      }
    }
    return nullptr;
  }

  //------------------------------------------------------------------------------------------------

  template<>
  const std::vector<SpatialOps::IntVec>*
  BCHelper::get_interior_bnd_mask<ParticleField>( const BndSpec& myBndSpec,
                                  const int& patchID ) const
  {
    return nullptr;
  }

  //------------------------------------------------------------------------------------------------

  Uintah::Iterator&
  BCHelper::get_uintah_extra_bnd_mask( const BndSpec& myBndSpec,
                                      const int& patchID )
  {
    const std::string bndName = myBndSpec.name;

    if ( bndNamePatchIDMaskMap_.find(bndName) != bndNamePatchIDMaskMap_.end() ) {
      PatchIDBndItrMapT& myMap = (*bndNamePatchIDMaskMap_.find(bndName)).second;
      if ( myMap.find(patchID) != myMap.end() ) {
        BoundaryIterators& myIters = (*myMap.find(patchID)).second;
        return myIters.extraBndCellsUintah;
      }
    }

    std::ostringstream msg;
    msg << "ERROR: It looks like you were trying to grab a boundary iterator that doesn't exist! "
    << "This could be caused by requesting an iterator for a boundary/patch combination that is inconsistent with your input. "
    << "Otherwise, this is likely a major bug that needs to be addressed by a core Wasatch developer." << std::endl;
    throw Uintah::ProblemSetupException( msg.str(), __FILE__, __LINE__ );
  }

  //------------------------------------------------------------------------------------------------

  template<typename FieldT>
  const SpatialOps::SpatialMask<FieldT>*
  BCHelper::get_spatial_mask( const BndSpec& myBndSpec,
                              const int& patchID,
                              const bool interior) const
  {
    const std::string bndName = myBndSpec.name;

    if ( bndNamePatchIDMaskMap_.find(bndName) != bndNamePatchIDMaskMap_.end() ) {
      const PatchIDBndItrMapT& myMap = (*bndNamePatchIDMaskMap_.find(bndName)).second;
      if ( myMap.find(patchID) != myMap.end() ) {
        const BoundaryIterators& myIters = (*myMap.find(patchID)).second;
        return myIters.get_spatial_mask<FieldT>(interior);
      }
    }

    std::ostringstream msg;
    msg << "ERROR: It looks like you were trying to grab a boundary iterator that doesn't exist! "
    << "This could be caused by requesting an iterator for a boundary/patch combination that is inconsistent with your input. "
    << "Otherwise, this is likely a major bug that needs to be addressed by a core Wasatch developer." << std::endl;
    throw Uintah::ProblemSetupException( msg.str(), __FILE__, __LINE__ );
  }

  //------------------------------------------------------------------------------------------------

  template<>
  const SpatialOps::SpatialMask<ParticleField>*
  BCHelper::get_spatial_mask( const BndSpec& myBndSpec,
                              const int& patchID,
                              const bool interior) const
  {
    return nullptr;
  }

  //------------------------------------------------------------------------------------------------

  void BCHelper::print() const
  {
    BOOST_FOREACH( const BndMapT::value_type& bndNameBCSpecPair, bndNameBndSpecMap_ ){
      bndNameBCSpecPair.second.print();
    }
  }

  //------------------------------------------------------------------------------------------------

  void BCHelper::parse_boundary_conditions()
  {
    using namespace std;
    // loop over the material set
    BOOST_FOREACH( const Uintah::MaterialSubset* matSubSet, materials_->getVector() ) {

      // loop over materials
      for( int im=0; im<matSubSet->size(); ++im ) {

        const int materialID = matSubSet->get(im);

        // loop over local patches
        BOOST_FOREACH( const Uintah::PatchSubset* const patches, localPatches_->getVector() ) {

          // loop over every patch in the patch subset
          BOOST_FOREACH( const Uintah::Patch* const patch, patches->getVector() ) {

            const int patchID = patch->getID();
            DBGBC << "*************************************************\n";
            DBGBC << "Patch ID = " << patchID << " | On Rank: " << Uintah::Parallel::getMPIRank() << std::endl;

            std::vector<Uintah::Patch::FaceType> bndFaces;
            patch->getBoundaryFaces(bndFaces);

            // loop over the physical boundaries of this patch. These are the LOGICAL boundaries
            // and do NOT include intrusions
            BOOST_FOREACH(const Uintah::Patch::FaceType face, bndFaces) {

              // Get the number of "boundaries" (children) specified on this boundary face.
              // example: x- boundary face has a circle specified as inlet while the rest of the
              // face is specified as wall. This results in two "boundaries" or children.
              // the BCDataArray will store this list of children
              const Uintah::BCDataArray* bcDataArray = patch->getBCDataArray(face);

              // Grab the number of children on this boundary face
              const int numChildren = bcDataArray->getNumberChildren(materialID);

              DBGBC << "Face = " << face << std::endl;
              //bcDataArray->print();

              // now go over every child-boundary (sub-boundary) specified on this domain boundary face
              for( int chid = 0; chid<numChildren; ++chid ) {
                DBGBC << " child ID = " << chid << std::endl;

                // here is where the fun starts. Now we can get information about this boundary condition.
                // The BCDataArray stores information related to its children as BCGeomBase objects.
                // Each child is associated with a BCGeomBase object. Grab that
                Uintah::BCGeomBase* thisGeom = bcDataArray->getChild(materialID,chid);
                const std::string bndName = thisGeom->getBCName();
                if (bndName.compare("NotSet")==0) {
                  std::ostringstream msg;
                  msg << "ERROR: It looks like you have not set a name for one of your boundary conditions! "
                      << "You MUST specify a name for your <Face> spec boundary condition. Please revise your input file." << std::endl;
                  throw Uintah::ProblemSetupException( msg.str(), __FILE__, __LINE__ );
                }
                DBGBC << " boundary name = " << bndName << std::endl;
                DBGBC << " geom bndtype  = " << thisGeom->getBndType() << std::endl;
                BndTypeEnum bndType = select_bnd_type_enum(thisGeom->getBndType());
                add_boundary( bndName, face, bndType, patchID, thisGeom->getParticleBndSpec() );
                DBGBC << " boundary type = " << bndType << std::endl;

                //__________________________________________________________________________________
                Uintah::Iterator bndIter; // allocate iterator
                // get the iterator for the extracells for this child
                bcDataArray->getCellFaceIterator(materialID, bndIter, chid);

                BoundaryIterators myIters;
                DBGBC << " Size of uintah iterator for boundary: " << bndName << " = " << bndIter.size() << std::endl;
                pack_uintah_iterator_as_spatialops(face, patch, bndIter, myIters); // convert the Uintah iterator to a SpatialOps-friendly mask
                // store a pointer to the list of particle index that are near this boundary.
                myIters.particleIdx = Uintah::ParticlesHelper::get_boundary_particles(bndName,patchID);

                add_boundary_mask( myIters, bndName, patchID );

                //__________________________________________________________________________________
                // Now, each BCGeomObject has BCData associated with it. This BCData contains the list
                // of variables and types (Dirichlet, etc...), and values that the user specified
                // through the input file!
                Uintah::BCData bcData;
                thisGeom->getBCData(bcData);

                BOOST_FOREACH( Uintah::BoundCondBase* bndCondBase, bcData.getBCData() ) {
                  const std::string varName     = bndCondBase->getBCVariable();
                  const BndCondTypeEnum atomBCTypeEnum = select_bc_type_enum(bndCondBase->getBCType());

                  DBGBC << " bc variable = " << varName << std::endl
                  << " bc type = "     << atomBCTypeEnum << std::endl;

                  double doubleVal=0.0;
                  std::string functorName="none";
                  BCValueTypeEnum bcValType=INVALID_TYPE;

                  switch ( bndCondBase->getValueType() ) {

                    case Uintah::BoundCondBase::DOUBLE_TYPE: {
                      const Uintah::BoundCond<double>* const new_bc = dynamic_cast<const Uintah::BoundCond<double>*>(bndCondBase);
                      doubleVal = new_bc->getValue();
                      bcValType = DOUBLE_TYPE;
                      break;
                    }

                    case Uintah::BoundCondBase::STRING_TYPE: {
                      const Uintah::BoundCond<std::string>* const new_bc = dynamic_cast<const Uintah::BoundCond<std::string>*>(bndCondBase);
                      functorName = new_bc->getValue();
                      bcValType = FUNCTOR_TYPE;
                      DBGBC << " functor name = " << functorName << std::endl;
                      break;
                    }
                    case Uintah::BoundCondBase::VECTOR_TYPE: {
                      // do nothing here... this is added for WARCHES support
                      break;
                    }
                    case Uintah::BoundCondBase::INT_TYPE: {
                      // do nothing here... this is added for RMCRT support
                      break;
                    }
                    default:
                    {
                      std::ostringstream msg;
                      msg << "ERROR: It looks like you have specified an unsupported datatype value for boundary " << bndName << ". "
                          << "Supported datatypes are: double, vector, and string (i.e. functor name)." << std::endl;
                      throw Uintah::ProblemSetupException( msg.str(), __FILE__, __LINE__ );
                    }
                      break;
                  }
                  const BndCondSpec bndCondSpec = {varName, functorName, doubleVal, atomBCTypeEnum, bcValType};
                  add_boundary_condition(bndName, bndCondSpec);
                }
              } // boundary child loop (note, a boundary child is what Wasatch thinks of as a boundary condition
            } // boundary faces loop

            // INTERIOR BOUNDARY CONDITIONS
            if (patch->hasInteriorBoundaryFaces()) {

              for(Uintah::Patch::FaceType face_side = Uintah::Patch::startFace;
                  face_side <= Uintah::Patch::endFace; face_side=Uintah::Patch::nextFace(face_side))
              {

                // Get the number of "boundaries" (children) specified on this interior boundary face.
                // example: x- boundary face has a circle specified as inlet while the rest of the
                // face is specified as wall. This results in two "boundaries" or children.
                // the BCDataArray will store this list of children
                const Uintah::BCDataArray* bcDataArray = patch->getInteriorBndBCDataArray(face_side);

                // Grab the number of children on this boundary face
                const int numChildren = bcDataArray->getNumberChildren(materialID);

                // now go over every child-boundary (sub-boundary) specified on this domain boundary face
                for( int chid = 0; chid<numChildren; ++chid ) {
                  DBGBC << " child ID = " << chid << std::endl;

                  // here is where the fun starts. Now we can get information about this boundary condition.
                  // The BCDataArray stores information related to its children as BCGeomBase objects.
                  // Each child is associated with a BCGeomBase object. Grab that
                  Uintah::BCGeomBase* thisGeom = bcDataArray->getChild(materialID,chid);
                  const std::string bndName = thisGeom->getBCName();
                  if (bndName.compare("NotSet")==0) {
                    std::ostringstream msg;
                    msg << "ERROR: It looks like you have not set a name for one of your boundary conditions! "
                        << "You MUST specify a name for your <Face> spec boundary condition. Please revise your input file." << std::endl;
                    throw Uintah::ProblemSetupException( msg.str(), __FILE__, __LINE__ );
                  }
                  DBGBC << " boundary name = " << bndName << std::endl;
                  DBGBC << " geom bndtype  = " << thisGeom->getBndType() << std::endl;
                  BndTypeEnum bndType = select_bnd_type_enum(thisGeom->getBndType());
                  add_boundary( bndName, face_side, bndType, patchID, thisGeom->getParticleBndSpec() );
                  DBGBC << " boundary type = " << bndType << std::endl;

                  //__________________________________________________________________________________
                  Uintah::Iterator bndIter; // allocate iterator
                  // get the iterator for the extracells for this child
                  bcDataArray->getCellFaceIterator(materialID, bndIter, chid);

                  BoundaryIterators myIters;
                  DBGBC << " Size of uintah iterator for boundary: " << bndName << " = " << bndIter.size() << std::endl;
                  pack_uintah_iterator_as_spatialops(face_side, patch, bndIter, myIters); // convert the Uintah iterator to a SpatialOps-friendly mask
                  // store a pointer to the list of particle index that are near this boundary.
                  myIters.particleIdx = Uintah::ParticlesHelper::get_boundary_particles(bndName,patchID);

                  add_boundary_mask( myIters, bndName, patchID );

                  //__________________________________________________________________________________
                  // Now, each BCGeomObject has BCData associated with it. This BCData contains the list
                  // of variables and types (Dirichlet, etc...), and values that the user specified
                  // through the input file!
                  Uintah::BCData bcData;
                  thisGeom->getBCData(bcData);

                  BOOST_FOREACH( Uintah::BoundCondBase* bndCondBase, bcData.getBCData() ) {
                    const std::string varName     = bndCondBase->getBCVariable();
                    const BndCondTypeEnum atomBCTypeEnum = select_bc_type_enum(bndCondBase->getBCType());

                    DBGBC << " bc variable = " << varName << std::endl
                          << " bc type = "     << atomBCTypeEnum << std::endl;

                    double doubleVal=0.0;
                    std::string functorName="none";
                    BCValueTypeEnum bcValType=INVALID_TYPE;

                    switch ( bndCondBase->getValueType() ) {

                      case Uintah::BoundCondBase::DOUBLE_TYPE: {
                        const Uintah::BoundCond<double>* const new_bc = dynamic_cast<const Uintah::BoundCond<double>*>(bndCondBase);
                        doubleVal = new_bc->getValue();
                        bcValType = DOUBLE_TYPE;
                        break;
                      }

                      case Uintah::BoundCondBase::STRING_TYPE: {
                        const Uintah::BoundCond<std::string>* const new_bc = dynamic_cast<const Uintah::BoundCond<std::string>*>(bndCondBase);
                        functorName = new_bc->getValue();
                        bcValType = FUNCTOR_TYPE;
                        DBGBC << " functor name = " << functorName << std::endl;
                        break;
                      }
                      case Uintah::BoundCondBase::VECTOR_TYPE: {
                        // do nothing here... this is added for WARCHES support
                        break;
                      }
                      case Uintah::BoundCondBase::INT_TYPE: {
                        // do nothing here... this is added for RMCRT support
                        break;
                      }
                      default:
                      {
                        std::ostringstream msg;
                        msg << "ERROR: It looks like you have specified an unsupported datatype value for boundary " << bndName << ". "
                            << "Supported datatypes are: double, vector, and string (i.e. functor name)." << std::endl;
                        throw Uintah::ProblemSetupException( msg.str(), __FILE__, __LINE__ );
                      }
                        break;
                    }
                    const BndCondSpec bndCondSpec = {varName, functorName, doubleVal, atomBCTypeEnum, bcValType};
                    add_boundary_condition(bndName, bndCondSpec);
                  }
                } // boundary child loop (note, a boundary child is what Wasatch thinks of as a boundary condition
              }
            }
          } // patch loop
        } // patch subset loop
      } // material loop
    } // material subset loop
  }

  //------------------------------------------------------------------------------------------------

  const BndMapT& BCHelper::get_boundary_information() const
  {
    return bndNameBndSpecMap_;
  }

  //------------------------------------------------------------------------------------------------

  bool BCHelper::has_boundaries() const
  {
    return !bndNameBndSpecMap_.empty();
  }

    //==========================================================================
  // Explicit template instantiation for supported versions of this class
  #include <spatialops/structured/FVStaggered.h>

  #define INSTANTIATE_MASK_TYPES(VOLT) \
    typedef SpatialOps::IntVec IntVecT; \
    template const std::vector<IntVecT>* BCHelper::get_extra_bnd_mask< VOLT >( const BndSpec& myBndSpec, const int& patchID ) const;\
    template const std::vector<IntVecT>* BCHelper::get_interior_bnd_mask< VOLT >( const BndSpec& myBndSpec,const int& patchID ) const;\
    template const SpatialOps::SpatialMask<VOLT>* BCHelper::get_spatial_mask< VOLT >( const BndSpec& myBndSpec,const int& patchID, const bool interior ) const;

  INSTANTIATE_MASK_TYPES(SpatialOps::SVolField);
  INSTANTIATE_MASK_TYPES(SpatialOps::SSurfXField);
  INSTANTIATE_MASK_TYPES(SpatialOps::SSurfYField);
  INSTANTIATE_MASK_TYPES(SpatialOps::SSurfZField);

  INSTANTIATE_MASK_TYPES(SpatialOps::XVolField);
  INSTANTIATE_MASK_TYPES(SpatialOps::YVolField);
  INSTANTIATE_MASK_TYPES(SpatialOps::ZVolField);
} // class BCHelper

max_stars_repo_name: abagusetty/Uintah

max_issues_repo_name: abagusetty/Uintah

max_forks_repo_name: abagusetty/Uintah


text:
/**
 ** Copyright (c) 2014 Illumina, Inc.
 **
 ** This file is part of Illumina's Enhanced Artificial Genome Engine (EAGLE),
 ** covered by the "BSD 2-Clause License" (see accompanying LICENSE file)
 **
 ** \author Lilian Janin
 **/

#ifndef EAGLE_FRAGMENTS_ALLOCATOR_HH
#define EAGLE_FRAGMENTS_ALLOCATOR_HH

#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <boost/noncopyable.hpp>
#include <boost/filesystem.hpp>
#include <boost/format.hpp>
#include <boost/random/mersenne_twister.hpp>

#include "model/Fragment.hh"
#include "model/IntervalGenerator.hh"
#include "genome/GcContent.hh"
#include "FragmentsAllocatorOptions.hh"

namespace eagle
{
namespace main
{

class FragmentsAllocator
{
public:
    FragmentsAllocator( const FragmentsAllocatorOptions &options );
    void run();

private:
    void setRandomSeed();
    model::FragmentWithAllocationMetadata getNextFragment( boost::shared_ptr<eagle::model::IntervalGenerator>& randomInterval,
                                                           boost::shared_ptr<eagle::model::MultiFragmentFilesReader>& multiFragmentFilesReader,
                                                           const unsigned long fragmentNum,
                                                           const unsigned long fragmentCount );

    const FragmentsAllocatorOptions &options_;
//    boost::shared_ptr< boost::mt19937 > randomGen_;
    genome::GcCoverageFit gcCoverageFit_;
};

} // namespace main
} // namespace eagle

#endif // EAGLE_FRAGMENTS_ALLOCATOR_HH

ext: hh

max_stars_repo_name: sequencing/EAGLE

max_issues_repo_name: sequencing/EAGLE

max_forks_repo_name: sequencing/EAGLE


text:
// Copyright (c) 2012 by Raytheon BBN Technologies Corp.
// All Rights Reserved.

#include "Generic/common/leak_detection.h" // This must be the first #include

#include "Generic/edt/MentionGroups/MentionGroupUtils.h"
#include "Generic/edt/MentionGroups/LinkInfoCache.h"
#include "Generic/edt/MentionGroups/MentionGroup.h"
#include "Generic/common/Assert.h"
#include "Generic/common/SessionLogger.h"

#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>

std::set<Symbol> MentionGroupUtils::getSymbolValueSet(std::vector<AttributeValuePair_ptr> pairs) {
	std::set<Symbol> result;
	BOOST_FOREACH(AttributeValuePair_ptr pair, pairs) {
		boost::shared_ptr< AttributeValuePair<Symbol> > symPair = boost::dynamic_pointer_cast< AttributeValuePair<Symbol> >(pair);
		// check that cast didn't fail
		SerifAssert(symPair != 0);
		result.insert(symPair->getValue());
	}
	return result;
}

std::set<int> MentionGroupUtils::getIntValueSet(std::vector<AttributeValuePair_ptr> pairs) {
	std::set<int> result;
	BOOST_FOREACH(AttributeValuePair_ptr pair, pairs) {
		boost::shared_ptr< AttributeValuePair<int> > symIntPair = boost::dynamic_pointer_cast< AttributeValuePair<int> >(pair);
		// check that cast didn't fail
		SerifAssert(symIntPair != 0);
		result.insert(symIntPair->getValue());
	}
	return result;
}

void MentionGroupUtils::addSymbolFeatures(std::vector<AttributeValuePair_ptr>& features,
                                          Symbol extractorName, Symbol featureName,
                                          std::wstring value, LinkInfoCache& cache)
{
	// normalize the feature value
	std::transform(value.begin(), value.end(), value.begin(), towlower);
	boost::replace_all(value, L".", L"");
	Symbol normalizedVal = Symbol(value.c_str());
	features.push_back(AttributeValuePair<Symbol>::create(featureName, normalizedVal, extractorName));

	// check for alternate spellings
	Symbol altValue = cache.lookupAlternateSpelling(normalizedVal);
	if (!altValue.is_null()) {
		features.push_back(AttributeValuePair<Symbol>::create(featureName, altValue, extractorName));
	}
}

bool MentionGroupUtils::featureIsUniqueInDoc(Symbol extractorName, Symbol featureName, Symbol value,
                                             const MentionGroup& g1, const MentionGroup& g2,
                                             LinkInfoCache& cache)
{
	std::vector<const Mention*> matches = cache.getMentionsByFeatureValue(extractorName, featureName, value);
	BOOST_FOREACH(const Mention* m, matches) {
		if (!g1.contains(m) && !g2.contains(m)) {
			if (SessionLogger::dbg_or_msg_enabled("MentionGroups_shouldMerge"))
				SessionLogger::dbg("MentionGroups_shouldMerge")
					<< "    Uniqueness of " << g1.toString() << " AND "
					<< g2.toString() << " blocked by " << m->getUID();
			return false;
		}
	}

	return true;
}

ext: cpp

max_stars_repo_name: BBN-E/serif

max_issues_repo_name: BBN-E/serif

max_forks_repo_name: BBN-E/serif


text:
//          Copyright Oliver Kowalke 2009.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#include <boost/fiber/spin/mutex.hpp>

#include <boost/thread/thread.hpp>

#include <boost/fiber/utility.hpp>

namespace boost {
namespace fibers {
namespace spin {

mutex::mutex() :
	state_( UNLOCKED)
{}

void
mutex::lock()
{
	for (;;)
	{
		state expected = UNLOCKED;
		if ( state_.compare_exchange_strong( expected, LOCKED) )
			break;
		if ( this_fiber::runs_as_fiber() )
			this_fiber::yield();
		else
			this_thread::yield();
	}
}

bool
mutex::try_lock()
{
	state expected = UNLOCKED;
	return state_.compare_exchange_strong( expected, LOCKED);
}

void
mutex::unlock()
{ state_.store( UNLOCKED); }

}}}

max_stars_repo_name: ghisguth/tasks

max_issues_repo_name: ghisguth/tasks

max_forks_repo_name: ghisguth/tasks


text:
/// @file  cmdet.hpp
/// @brief Declarations for methods based on Cayley-Menger determinants.

#pragma once
#ifndef OGT_EMBED_CMDET_HPP
#define OGT_EMBED_CMDET_HPP

#include <ogt/config.hpp>
#include <Eigen/Dense>
#include <memory>

namespace OGT_NAMESPACE {
namespace embed {

/// Captures a set of Cayley-Menger reference vertices which can be used to find
/// pairwise distances between points, or to embed the set.
///
/// This method is based on:
/// [1]	M. J. Sippl and H. A. Scheraga, "Cayley-Menger coordinates.,"
///     Proceedings of the National Academy of Sciences, Apr. 1986.
struct CMReference {

	/// Construct a concrete instance, given all pairwise squared Euclidean
	/// distances between the reference vertices.
	static std::shared_ptr<CMReference> Create(Eigen::MatrixXd refSqDists);

	/// Virtual destructor.
	virtual ~CMReference() = default;

	/// Find the squared Euclidean distance between two points with the given
	/// squared Euclidean distances to the reference vertices.
	virtual double sqDist(Eigen::VectorXd v1, Eigen::VectorXd v2) = 0;
};

} // end namespace embed
} // end namespace OGT_NAMESPACE
#endif /* OGT_EMBED_CMDET_HPP */

ext: hpp

max_stars_repo_name: jesand/lloe

max_issues_repo_name: jesand/lloe

max_forks_repo_name: jesand/lloe


text:
#include "lattice_net/TrainParams.h"

//loguru
#define LOGURU_REPLACE_GLOG 1
#include <loguru.hpp>

//configuru
#define CONFIGURU_WITH_EIGEN 1
#define CONFIGURU_IMPLICIT_CONVERSIONS 1
#include <configuru.hpp>
using namespace configuru;

//boost
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;

TrainParams::TrainParams(const std::string config_file){

    init_params(config_file);
}

void TrainParams::init_params(const std::string config_file){

    //read all the parameters
    // Config cfg = configuru::parse_file(std::string(CMAKE_SOURCE_DIR)+"/config/"+config_file, CFG);

    std::string config_file_abs;
    if (fs::path(config_file).is_relative()){
        config_file_abs=(fs::path(PROJECT_SOURCE_DIR) / config_file).string();
    }else{
        config_file_abs=config_file;
    }
    Config cfg = configuru::parse_file(config_file_abs, CFG);

    Config train_config=cfg["train"];
    m_dataset_name=(std::string)train_config["dataset_name"];
    m_with_viewer = train_config["with_viewer"];
    m_with_visdom = train_config["with_visdom"];
    m_lr = train_config["lr"];
    m_weight_decay = train_config["weight_decay"];
    m_save_checkpoint=train_config["save_checkpoint"];
    m_checkpoint_path=(std::string)train_config["checkpoint_path"];

    if(m_save_checkpoint && !fs::is_directory(m_checkpoint_path)) {
        LOG(FATAL) << "The directory for saving checkpoint was not created under " << m_checkpoint_path << ". Maybe you need to create it or maybe you are on the wrong machine.";
    }

}

std::string TrainParams::dataset_name(){
    return m_dataset_name;
}
bool TrainParams::with_viewer(){
    return m_with_viewer;
}
bool TrainParams::with_visdom(){
    return m_with_visdom;
}
float TrainParams::lr(){
    return m_lr;
}
float TrainParams::weight_decay(){
    return m_weight_decay;
}
bool TrainParams::save_checkpoint(){
    return m_save_checkpoint;
}
std::string TrainParams::checkpoint_path(){
    return m_checkpoint_path;
}

ext: cxx


text:
/* Copyright (C) 2010 Ion Torrent Systems, Inc. All Rights Reserved */
#include <vector>
#include <algorithm>
#include <limits>
#include "BfMetric.h"
#include <Eigen/Dense>
#include <Eigen/LU>
#include "IonH5File.h"
#include "IonH5Eigen.h"
#include "Stats.h"
#include "ChipReduction.h"
#include "SampleStats.h"

#define BFMETRIC_REDUCE_STEP 5
#define BFMETRIC_MIN_GOOD_WELLS 15

void BfMetric::CalcBfSdMetric(const short *__restrict image,
                              Mask *mask,
                              const char *__restrict bad_wells,
                              const float *__restrict t0,
                              const std::string &dbg_name,
                              int t0_window_start,
                              int t0_window_end,
                              int row_step,
                              int col_step,
                              int num_row_neighbors,
                              int num_col_neighbors) {

  // Calculate the average trace in region around each well
  m_nn_avg.Init(m_num_rows, m_num_cols, m_num_frames);
  m_nn_avg.SetGainMinMult(m_gain_min_mult);
  m_nn_avg.CalcCumulativeSum(image, mask, bad_wells);
  m_nn_avg.CalcNNAvgAndMinFrame(row_step, col_step,
                                num_row_neighbors, num_col_neighbors,
                                m_trace_min, m_trace_min_frame,
                                image, true);

  short * __restrict well_nn_diff = (short *) memalign(VEC8F_SIZE_B, (sizeof(short) * m_num_rows) * (m_num_cols * m_num_frames));
  int * __restrict well_nn_sq_diff = (int *) memalign(VEC8F_SIZE_B, (sizeof(int) * m_num_rows) * (m_num_cols * m_num_frames));
  memset(well_nn_diff, 0, (sizeof(short) * m_num_rows) * (m_num_cols * m_num_frames));
  memset(well_nn_sq_diff, 0, (sizeof(int) * m_num_rows) * (m_num_cols * m_num_frames));
  // Subtract each well from the average around it, this is beadfind buffering estimate
  short *__restrict diff_start = well_nn_diff;
  short *__restrict diff_end = diff_start + (m_num_rows * m_num_cols * m_num_frames);
  const short *__restrict img = image;
  const float *__restrict nn_avg = m_nn_avg.GetNNAvgImagePtr();
  const float *__restrict nn_avg_start = m_nn_avg.GetNNAvgImagePtr();
  while (diff_start != diff_end) {
    *diff_start = (short) (*img - *nn_avg_start + .5);
    diff_start++;
    img++;
    nn_avg_start++;
  }

  // if (!dbg_name.empty()) {
  //   H5File h5(dbg_name);
  //   h5.Open(true);
  //   int num_wells = m_num_rows * m_num_cols;
  //   const short *__restrict diff = well_nn_diff;
  //   const float *__restrict avg = nn_avg;
  //   const short *__restrict img = image;
  //   Eigen::MatrixXf mat_diff(m_num_cols * m_num_rows, m_num_frames);
  //   Eigen::MatrixXf mat_avg(m_num_cols * m_num_rows, m_num_frames);
  //   Eigen::MatrixXf mat_data(m_num_cols * m_num_rows, m_num_frames);
  //   for (int fIx = 0; fIx < m_num_frames; fIx++) {
  //     for (int well_ix = 0; well_ix < num_wells; well_ix++) {
  //       mat_diff(well_ix,fIx) = *diff;
  //       mat_avg(well_ix, fIx) = *avg;
  //       mat_data(well_ix, fIx) = *img;
  //       diff++;
  //       avg++;
  //       img++;
  //     }
  //   }
  //   H5Eigen::WriteMatrix(h5, "/bf_diff", mat_diff);
  //   H5Eigen::WriteMatrix(h5, "/bf_avg", mat_avg);
  //   H5Eigen::WriteMatrix(h5, "/bf_img", mat_data);
  //   h5.Close();
  // }

  // Gotta cleanup memory asap as these things take a lot of mem for 318 series
  m_nn_avg.Cleanup();

  // Now calculate the sum of differences and squared differences to determine frame in region
  // that has the maximum variance over wells in region and use that as bf metric

  // Calculate the cumulative sum of difference in the region per frame
  m_diff_sum_avg.Init(m_num_rows, m_num_cols, m_num_frames);
  m_diff_sum_avg.FreeNNAvg();
  m_diff_sum_avg.CalcCumulativeSum(well_nn_diff, mask, bad_wells);

  // Calculate the squared cumulative sum of difference
  diff_start = well_nn_diff;
  diff_end = diff_start + (m_num_rows * m_num_cols * m_num_frames);
  int *__restrict diff_sq_start = well_nn_sq_diff;
  while (diff_start != diff_end) {
    *diff_sq_start = (*diff_start) * (*diff_start);
    diff_start++;
    diff_sq_start++;
  }
  m_diff_sum_sq_avg.Init(m_num_rows, m_num_cols, m_num_frames);
  m_diff_sum_sq_avg.FreeNNAvg();
  m_diff_sum_sq_avg.CalcCumulativeSum(well_nn_sq_diff, mask, bad_wells);
  m_diff_sum_sq_avg.FreeNumGoodWells();
  FREEZ(&well_nn_sq_diff);

  // Figure out the frame around which a well that has the largest deviation
  int cs_frame_stride = (m_num_cols + 1) * (m_num_rows + 1);
  size_t frame_stride = m_num_cols * m_num_rows;
  const int64_t *__restrict cum_sum = m_diff_sum_avg.GetNNCumSumPtr();
  const int64_t *__restrict cum_sum_sq = m_diff_sum_sq_avg.GetNNCumSumPtr();
  const int *__restrict num_good_wells = m_diff_sum_avg.GetNumGoodWellsPtr();

  // Allocate scratch data frames to keep track of the frame with maximum variance in region
  uint8_t *max_sd_frame = (uint8_t*) memalign(VEC8F_SIZE_B, sizeof(uint8_t) * m_num_cols * m_num_rows);
  memset(max_sd_frame, 0, sizeof(uint8_t) * m_num_cols * m_num_rows);
  float *max_sd_value = (float*) memalign(VEC8F_SIZE_B, sizeof(float) * m_num_cols * m_num_rows);
  memset(max_sd_value, 0, sizeof(float) * m_num_cols * m_num_rows);

  // Go through each frame and calculate the sd for each window for each well.
  for (int fIx = 0; fIx < m_num_frames; fIx++) {
    const int64_t *__restrict cum_sum_frame = cum_sum + (fIx * cs_frame_stride);
    const int64_t *__restrict cum_sum_sq_frame = cum_sum_sq + (fIx * cs_frame_stride);
    int cs_col_size = m_num_cols + 1;
    int q1, q2,q3,q4;
    for (int rowIx = 0; rowIx < m_num_rows; rowIx++) {
      for (int colIx = 0; colIx < m_num_cols; colIx++) {
        int well_ix = rowIx * m_num_cols + colIx;
        int row_below_clip = row_step * (rowIx / row_step) -1;
        int row_above_clip = std::min(row_below_clip + row_step, m_num_rows - 1);
        int col_below_clip = col_step * (colIx / col_step)  -1;
        int col_above_clip = std::min(col_below_clip + col_step, m_num_cols - 1);

        int r_start = std::max(row_below_clip,rowIx - num_row_neighbors - 1); // -1 ok for calc of edge conditions
        int r_end = std::min(rowIx+num_row_neighbors, row_above_clip);
        int c_start = std::max(col_below_clip, colIx - num_col_neighbors - 1); // -1 ok for calc of edge conditions
        int c_end = std::min(col_above_clip,colIx + num_col_neighbors);

        int64_t sum_sq = 0, sum = 0;
        int good_wells = 0;
        int bad_wells = 0;
        /* calculate sd stats for wells */
        q1 = (r_end + 1) * cs_col_size + c_end + 1;
        q2 = (r_start +1) * cs_col_size + c_end + 1;
        q3 = (r_end+1) * cs_col_size + c_start + 1;
        q4 = (r_start+1) * cs_col_size + c_start + 1;

        good_wells = num_good_wells[q1] - num_good_wells[q2] - num_good_wells[q3] + num_good_wells[q4];
        /* combine them if we got some good wells */

        if (good_wells > 0) {
          sum  = cum_sum_frame[q1] - cum_sum_frame[q2] - cum_sum_frame[q3] + cum_sum_frame[q4];
          sum_sq  = cum_sum_sq_frame[q1] - cum_sum_sq_frame[q2] - cum_sum_sq_frame[q3] + cum_sum_sq_frame[q4];
          float region_sd = (sum_sq - (float)(sum*sum/good_wells))/good_wells;
          if (region_sd > max_sd_value[well_ix] && fIx >= (t0[well_ix] + t0_window_start) && fIx <= (t0[well_ix] + t0_window_end)) {
            max_sd_value[well_ix] = region_sd;
            max_sd_frame[well_ix] = (uint8_t)fIx;
          }
        }
        else {
          // do nothing, leave at zero
        }

      }
    }
  }

  // Cleanup as fast as possible these things take up a lot of memory
  m_diff_sum_avg.Cleanup();
  m_diff_sum_sq_avg.Cleanup();
  FREEZ(&max_sd_value);
  // Loop through and grab the data from frame with the value at the maximum sd frame point
  int *__restrict bf_start = m_bf_metric;
  int *__restrict bf_end = bf_start + frame_stride;
  uint8_t *__restrict max_frame_start = max_sd_frame;
  int *__restrict trace_min_start = m_trace_min_frame;
  int well_index = 0;
  int frame_counts[m_num_frames];
  memset(frame_counts, 0, sizeof(int) * m_num_frames);
  while(bf_start != bf_end) {
    frame_counts[*max_frame_start]++;
    *trace_min_start = *max_frame_start;
    *bf_start += *(well_nn_diff + (*max_frame_start) * frame_stride + well_index);
    *bf_start += *(well_nn_diff + (std::max(*max_frame_start-1,0)) * frame_stride + well_index);
    *bf_start += *(well_nn_diff + (std::min(*max_frame_start+1,m_num_frames-1)) * frame_stride + well_index);
    trace_min_start++;
    well_index++;
    max_frame_start++;
    bf_start++;
  }

  FREEZ(&well_nn_diff);
  FREEZ(&max_sd_frame);
  FREEZ(&max_sd_value);
}

void BfMetric::CalcBfSdMetricReduce(const short *__restrict image,
                                    Mask *mask,
                                    const char *__restrict bad_wells,
                                    const float *__restrict t0,
                                    const std::string &dbg_name,
                                    int t0_window_start,
                                    int t0_window_end,
                                    int row_step,
                                    int col_step,
                                    int num_row_neighbors,
                                    int num_col_neighbors,
                                    int frame_window) {

  // Calculate the average trace in region around each well
  ChipReduction reduce_avg(m_num_rows, m_num_cols, m_num_frames,
                       BFMETRIC_REDUCE_STEP, BFMETRIC_REDUCE_STEP,
                       row_step, col_step, BFMETRIC_MIN_GOOD_WELLS);
  reduce_avg.Reduce(image, bad_wells);
  reduce_avg.SmoothBlocks(num_row_neighbors, num_col_neighbors);

  short * __restrict well_nn_diff = (short *) memalign(VEC8F_SIZE_B, (sizeof(short) * m_num_rows) * (m_num_cols * m_num_frames));
  int * __restrict well_nn_sq_diff = (int *) memalign(VEC8F_SIZE_B, (sizeof(int) * m_num_rows) * (m_num_cols * m_num_frames));
  memset(well_nn_diff, 0, (sizeof(short) * m_num_rows) * (m_num_cols * m_num_frames));
  memset(well_nn_sq_diff, 0, (sizeof(int) * m_num_rows) * (m_num_cols * m_num_frames));

  // Subtract each well from the average around it, this is beadfind buffering estimate
  short *__restrict diff_start = well_nn_diff;
  int *__restrict diff_sq_start = well_nn_sq_diff;
  const short *__restrict img = image;

  for (int frame_ix = 0; frame_ix < m_num_frames; frame_ix++) {
    for (int row_ix = 0; row_ix < m_num_rows; row_ix++) {
      for (int col_ix = 0; col_ix < m_num_cols; col_ix++) {
        *diff_start = (short)(*img - reduce_avg.GetSmoothEst(row_ix, col_ix, frame_ix) + .5f);
        *diff_sq_start = (int)(*diff_start) * (*diff_start);
        diff_start++;
        diff_sq_start++;
        img++;
      }
    }
  }

  if (!dbg_name.empty()) {
    H5File h5(dbg_name);
    h5.Open(true);
    int num_wells = m_num_rows * m_num_cols;
    const short *__restrict diff = well_nn_diff;
    const short *__restrict img = image;
    Eigen::MatrixXf mat_diff(m_num_cols * m_num_rows, m_num_frames);
    Eigen::MatrixXf mat_avg(m_num_cols * m_num_rows, m_num_frames);
    Eigen::MatrixXf mat_data(m_num_cols * m_num_rows, m_num_frames);
    for (int fIx = 0; fIx < m_num_frames; fIx++) {
      for (int well_ix = 0; well_ix < num_wells; well_ix++) {
        mat_diff(well_ix,fIx) = *diff;
        mat_avg(well_ix, fIx) = reduce_avg.GetSmoothEst(well_ix / m_num_cols, well_ix % m_num_cols, fIx);
        mat_data(well_ix, fIx) = *img;
        diff++;
        img++;
      }
    }
    H5Eigen::WriteMatrix(h5, "/bf_diff", mat_diff);
    H5Eigen::WriteMatrix(h5, "/bf_avg", mat_avg);
    H5Eigen::WriteMatrix(h5, "/bf_img", mat_data);
    h5.Close();
  }

  ChipReduction diff_avg(m_num_rows, m_num_cols, m_num_frames,
                         BFMETRIC_REDUCE_STEP, BFMETRIC_REDUCE_STEP,
                         row_step, col_step, BFMETRIC_MIN_GOOD_WELLS);
  diff_avg.SetAvgReduce(false);
  diff_avg.Reduce(well_nn_diff, bad_wells);
  diff_avg.SmoothBlocks(row_step, col_step);

  ChipReduction diff_sq_avg(m_num_rows, m_num_cols, m_num_frames,
                            BFMETRIC_REDUCE_STEP, BFMETRIC_REDUCE_STEP,
                            row_step, col_step, BFMETRIC_MIN_GOOD_WELLS);
  diff_sq_avg.SetAvgReduce(false);
  diff_sq_avg.Reduce(well_nn_sq_diff, bad_wells);
  diff_sq_avg.SmoothBlocks(row_step, col_step);

  NNAvg good_wells(diff_avg.GetBlockHeight(), diff_avg.GetBlockWidth(), 1);
  good_wells.CalcCumulativeSum(diff_avg.GetGoodWells(), diff_avg.GetBadWells());

  // Now calculate the sum of differences and squared differences to determine frame in region
  // that has the maximum variance over wells in region and use that as bf metric

  // Allocate scratch data frames to keep track of the frame with maximum variance in region
  uint8_t *max_sd_frame = (uint8_t*) memalign(VEC8F_SIZE_B, sizeof(uint8_t) * m_num_cols * m_num_rows);
  memset(max_sd_frame, 0, sizeof(uint8_t) * m_num_cols * m_num_rows);
  float *max_sd_value = (float*) memalign(VEC8F_SIZE_B, sizeof(float) * m_num_cols * m_num_rows);
  memset(max_sd_value, 0, sizeof(float) * m_num_cols * m_num_rows);

  // Go through each frame and calculate the sd for each window for each well.
  for (int fIx = 0; fIx < m_num_frames; fIx++) {
    int well_ix = 0;
    for (int rowIx = 0; rowIx < m_num_rows; rowIx++) {
      for (int colIx = 0; colIx < m_num_cols; colIx++) {
        float sum_sq = 0;
        float sum = 0;
        float total_sum = 0;
        int good_count = 0;
        diff_avg.m_nn_avg.GetRegionSum(rowIx/BFMETRIC_REDUCE_STEP, colIx/BFMETRIC_REDUCE_STEP, fIx,
                                       row_step/BFMETRIC_REDUCE_STEP, col_step/BFMETRIC_REDUCE_STEP,
                                       num_row_neighbors/BFMETRIC_REDUCE_STEP, num_col_neighbors/BFMETRIC_REDUCE_STEP,
                                       sum, good_count);
        diff_sq_avg.m_nn_avg.GetRegionSum(rowIx/BFMETRIC_REDUCE_STEP, colIx/BFMETRIC_REDUCE_STEP, fIx,
                                          row_step/BFMETRIC_REDUCE_STEP, col_step/BFMETRIC_REDUCE_STEP,
                                          num_row_neighbors/BFMETRIC_REDUCE_STEP, num_col_neighbors/BFMETRIC_REDUCE_STEP,
                                          sum_sq, good_count);
        good_wells.GetRegionSum(rowIx/BFMETRIC_REDUCE_STEP, colIx/BFMETRIC_REDUCE_STEP, 0,
                                row_step/BFMETRIC_REDUCE_STEP, col_step/BFMETRIC_REDUCE_STEP,
                                num_row_neighbors/BFMETRIC_REDUCE_STEP, num_col_neighbors/BFMETRIC_REDUCE_STEP,
                                total_sum, good_count);
        // if (rowIx == 100 && colIx == 100) {
        //   fprintf(stdout, "Here we go\n");
        // }
        if (total_sum > 0) {
          float region_sd = (sum_sq - (float)(sum * sum/total_sum))/total_sum;
          if (region_sd > max_sd_value[well_ix] && fIx >= (t0[well_ix] + t0_window_start) && fIx <= (t0[well_ix] + t0_window_end)) {
            max_sd_value[well_ix] = region_sd;
            max_sd_frame[well_ix] = (uint8_t)fIx;
          }
        }
        else {
          // do nothing, leave at zero
        }
        well_ix++;
      }
    }
  }

  FREEZ(&max_sd_value);
  // Loop through and grab the data from frame with the value at the maximum sd frame point
  int *__restrict bf_start = m_bf_metric;
  int frame_stride = (m_num_rows * m_num_cols);
  int *__restrict bf_end = bf_start + frame_stride;
  uint8_t *__restrict max_frame_start = max_sd_frame;
  int *__restrict trace_min_start = m_trace_min_frame;
  int well_index = 0;
  int frame_counts[m_num_frames];
  memset(frame_counts, 0, sizeof(int) * m_num_frames);
  while(bf_start != bf_end) {
    frame_counts[*max_frame_start]++;
    *trace_min_start = *max_frame_start;
    // *bf_start += *(well_nn_diff + (*max_frame_start) * frame_stride + well_index);
    // *bf_start += *(well_nn_diff + (std::max(*max_frame_start-1,0)) * frame_stride + well_index);
    // *bf_start += *(well_nn_diff + (std::min(*max_frame_start+1,m_num_frames-1)) * frame_stride + well_index);
    int f_start = max(0, *max_frame_start - frame_window);
    int f_end = min(m_num_frames - 1, *max_frame_start + frame_window);
    for (int f_ix = f_start; f_ix <= f_end; f_ix++) {
      *bf_start += *(well_nn_diff + f_ix * frame_stride + well_index);
    }
    trace_min_start++;
    well_index++;
    max_frame_start++;
    bf_start++;
  }

  FREEZ(&well_nn_diff);
  FREEZ(&well_nn_sq_diff);
  FREEZ(&max_sd_frame);
}

void BfMetric::NormalizeMetric(const char *bad_wells,
                               int row_clip, int col_clip,
                               int num_row_neighbors,
                               int num_col_neighbors) {

  /* Get some filteres to avoid really bad outliers... */
  std::vector<char> norm_bad_wells(m_num_cols * m_num_cols);
  std::vector<float> bf_metric_sorted(norm_bad_wells.size());
  std::copy(m_bf_metric, m_bf_metric + norm_bad_wells.size(), bf_metric_sorted.begin());
  std::sort(bf_metric_sorted.begin(), bf_metric_sorted.end());
  float high_threshold = std::numeric_limits<float>::max();
  float low_threshold = -1.0f * high_threshold;
  if (bf_metric_sorted.size() > BFMETRIC_MIN_GOOD_WELLS) {
    float q25 = ionStats::quantile_sorted(bf_metric_sorted, .25);
    float q75 = ionStats::quantile_sorted(bf_metric_sorted, .75);
    float q02 = ionStats::quantile_sorted(bf_metric_sorted, .02);
    float q98 = ionStats::quantile_sorted(bf_metric_sorted, .98);
    float iqr = q75-q25;
    float hx3 = q75 + 3 * iqr;
    float lx3 = q25 - 3 * iqr;
    high_threshold = min(q98, hx3);
    low_threshold = max(lx3, q02);
  }
  /* If we fail our filters don't use it, otherwise default to regular mask. */
  for (size_t i = 0; i < norm_bad_wells.size(); i++) {
    if (m_bf_metric[i] <= low_threshold || m_bf_metric[i] >= high_threshold) {
      norm_bad_wells[i] = 1;
    }
    else {
      norm_bad_wells[i] = bad_wells[i];
    }
  }

  ChipReduction sum(m_num_rows, m_num_cols, 1,
                    BFMETRIC_REDUCE_STEP, BFMETRIC_REDUCE_STEP,
                    row_clip, col_clip,
                    BFMETRIC_MIN_GOOD_WELLS);
  sum.SetAvgReduce(false);
  sum.Reduce(m_bf_metric, &norm_bad_wells[0]);
  sum.SmoothBlocks(num_row_neighbors, num_col_neighbors);
  std::vector<int> sq_bf_metric(m_num_cols * m_num_rows);
  int *__restrict bf_start = m_bf_metric;
  int *__restrict bf_end = m_bf_metric + m_num_rows * m_num_cols;
  int *__restrict sq_start = &sq_bf_metric[0];
  while(bf_start != bf_end) {
    *sq_start = *bf_start * *bf_start;
    sq_start++;
    bf_start++;
  }
  ChipReduction sum_sq(m_num_rows, m_num_cols, 1,
                       BFMETRIC_REDUCE_STEP, BFMETRIC_REDUCE_STEP,
                       row_clip, col_clip,
                       BFMETRIC_MIN_GOOD_WELLS);
  sum_sq.SetAvgReduce(false);
  sum_sq.Reduce(&sq_bf_metric[0], &norm_bad_wells[0]);
  sum_sq.SmoothBlocks(num_row_neighbors, num_col_neighbors);

  std::vector<float> norm_ref(m_num_cols * m_num_rows);
  NNAvg good_wells(sum.GetBlockHeight(), sum.GetBlockWidth(), 1);
  good_wells.CalcCumulativeSum(sum.GetGoodWells(), sum.GetBadWells());
  SampleStats<float> bf_stats;
  SampleStats<float> ref_bf_stats;
  bf_start = m_bf_metric;
  bf_end = bf_start + norm_ref.size();
  const char *bad_start = &norm_bad_wells[0];
  while(bf_start != bf_end) {
    if (*bad_start == 0) {
      bf_stats.AddValue(*bf_start);
    }
    bad_start++;
    bf_start++;
  }

  int well_ix = 0;
  for (int rowIx = 0; rowIx < m_num_rows; rowIx++) {
    for (int colIx = 0; colIx < m_num_cols; colIx++) {
      float s = 0;
      float sq = 0;
      float total_sum = 0;
      int good_count = 0;
      sum.m_nn_avg.GetRegionSum(rowIx/BFMETRIC_REDUCE_STEP, colIx/BFMETRIC_REDUCE_STEP, 0,
                                row_clip/BFMETRIC_REDUCE_STEP, col_clip/BFMETRIC_REDUCE_STEP,
                                num_row_neighbors/BFMETRIC_REDUCE_STEP, num_col_neighbors/BFMETRIC_REDUCE_STEP,
                                s, good_count);
      sum_sq.m_nn_avg.GetRegionSum(rowIx/BFMETRIC_REDUCE_STEP, colIx/BFMETRIC_REDUCE_STEP, 0,
                                   row_clip/BFMETRIC_REDUCE_STEP, col_clip/BFMETRIC_REDUCE_STEP,
                                   num_row_neighbors/BFMETRIC_REDUCE_STEP, num_col_neighbors/BFMETRIC_REDUCE_STEP,
                                   sq, good_count);
      good_wells.GetRegionSum(rowIx/BFMETRIC_REDUCE_STEP, colIx/BFMETRIC_REDUCE_STEP, 0,
                              row_clip/BFMETRIC_REDUCE_STEP, col_clip/BFMETRIC_REDUCE_STEP,
                              num_row_neighbors/BFMETRIC_REDUCE_STEP, num_col_neighbors/BFMETRIC_REDUCE_STEP,
                              total_sum, good_count);
      float ref_avg = sum.GetSmoothEst(rowIx, colIx, 0) / total_sum;
      float var = (sq - (float)(s * s)/total_sum)/total_sum;
      //        norm_ref[well_ix] = (m_bf_metric[well_ix] - ref_avg)/var
      if (good_count > 0 && var > 0) {
        m_norm_bf_metric[well_ix] = (m_bf_metric[well_ix] - ref_avg)/sqrt(var);
      }
      else {
        m_norm_bf_metric[well_ix] = (m_bf_metric[well_ix] - bf_stats.GetMean())/bf_stats.GetSD();
      }
      well_ix++;
    }
  }
}

void BfMetric::NormalizeMetricRef(const char *ref_wells, const char *bad_wells,
                                  int row_clip, int col_clip,
                                  int num_row_neighbors,
                                  int num_col_neighbors) {

  /* Get some filteres to avoid really bad outliers... */
  std::vector<char> norm_bad_wells(m_num_rows * m_num_cols);
  std::vector<char> ref_flip(norm_bad_wells.size());
  std::vector<float> bf_metric_sorted(norm_bad_wells.size());
  std::copy(m_bf_metric, m_bf_metric + norm_bad_wells.size(), bf_metric_sorted.begin());
  std::sort(bf_metric_sorted.begin(), bf_metric_sorted.end());
  float high_threshold = std::numeric_limits<float>::max();
  float low_threshold = -1.0f * high_threshold;
  if (bf_metric_sorted.size() > BFMETRIC_MIN_GOOD_WELLS) {
    float q25 = ionStats::quantile_sorted(bf_metric_sorted, .25);
    float q75 = ionStats::quantile_sorted(bf_metric_sorted, .75);
    float q02 = ionStats::quantile_sorted(bf_metric_sorted, .02);
    float q98 = ionStats::quantile_sorted(bf_metric_sorted, .98);
    float iqr = q75-q25;
    float hx3 = q75 + 3 * iqr;
    float lx3 = q25 - 3 * iqr;
    high_threshold = min(q98, hx3);
    low_threshold = max(lx3, q02);
  }
  /* If we fail our filters don't use it, otherwise default to regular mask. */
  for (size_t i = 0; i < norm_bad_wells.size(); i++) {
    if (ref_wells[i] == 0) {
      ref_flip[i] = 1;
    }
    else {
      ref_flip[i] = 0;
    }
    if (m_bf_metric[i] <= low_threshold || m_bf_metric[i] >= high_threshold) {
      norm_bad_wells[i] = 1;
    }
    else {
      norm_bad_wells[i] = bad_wells[i];
    }
  }

  ChipReduction sum(m_num_rows, m_num_cols, 1,
                    BFMETRIC_REDUCE_STEP, BFMETRIC_REDUCE_STEP,
                    row_clip, col_clip,
                    BFMETRIC_MIN_GOOD_WELLS);
  sum.SetAvgReduce(false);
  sum.Reduce(m_bf_metric, &norm_bad_wells[0]);
  sum.SmoothBlocks(num_row_neighbors, num_col_neighbors);
  std::vector<int> sq_bf_metric(m_num_cols * m_num_rows);
  int *__restrict bf_start = m_bf_metric;
  int *__restrict bf_end = m_bf_metric + m_num_rows * m_num_cols;
  int *__restrict sq_start = &sq_bf_metric[0];
  while(bf_start != bf_end) {
    *sq_start = *bf_start * *bf_start;
    sq_start++;
    bf_start++;
  }
  ChipReduction sum_sq(m_num_rows, m_num_cols, 1,
                       BFMETRIC_REDUCE_STEP, BFMETRIC_REDUCE_STEP,
                       row_clip, col_clip,
                       BFMETRIC_MIN_GOOD_WELLS);
  sum_sq.SetAvgReduce(false);
  sum_sq.Reduce(&sq_bf_metric[0], &norm_bad_wells[0]);
  sum_sq.SmoothBlocks(num_row_neighbors, num_col_neighbors);

  std::vector<float> norm_ref(m_num_cols * m_num_rows);
  NNAvg good_wells(sum.GetBlockHeight(), sum.GetBlockWidth(), 1);
  good_wells.CalcCumulativeSum(sum.GetGoodWells(), sum.GetBadWells());
  SampleStats<float> bf_stats;
  SampleStats<float> ref_bf_stats;
  bf_start = m_bf_metric;
  bf_end = bf_start + norm_ref.size();
  const char *bad_start = &norm_bad_wells[0];
  const char *ref_start = ref_wells;
  while(bf_start != bf_end) {
    if (*bad_start == 0 && *ref_start == 1) {
      bf_stats.AddValue(*bf_start);
    }
    bad_start++;
    bf_start++;
    ref_start++;
  }

  ChipReduction ref_avg(m_num_rows, m_num_cols, 1,
                       BFMETRIC_REDUCE_STEP, BFMETRIC_REDUCE_STEP,
                       row_clip, col_clip,
                       BFMETRIC_MIN_GOOD_WELLS);
  ref_avg.Reduce(m_bf_metric, &ref_flip[0]);
  ref_avg.SmoothBlocks(num_row_neighbors, num_col_neighbors);

  int well_ix = 0;
  for (int rowIx = 0; rowIx < m_num_rows; rowIx++) {
    for (int colIx = 0; colIx < m_num_cols; colIx++) {
      float s = 0;
      float sq = 0;
      float total_sum = 0;
      int good_count = 0;
      sum.m_nn_avg.GetRegionSum(rowIx/BFMETRIC_REDUCE_STEP, colIx/BFMETRIC_REDUCE_STEP, 0,
                                row_clip/BFMETRIC_REDUCE_STEP, col_clip/BFMETRIC_REDUCE_STEP,
                                num_row_neighbors/BFMETRIC_REDUCE_STEP, num_col_neighbors/BFMETRIC_REDUCE_STEP,
                                s, good_count);
      sum_sq.m_nn_avg.GetRegionSum(rowIx/BFMETRIC_REDUCE_STEP, colIx/BFMETRIC_REDUCE_STEP, 0,
                                   row_clip/BFMETRIC_REDUCE_STEP, col_clip/BFMETRIC_REDUCE_STEP,
                                   num_row_neighbors/BFMETRIC_REDUCE_STEP, num_col_neighbors/BFMETRIC_REDUCE_STEP,
                                   sq, good_count);
      good_wells.GetRegionSum(rowIx/BFMETRIC_REDUCE_STEP, colIx/BFMETRIC_REDUCE_STEP, 0,
                              row_clip/BFMETRIC_REDUCE_STEP, col_clip/BFMETRIC_REDUCE_STEP,
                              num_row_neighbors/BFMETRIC_REDUCE_STEP, num_col_neighbors/BFMETRIC_REDUCE_STEP,
                              total_sum, good_count);
      float all_mean = sum.GetSmoothEst(rowIx, colIx, 0) / total_sum;
      float ref_mean = ref_avg.GetSmoothEst(rowIx, colIx, 0);
      float var = (sq - (float)(s * s)/total_sum)/total_sum;
      if (good_count > 0 && var > 0) {
        float sd = sqrt(var);
        float ref_center = ref_mean / sd;
        m_norm_bf_metric[well_ix] = (m_bf_metric[well_ix] - all_mean)/sd - ref_center;
      }
      else {
        m_norm_bf_metric[well_ix] = (m_bf_metric[well_ix] - bf_stats.GetMean())/bf_stats.GetSD();
      }
      well_ix++;
    }
  }
}

max_stars_repo_name: konradotto/TS

max_issues_repo_name: konradotto/TS

max_forks_repo_name: konradotto/TS


text:
/**
 * Copyright 2021 Scott Brauer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an  BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file   SignalCurrentCandleTriggerProgramOptions.hpp
 * @author Scott Brauer
 * @date   10-12-2021
 */
#ifndef OFFCENTER_TRADING_SIGNALCURRENTCANDLETRIGGER_SIGNALCURRENTCANDLETRIGGERPROGRAMOPTIONS_H_
#define OFFCENTER_TRADING_SIGNALCURRENTCANDLETRIGGER_SIGNALCURRENTCANDLETRIGGERPROGRAMOPTIONS_H_

#include <functional>
#include <iostream>
#include <sstream>

#include <boost/bind/bind.hpp>

#include "offcenter/common/program_options/ProgramOptionsGroup.hpp"
#include "offcenter/trading/signalcurrentcandletrigger/SignalCurrentCandleTriggerOptions.hpp"

namespace offcenter {
namespace trading {
namespace signalcurrentcandletrigger {

class SignalCurrentCandleTriggerProgramOptions:
		public offcenter::common::program_options::ProgramOptionsGroup<offcenter::trading::signalcurrentcandletrigger::SignalCurrentCandleTriggerOptions>
{
public:
	SignalCurrentCandleTriggerProgramOptions():
		ProgramOptionsGroup("Create Trading DB Options")
	{
		optionsDescription().add_options()
				("token,t", po::value<std::string>(&optionConfig().m_accessToken)->default_value(""), "Oanda access token")
				("account,a", po::value<std::string>(&optionConfig().m_userAccount)->default_value(""), "Oanda user account")
				("fxtrade", po::bool_switch(&optionConfig().m_fxtrade)->default_value(false), "Connect to Oanda's trade server")
				("instrument,i", po::value<std::string>(&optionConfig().m_instrument), "Instrument")
				("granularity", po::value<std::string>(&optionConfig().m_granularity)->default_value("S5"), "Granularity")
				("start,s", po::value<std::string>()->notifier(boost::bind(&SignalCurrentCandleTriggerOptions::setStart, &optionConfig(), boost::placeholders::_1)), "Start time (2004-06-09T20:59:55.000000000Z)")
				("end,e", po::value<std::string>()->notifier(boost::bind(&SignalCurrentCandleTriggerOptions::setEnd, &optionConfig(), boost::placeholders::_1)), "End time (2004-08-09T20:59:55.000000000Z)")
				;
	}

	virtual ~SignalCurrentCandleTriggerProgramOptions() = default;

	virtual void validateOptions(po::variables_map& vm) {

	}
};

} /* namespace signalcurrentcandletrigger */
} /* namespace trading */
} /* namespace offcenter */

#endif /* OFFCENTER_TRADING_SIGNALCURRENTCANDLETRIGGER_SIGNALCURRENTCANDLETRIGGERPROGRAMOPTIONS_H_ */


text:
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _CEPHMESOS_STATE_STATEMACHINE_HPP_
#define _CEPHMESOS_STATE_STATEMACHINE_HPP_

#include <boost/unordered_map.hpp>
#include <vector>

#include "Phase.hpp"
#include "TaskState.hpp"
#include "HostConfig.hpp"
#include "common/Config.hpp"
#include "common/TaskType.hpp"

using ceph::Phase;
using ceph::TaskState;
using ceph::TaskType;
using ceph::HostConfig;
using boost::unordered_map;
using std::string;
using std::vector;

class StateMachine
{
public:
  StateMachine();

  StateMachine(Config* _config);

  ~StateMachine();

  void addStagingTask(
      string taskId,
      string executorId,
      TaskType taskType,
      string hostname,
      string slaveId);

  void updateTaskToRunning(string taskId);

  void updateTaskToWaitingOSDID(string taskId);

  void updateTaskToStarting(string taskId);

  void updateTaskToFailed(string taskId);

  void updateCounters(TaskType taskType,Status oldStatus, Status newStatus);

  void printCounters();

  void moveState();

  void decreaseOSDIndex();

  TaskState getInitialMon();

  vector<TaskState> getWaitingOSDTask();

  vector<int>* getPendingOSDID();

  void addPendingOSDID(int osdID);

  Phase getCurrentPhase();

  bool nextMove(TaskType& taskType, int& token, string hostName);

  void addConfig(string hostname);

  HostConfig* getConfig(string hostname);

private:
  boost::unordered_map<std::string, ceph::TaskState> taskMap;

  boost::unordered_map<std::string, ceph::HostConfig> hostConfigMap;

  vector<int> pendingOSDID;

  Phase currentPhase;

  int monRunningNum = 0;
  int monStagingNum = 0;
  int monIndex = 0;

  int osdRunningNum = 0;
  int osdStagingNum = 0;
  int osdIndex = 0;

  int radosgwRunningNum = 0;
  int radosgwStagingNum = 0;
  int radosgwIndex = 0;

  Config* config;
  HostConfig defaultHostConfig;
};

#endif

max_stars_repo_name: vivint-smarthome/ceph-mesos

max_issues_repo_name: vivint-smarthome/ceph-mesos

max_forks_repo_name: vivint-smarthome/ceph-mesos


text:
// Copyright 2022 PingCAP, Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <Common/FailPoint.h>
#include <Storages/DeltaMerge/tools/workload/Options.h>
#include <Storages/DeltaMerge/tools/workload/Utils.h>
#include <fmt/ranges.h>

#include <boost/program_options.hpp>

namespace DB::DM::tests
{
std::string WorkloadOptions::toString(std::string seperator) const
{
    return fmt::format("max_key_count {}{}", max_key_count, seperator) + //
        fmt::format("write_key_distribution {}{}", write_key_distribution, seperator) + //
        fmt::format("write_count {}{}", write_count, seperator) + //
        fmt::format("write_thread_count {}{}", write_thread_count, seperator) + //
        fmt::format("table {}{}", table, seperator) + //
        fmt::format("pk_type {}{}", pk_type, seperator) + //
        fmt::format("colmuns_count {}{}", columns_count, seperator) + //
        fmt::format("failpoints {}{}", failpoints, seperator) + //
        fmt::format("max_write_per_sec {}{}", max_write_per_sec, seperator) + //
        fmt::format("log_file {}{}", log_file, seperator) + //
        fmt::format("log_level {}{}", log_level, seperator) + //
        fmt::format("verification {}{}", verification, seperator) + //
        fmt::format("verify_round {}{}", verify_round, seperator) + //
        fmt::format("random_kill {}{}", random_kill, seperator) + //
        fmt::format("max_sleep_sec {}{}", max_sleep_sec, seperator) + //
        fmt::format("work_dirs {}{}", work_dirs, seperator) + //
        fmt::format("config_file {}{}", config_file, seperator) + //
        fmt::format("read_thread_count {}{}", read_thread_count, seperator) + //
        fmt::format("read_stream_count {}{}", read_stream_count, seperator) + //
        fmt::format("testing_type {}{}", testing_type, seperator) + //
        fmt::format("log_write_request {}{}", log_write_request, seperator) + //
        fmt::format("enable_ps_v3 {}{}", enable_ps_v3, seperator) + //
        fmt::format("bg_thread_count {}{}", bg_thread_count, seperator) + //
        fmt::format("table_id {}{}", table_id, seperator) + //
        fmt::format("table_name {}{}", table_name, seperator);
}

std::pair<bool, std::string> WorkloadOptions::parseOptions(int argc, char * argv[])
{
    using boost::program_options::value;
    boost::program_options::options_description desc("Allowed options");

    desc.add_options() //
        ("help", "produce help message") //
        ("max_key_count", value<uint64_t>()->default_value(20000000), "Default is 2000w.") //
        ("write_key_distribution", value<std::string>()->default_value("uniform"), "uniform/normal/incremental") //
        ("write_count", value<uint64_t>()->default_value(5000000), "Default is 500w.") //
        ("write_thread_count", value<uint64_t>()->default_value(4), "") //
        ("max_write_per_sec", value<uint64_t>()->default_value(0), "") //
        //
        ("table", value<std::string>()->default_value("constant"), "constant/random") //
        ("pk_type", value<std::string>()->default_value("tidb_rowid"), "tidb_rowid") //
        ("columns_count", value<uint64_t>()->default_value(0), "0 means random columns count") //
        //
        ("failpoints,F", value<std::vector<std::string>>()->multitoken(), "failpoint(s) to enable: fp1 fp2 fp3...") //
        //
        ("log_file", value<std::string>()->default_value(""), "") //
        ("log_level", value<std::string>()->default_value("information"), "") //
        //
        ("verification", value<bool>()->default_value(true), "") //
        ("verify_round", value<uint64_t>()->default_value(10), "") //
        //
        ("random_kill", value<uint64_t>()->default_value(0), "") //
        ("max_sleep_sec", value<uint64_t>()->default_value(600), "") //
        //
        ("work_dirs", value<std::vector<std::string>>()->multitoken()->default_value(std::vector<std::string>{"tmp1", "tmp2", "tmp3"}, "tmp1 tmp2 tmp3"), "dir1 dir2 dir3...") //
        ("config_file", value<std::string>()->default_value(""), "Configuation file of DeltaTree") //
        //
        ("read_thread_count", value<uint64_t>()->default_value(1), "") //
        ("read_stream_count", value<uint64_t>()->default_value(4), "") //
        //
        ("testing_type", value<std::string>()->default_value(""), "daily_perf/daily_random") //
        //
        ("log_write_request", value<bool>()->default_value(false), "") //
        //
        ("enable_ps_v3", value<bool>()->default_value(true), "") //
        //
        ("bg_thread_count", value<uint64_t>()->default_value(4), "") //
        //
        ("table_name", value<std::string>()->default_value(""), "") //
        ("table_id", value<int64_t>()->default_value(-1), "") //
        ;

    boost::program_options::variables_map vm;
    boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
    if (vm.count("help") > 0)
    {
        std::stringstream ss;
        ss << desc;
        return {false, ss.str()};
    }

    max_key_count = vm["max_key_count"].as<uint64_t>();
    write_key_distribution = vm["write_key_distribution"].as<std::string>();
    write_count = vm["write_count"].as<uint64_t>();
    write_thread_count = vm["write_thread_count"].as<uint64_t>();
    max_write_per_sec = vm["max_write_per_sec"].as<uint64_t>();

    table = vm["table"].as<std::string>();
    pk_type = vm["pk_type"].as<std::string>();
    if (pk_type != "tidb_rowid")
    {
        return {false, fmt::format("pk_type must be tidb_rowid.")};
    }
    columns_count = vm["columns_count"].as<uint64_t>();

    if (vm.count("failpoints"))
    {
        failpoints = vm["failpoints"].as<std::vector<std::string>>();
    }

    log_file = vm["log_file"].as<std::string>();
    log_level = vm["log_level"].as<std::string>();

    verification = vm["verification"].as<bool>();
    verify_round = vm["verify_round"].as<uint64_t>();

    random_kill = vm["random_kill"].as<uint64_t>();
    max_sleep_sec = vm["max_sleep_sec"].as<uint64_t>();

    if (vm.count("work_dirs"))
    {
        work_dirs = vm["work_dirs"].as<std::vector<std::string>>();
    }
    config_file = vm["config_file"].as<std::string>();

    // Randomly kill could cause DeltaMergeStore loss some data, so disallow verification and random_kill both enable.
    if (verification && random_kill > 0)
    {
        return {false, fmt::format("Disallow verification({}) and randomly kill({}) are enabled simultaneously.", verification, random_kill)};
    }

    if (log_file.empty())
    {
        log_file = fmt::format("{}/dt_workload_{}.log", work_dirs[0], localTime());
    }

    read_thread_count = vm["read_thread_count"].as<uint64_t>();
    read_stream_count = vm["read_stream_count"].as<uint64_t>();

    testing_type = vm["testing_type"].as<std::string>();
    log_write_request = vm["log_write_request"].as<bool>();

    enable_ps_v3 = vm["enable_ps_v3"].as<bool>();

    bg_thread_count = vm["bg_thread_count"].as<uint64_t>();

    table_id = vm["table_id"].as<int64_t>();
    table_name = vm["table_name"].as<std::string>();

    return {true, toString()};
}

void WorkloadOptions::initFailpoints() const
{
#ifdef FIU_ENABLE
    fiu_init(0);
    for (const auto & fp : failpoints)
    {
        DB::FailPointHelper::enableFailPoint(fp);
    }
#endif
}

} // namespace DB::DM::tests

max_stars_repo_name: pingcap/tiflash

max_issues_repo_name: pingcap/tiflash

max_forks_repo_name: pingcap/tiflash