text:
#include "vtkTemporalTransforms.h"
#include "vtkEigenTools.h"
#include "vtkConversions.h"

#include <vtkCellData.h>
#include <vtkPolyLine.h>
#include <vtkTransform.h>

#include <cmath>

// Eigen
#include <Eigen/Dense>

//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkTemporalTransforms)

//-----------------------------------------------------------------------------
vtkTemporalTransforms::vtkTemporalTransforms()
{
  auto points = vtkSmartPointer<vtkPoints>::New();

  auto timeArray = vtkSmartPointer<vtkDoubleArray>::New();
  timeArray->SetName(this->TimeArrayName);

  auto orientationArray = vtkSmartPointer<vtkDoubleArray>::New();
  orientationArray->SetName(this->OrientationArrayName);
  orientationArray->SetNumberOfComponents(4);

  this->SetPoints(points);
  this->GetPointData()->AddArray(timeArray);
  this->GetPointData()->AddArray(orientationArray);
}

//-----------------------------------------------------------------------------
vtkSmartPointer<vtkTemporalTransforms> vtkTemporalTransforms::CreateFromPolyData(vtkPolyData *poly)
{
  if (!poly)
  {
    return nullptr;
  }
  // copy the data into a vtkTemporalTransforms before checking that it is well-formed
  auto temporalTransforms = vtkSmartPointer<vtkTemporalTransforms>::New();
  temporalTransforms->ShallowCopy(poly);

  bool isWellFormed = temporalTransforms->GetTimeArray() &&
                      temporalTransforms->GetTranslationArray() &&
                      temporalTransforms->GetOrientationArray();
  if(!isWellFormed)
  {
    return nullptr;
  }

  // create polyline if needed
  if (temporalTransforms->GetLines()->GetNumberOfCells() == 0)
  {
    // create the cell in the same time for visualization
    auto polyLine = vtkSmartPointer<vtkPolyLine>::New();
    polyLine->GetPointIds()->SetNumberOfIds(temporalTransforms->GetNumberOfPoints());
    for (vtkIdType i = 0; i < temporalTransforms->GetNumberOfPoints(); i++)
    {
      polyLine->GetPointIds()->SetId(i,i);
    }
    auto cell = vtkSmartPointer<vtkCellArray>::New();
    cell->InsertNextCell(polyLine);
    temporalTransforms->SetLines(cell);
  }

  return temporalTransforms;
}

vtkSmartPointer<vtkTransform> vtkTemporalTransforms::GetTransform(unsigned int transformNumber)
{
  auto axisAngle = this->GetOrientationArray();
  auto transform = vtkSmartPointer<vtkTransform>::New();
  transform->PostMultiply();
  double x = axisAngle->GetTuple4(transformNumber)[0];
  double y = axisAngle->GetTuple4(transformNumber)[1];
  double z = axisAngle->GetTuple4(transformNumber)[2];
  double w = axisAngle->GetTuple4(transformNumber)[3]
      * 180 / vtkMath::Pi(); // vtk need degrees not radians
  transform->RotateWXYZ(w, x, y, z);
  transform->Translate(this->GetTranslationArray()->GetTuple(transformNumber));

  return transform;
}

vtkSmartPointer<vtkVelodyneTransformInterpolator> vtkTemporalTransforms::CreateInterpolator()
{
  auto interpolator = vtkSmartPointer<vtkVelodyneTransformInterpolator>::New();

  auto timestamp = this->GetTimeArray();

  for (unsigned int i = 0; i < this->GetNumberOfPoints(); i++)
  {
    // get timestamp
    double currentTimestamp = timestamp->GetTuple1(i);

    // create the tranform
    auto transform = this->GetTransform(i);

    // add the tranform to the interpolator
    if (!vtkMath::IsNan(currentTimestamp))
    {
      interpolator->AddTransform(currentTimestamp, transform);
    }
    else
    {
      vtkErrorMacro(<< "Timestamp " << i << "is not a number")
    }
  }
  interpolator->Modified();
  return interpolator;
}

//-----------------------------------------------------------------------------
void vtkTemporalTransforms::SetOrientationArray(vtkDoubleArray *array)
{
  if (array->GetNumberOfComponents() != 4)
  {
    vtkErrorMacro(<< "The orientation array has " << array->GetNumberOfComponents()
                  << ". 4 components are expected")
  }
  array->SetName(this->OrientationArrayName);
  this->GetPointData()->AddArray(array);
}

//-----------------------------------------------------------------------------
void vtkTemporalTransforms::SetTranslationArray(vtkDataArray *array)
{
  if (array->GetNumberOfComponents() != 3)
  {
    vtkErrorMacro(<< "The translation array has " << array->GetNumberOfComponents()
                  << ". 3 components are expected")
  }
  auto points =  vtkSmartPointer<vtkPoints>::New();
  this->SetPoints(points);
  this->GetPoints()->SetData(array);

  // create the cell in the same time for visualization
  auto polyLine = vtkSmartPointer<vtkPolyLine>::New();
  polyLine->GetPointIds()->SetNumberOfIds(this->GetNumberOfPoints());
  for (vtkIdType i = 0; i < array->GetNumberOfTuples(); i++)
  {
    polyLine->GetPointIds()->SetId(i,i);
  }
  auto cell = vtkSmartPointer<vtkCellArray>::New();
  cell->InsertNextCell(polyLine);
  this->SetLines(cell);
}

//-----------------------------------------------------------------------------
void vtkTemporalTransforms::SetTimeArray(vtkDoubleArray *array)
{
  if (array->GetNumberOfComponents() != 1)
  {
    vtkErrorMacro(<< "The time array has " << array->GetNumberOfComponents()
                  << ". 1 component is expected")
  }
  array->SetName(this->TimeArrayName);
  this->GetPointData()->AddArray(array);
}

//-----------------------------------------------------------------------------
vtkSmartPointer<vtkTemporalTransforms> vtkTemporalTransforms::IsometricTransform(vtkSmartPointer<vtkTransform> H)
{
  vtkSmartPointer<vtkTemporalTransforms> outputPoses = vtkSmartPointer<vtkTemporalTransforms>::New();
  outputPoses->DeepCopy(this);

  // First, compute the rotation and translation from H 4x4 matrix
  std::pair<Eigen::Vector3d, Eigen::Vector3d> transParams = GetPoseParamsFromTransform(H);
  Eigen::Matrix3d R0 = RollPitchYawToMatrix(transParams.first);
  Eigen::Vector3d T0 = transParams.second;

  // We need to change the angle axis and position arrays
  vtkDoubleArray* xyzwArray = vtkDoubleArray::SafeDownCast(this->GetOrientationArray());
  vtkDoubleArray* xyzArray = vtkDoubleArray::SafeDownCast(this->GetTranslationArray());

  // Loop over the transforms points
  for (unsigned int transformIndex = 0; transformIndex < this->GetNumberOfPoints(); transformIndex++)
  {
    // Get the angle-axis representation
    double* xyzw = xyzwArray->GetTuple4(transformIndex);
    double* xyz = xyzArray->GetTuple3(transformIndex);

    // Compute the corresponding rotation matrix that
    // correspond to the angle axis representation
    Eigen::Vector3d T(xyz[0], xyz[1], xyz[2]);
    Eigen::AngleAxisd angleAxis(xyzw[3], Eigen::Vector3d(xyzw[0], xyzw[1], xyzw[2]));
    Eigen::Matrix3d R = angleAxis.toRotationMatrix();

    // Compute the new rotation and translation
    R = R0 * R;
    T = R0 * T + T0;

    // Get the axis angle representation of this new rotation
    Eigen::AngleAxisd newAngleAxis(R);
    double newXyzw[4] = {newAngleAxis.axis()(0), newAngleAxis.axis()(1),
                         newAngleAxis.axis()(2), newAngleAxis.angle()};

    // Replace it
    xyzwArray->SetTuple4(transformIndex, newXyzw[0], newXyzw[1], newXyzw[2], newXyzw[3]);
    xyzArray->SetTuple3(transformIndex, T(0), T(1), T(2));
  }
  return outputPoses;
}

//-----------------------------------------------------------------------------
vtkSmartPointer<vtkTemporalTransforms> vtkTemporalTransforms::CycloidicTransform(vtkSmartPointer<vtkTransform> H)
{
  vtkSmartPointer<vtkTemporalTransforms> outputPoses = vtkSmartPointer<vtkTemporalTransforms>::New();
  outputPoses->DeepCopy(this);

  // First, compute the rotation and translation from H 4x4 matrix
  std::pair<Eigen::Vector3d, Eigen::Vector3d> transParams = GetPoseParamsFromTransform(H);
  Eigen::Matrix3d R0 = RollPitchYawToMatrix(transParams.first);
  Eigen::Vector3d T0 = transParams.second;

  // We need to change the angle axis and position arrays
  vtkDoubleArray* xyzwArray = vtkDoubleArray::SafeDownCast(this->GetOrientationArray());
  vtkDoubleArray* xyzArray = vtkDoubleArray::SafeDownCast(this->GetTranslationArray());

  // Loop over the transforms points
  for (unsigned int transformIndex = 0; transformIndex < this->GetNumberOfPoints(); transformIndex++)
  {
    // Get the angle-axis representation
    double* xyzw = xyzwArray->GetTuple4(transformIndex);
    double* xyz = xyzArray->GetTuple3(transformIndex);

    // Compute the corresponding rotation matrix that
    // correspond to the angle axis representation
    Eigen::Vector3d T(xyz[0], xyz[1], xyz[2]);
    Eigen::AngleAxisd angleAxis(xyzw[3], Eigen::Vector3d(xyzw[0], xyzw[1], xyzw[2]));
    Eigen::Matrix3d R = angleAxis.toRotationMatrix();

    // Compute the new rotation and translation
    T = R * T0 + T;
    R = R * R0;

    // Get the axis angle representation of this new rotation
    Eigen::AngleAxisd newAngleAxis(R);
    double newXyzw[4] = {newAngleAxis.axis()(0), newAngleAxis.axis()(1),
                         newAngleAxis.axis()(2), newAngleAxis.angle()};

    // Replace it
    xyzwArray->SetTuple4(transformIndex, newXyzw[0], newXyzw[1], newXyzw[2], newXyzw[3]);
    xyzArray->SetTuple3(transformIndex, T(0), T(1), T(2));
  }
  return outputPoses;
}

//-----------------------------------------------------------------------------
void vtkTemporalTransforms::PushBack(double time, const Eigen::AngleAxisd& orientation , const Eigen::Vector3d translation)
{
  this->GetTimeArray()->InsertNextTuple1(time);
  this->GetOrientationArray()->InsertNextTuple4(orientation.axis()[0],
                                                orientation.axis()[1],
                                                orientation.axis()[2],
                                                orientation.angle());
  this->GetTranslationArray()->InsertNextTuple(static_cast<const double*>(translation.data()));

  // replace the cell by a line with one more point
  auto polyLine = vtkSmartPointer<vtkPolyLine>::New();
  polyLine->GetPointIds()->SetNumberOfIds(this->GetNumberOfPoints());
  for (vtkIdType i = 0; i < this->GetNumberOfPoints(); i++)
  {
    polyLine->GetPointIds()->SetId(i,i);
  }
  auto cell = vtkSmartPointer<vtkCellArray>::New();
  cell->InsertNextCell(polyLine);
  this->SetLines(cell);
}

vtkSmartPointer<vtkTemporalTransforms> vtkTemporalTransforms::ExtractTimes(double tstart, double tend)
{
  auto extract = vtkSmartPointer<vtkTemporalTransforms>::New();

  for (unsigned int i = 0; i < this->GetNumberOfPoints(); i++)
  {
    // get timestamp
    double currentTimestamp = this->GetTimeArray()->GetTuple1(i);
    if (currentTimestamp < tstart || currentTimestamp > tend) {
      continue;
    }

    extract->GetTimeArray()->InsertNextTuple1(currentTimestamp);
    double* aa = this->GetOrientationArray()->GetTuple4(i);
    extract->GetOrientationArray()->InsertNextTuple(aa);
    double* xyz = this->GetTranslationArray()->GetTuple3(i);
    extract->GetTranslationArray()->InsertNextTuple(xyz);
  }

  return extract;
}

vtkSmartPointer<vtkTemporalTransforms> vtkTemporalTransforms::Subsample(int N)
{

  auto extract = vtkSmartPointer<vtkTemporalTransforms>::New();

  for (unsigned int i = 0; i < this->GetNumberOfPoints(); i++)
  {
    if (i % N != 0)
    {
      continue;
    }
    // get timestamp
    double currentTimestamp = this->GetTimeArray()->GetTuple1(i);
    extract->GetTimeArray()->InsertNextTuple1(currentTimestamp);
    double* aa = this->GetOrientationArray()->GetTuple4(i);
    extract->GetOrientationArray()->InsertNextTuple(aa);
    double* xyz = this->GetTranslationArray()->GetTuple3(i);
    extract->GetTranslationArray()->InsertNextTuple(xyz);
  }

  auto polyLine = vtkSmartPointer<vtkPolyLine>::New();
  polyLine->GetPointIds()->SetNumberOfIds(extract->GetNumberOfPoints());
  for (vtkIdType i = 0; i < extract->GetNumberOfPoints(); i++)
  {
    polyLine->GetPointIds()->SetId(i,i);
  }
  auto cell = vtkSmartPointer<vtkCellArray>::New();
  cell->InsertNextCell(polyLine);
  extract->SetLines(cell);

  return extract;
}

vtkSmartPointer<vtkTemporalTransforms> vtkTemporalTransforms::ApplyTimeshift(double shift)
{
  auto timeshifted = vtkSmartPointer<vtkTemporalTransforms>::New();

  for (unsigned int i = 0; i < this->GetNumberOfPoints(); i++)
  {
    double currentTimestamp = this->GetTimeArray()->GetTuple1(i);
    timeshifted->GetTimeArray()->InsertNextTuple1(currentTimestamp + shift);
    double* aa = this->GetOrientationArray()->GetTuple4(i);
    timeshifted->GetOrientationArray()->InsertNextTuple(aa);
    double* xyz = this->GetTranslationArray()->GetTuple3(i);
    timeshifted->GetTranslationArray()->InsertNextTuple(xyz);
  }

  auto polyLine = vtkSmartPointer<vtkPolyLine>::New();
  polyLine->GetPointIds()->SetNumberOfIds(timeshifted->GetNumberOfPoints());
  for (vtkIdType i = 0; i < timeshifted->GetNumberOfPoints(); i++)
  {
    polyLine->GetPointIds()->SetId(i,i);
  }
  auto cell = vtkSmartPointer<vtkCellArray>::New();
  cell->InsertNextCell(polyLine);
  timeshifted->SetLines(cell);

  bool isWellFormed = timeshifted->GetTimeArray() &&
                      timeshifted->GetTranslationArray() &&
                      timeshifted->GetOrientationArray();
  if(!isWellFormed)
  {
    std::cout << "warning ! timeshifted not well formed" << std::endl;
  }

  return timeshifted;
}

vtkSmartPointer<vtkTemporalTransforms> vtkTemporalTransforms::ApplyScale(double scale)
{
  auto scaled = vtkSmartPointer<vtkTemporalTransforms>::New();

  for (unsigned int i = 0; i < this->GetNumberOfPoints(); i++)
  {
    double currentTimestamp = this->GetTimeArray()->GetTuple1(i);
    scaled->GetTimeArray()->InsertNextTuple1(currentTimestamp);
    double* aa = this->GetOrientationArray()->GetTuple4(i);
    scaled->GetOrientationArray()->InsertNextTuple(aa);
    double* xyz = this->GetTranslationArray()->GetTuple3(i);
    xyz[0] *= scale;
    xyz[1] *= scale;
    xyz[2] *= scale;
    scaled->GetTranslationArray()->InsertNextTuple(xyz);
  }

  auto polyLine = vtkSmartPointer<vtkPolyLine>::New();
  polyLine->GetPointIds()->SetNumberOfIds(scaled->GetNumberOfPoints());
  for (vtkIdType i = 0; i < scaled->GetNumberOfPoints(); i++)
  {
    polyLine->GetPointIds()->SetId(i,i);
  }
  auto cell = vtkSmartPointer<vtkCellArray>::New();
  cell->InsertNextCell(polyLine);
  scaled->SetLines(cell);

  bool isWellFormed = scaled->GetTimeArray() && scaled->GetTranslationArray() &&
                      scaled->GetOrientationArray();
  if(!isWellFormed)
  {
    std::cout << "warning ! scaled not well formed" << std::endl;
  }

  return scaled;
}

ext: cxx

max_stars_repo_name: zhihua-wang/VeloView

max_issues_repo_name: zactodd/VeloView

max_forks_repo_name: zactodd/VeloView


text:
#ifndef LM_FILTER_VOCAB_H
#define LM_FILTER_VOCAB_H

// Vocabulary-based filters for language models.

#include "../../util/multi_intersection.hh"
#include "../../util/string_piece.hh"
#include "../../util/string_piece_hash.hh"
#include "../../util/tokenize_piece.hh"

#include <boost/noncopyable.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/unordered/unordered_map.hpp>
#include <boost/unordered/unordered_set.hpp>

#include <string>
#include <vector>

namespace lm {
namespace vocab {

void ReadSingle(std::istream &in, boost::unordered_set<std::string> &out);

// Read one sentence vocabulary per line.  Return the number of sentences.
unsigned int ReadMultiple(std::istream &in, boost::unordered_map<std::string, std::vector<unsigned int> > &out);

/* Is this a special tag like <s> or <UNK>?  This actually includes anything
 * surrounded with < and >, which most tokenizers separate for real words, so
 * this should not catch real words as it looks at a single token.
 */
inline bool IsTag(const StringPiece &value) {
  // The parser should never give an empty string.
  assert(!value.empty());
  return (value.data()[0] == '<' && value.data()[value.size() - 1] == '>');
}

class Single {
  public:
    typedef boost::unordered_set<std::string> Words;

    explicit Single(const Words &vocab) : vocab_(vocab) {}

    template <class Iterator> bool PassNGram(const Iterator &begin, const Iterator &end) {
      for (Iterator i = begin; i != end; ++i) {
        if (IsTag(*i)) continue;
        if (FindStringPiece(vocab_, *i) == vocab_.end()) return false;
      }
      return true;
    }

  private:
    const Words &vocab_;
};

class Union {
  public:
    typedef boost::unordered_map<std::string, std::vector<unsigned int> > Words;

    explicit Union(const Words &vocabs) : vocabs_(vocabs) {}

    template <class Iterator> bool PassNGram(const Iterator &begin, const Iterator &end) {
      sets_.clear();

      for (Iterator i(begin); i != end; ++i) {
        if (IsTag(*i)) continue;
        Words::const_iterator found(FindStringPiece(vocabs_, *i));
        if (vocabs_.end() == found) return false;
        sets_.push_back(boost::iterator_range<const unsigned int*>(&*found->second.begin(), &*found->second.end()));
      }
      return (sets_.empty() || util::FirstIntersection(sets_));
    }

  private:
    const Words &vocabs_;

    std::vector<boost::iterator_range<const unsigned int*> > sets_;
};

class Multiple {
  public:
    typedef boost::unordered_map<std::string, std::vector<unsigned int> > Words;

    Multiple(const Words &vocabs) : vocabs_(vocabs) {}

  private:
    // Callback from AllIntersection that does AddNGram.
    template <class Output> class Callback {
      public:
        Callback(Output &out, const StringPiece &line) : out_(out), line_(line) {}

        void operator()(unsigned int index) {
          out_.SingleAddNGram(index, line_);
        }

      private:
        Output &out_;
        const StringPiece &line_;
    };

  public:
    template <class Iterator, class Output> void AddNGram(const Iterator &begin, const Iterator &end, const StringPiece &line, Output &output) {
      sets_.clear();
      for (Iterator i(begin); i != end; ++i) {
        if (IsTag(*i)) continue;
        Words::const_iterator found(FindStringPiece(vocabs_, *i));
        if (vocabs_.end() == found) return;
        sets_.push_back(boost::iterator_range<const unsigned int*>(&*found->second.begin(), &*found->second.end()));
      }
      if (sets_.empty()) {
        output.AddNGram(line);
        return;
      }

      Callback<Output> cb(output, line);
      util::AllIntersection(sets_, cb);
    }

    template <class Output> void AddNGram(const StringPiece &ngram, const StringPiece &line, Output &output) {
      AddNGram(util::TokenIter<util::SingleCharacter, true>(ngram, ' '), util::TokenIter<util::SingleCharacter, true>::end(), line, output);
    }

    void Flush() const {}

  private:
    const Words &vocabs_;

    std::vector<boost::iterator_range<const unsigned int*> > sets_;
};

} // namespace vocab
} // namespace lm

#endif // LM_FILTER_VOCAB_H

ext: hh


text:
/*    Copyright (c) 2010-2018, Delft University of Technology
 *    All rigths reserved
 *
 *    This file is part of the Tudat. Redistribution and use in source and
 *    binary forms, with or without modification, are permitted exclusively
 *    under the terms of the Modified BSD license. You should have received
 *    a copy of the license with this file. If not, please or visit:
 *    http://tudat.tudelft.nl/LICENSE.
 *
 *    References
 *      Klothakis, A. and Nikolos, I., “Modeling of Rarefied Hypersonic Flows Using the Massively
 *        Parallel DSMC Kernel “SPARTA”,” in 8th GRACM International Congress on Computational Mechanics,
 *        Volos, Greece, July 2015.
 *      Dirkx, D. and Mooij, E., Conceptual Shape Optimization of Entry Vehicles. Springer, 2017.
 */

#include <boost/make_shared.hpp>
#include <boost/assign/list_of.hpp>

#include <Eigen/Geometry>

#include "Tudat/InputOutput/basicInputOutput.h"
#include "Tudat/InputOutput/matrixTextFileReader.h"
#include "Tudat/InputOutput/spartaDataReader.h"
#include "Tudat/InputOutput/spartaInputOutput.h"

#include "Tudat/Astrodynamics/Aerodynamics/rarefiedFlowAnalysis.h"

namespace tudat
{

namespace aerodynamics
{

using namespace unit_conversions;
using namespace input_output;

//! Returns default values of altitude for use in RarefiedFlowAnalysis.
std::vector< double > getDefaultRarefiedFlowAltitudePoints(
        const std::string& targetPlanet )
{
    std::vector< double > altitudePoints;

    // Set default points for Earth.
    if ( targetPlanet == "Earth" )
    {
        altitudePoints.resize( 5 );
        altitudePoints[ 0 ] = 225.0e3;
        altitudePoints[ 1 ] = 250.0e3;
        altitudePoints[ 2 ] = 300.0e3;
        altitudePoints[ 3 ] = 400.0e3;
        altitudePoints[ 4 ] = 600.0;
    }
    // Set default points for Mars.
    else if ( targetPlanet == "Mars" )
    {
        altitudePoints.resize( 5 );
        altitudePoints[ 0 ] = 125.0e3;
        altitudePoints[ 1 ] = 150.0e3;
        altitudePoints[ 2 ] = 200.0e3;
        altitudePoints[ 3 ] = 300.0e3;
        altitudePoints[ 4 ] = 500.0e3;
    }
    // Give error otherwise.
    else
    {
        throw std::runtime_error( "Error in altitude range selection for SPARTA simulation. "
                                  "Planet not supported." );
    }
    return altitudePoints;
}

//! Returns default values of Mach number for use in RarefiedFlowAnalysis.
std::vector< double > getDefaultRarefiedFlowMachPoints(
        const std::string& machRegime )
{
    std::vector< double > machPoints;

    // Set default points for full hypersonic analysis.
    if ( machRegime == "Full" )
    {
        machPoints.resize( 6 );

        machPoints[ 0 ] = 3.0;
        machPoints[ 1 ] = 4.0;
        machPoints[ 2 ] = 5.0;
        machPoints[ 3 ] = 8.0;
        machPoints[ 4 ] = 10.0;
        machPoints[ 5 ] = 20.0;
    }
    // Set default points for low hypersonic analysis.
    else if ( machRegime == "Low" )
    {
        machPoints.resize( 5 );
        machPoints[ 0 ] = 3.0;
        machPoints[ 1 ] = 4.0;
        machPoints[ 2 ] = 5.0;
        machPoints[ 3 ] = 8.0;
        machPoints[ 4 ] = 10.0;
    }
    // Set default points for high hypersonic analysis.
    else if ( machRegime == "High" )
    {
        machPoints.resize( 4 );
        machPoints[ 0 ] = 5.0;
        machPoints[ 1 ] = 8.0;
        machPoints[ 2 ] = 10.0;
        machPoints[ 3 ] = 20.0;
    }
    return machPoints;
}

//! Returns default values of angle of attack for use in RarefiedFlowAnalysis.
std::vector< double > getDefaultRarefiedFlowAngleOfAttackPoints(
        const std::string& angleOfAttackRegime )
{
    std::vector< double > angleOfAttackPoints;

    // Set default angles of attack
    double a = - 35;
    while ( a <= 35 )
    {
        angleOfAttackPoints.push_back( convertDegreesToRadians( a ) );
        a += 5;
    }

    // Add extra points if required
    if ( angleOfAttackRegime == "Full" )
    {
        std::vector< double > frontExtension = { convertDegreesToRadians( -85.0 ),
                                                 convertDegreesToRadians( -70.0 ),
                                                 convertDegreesToRadians( -55.0 ),
                                                 convertDegreesToRadians( -40.0 ) };
        std::vector< double > rearExtension = { convertDegreesToRadians( 40.0 ),
                                                convertDegreesToRadians( 55.0 ),
                                                convertDegreesToRadians( 70.0 ),
                                                convertDegreesToRadians( 85.0 ) };
        angleOfAttackPoints.insert( angleOfAttackPoints.begin( ), frontExtension.begin( ), frontExtension.end( ) );
        angleOfAttackPoints.insert( angleOfAttackPoints.end( ), rearExtension.begin( ), rearExtension.end( ) );
    }
    return angleOfAttackPoints;
}

//! Function to sort the rows of a matrix, based on the specified column and specified order.
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > sortMatrixRows(
        const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor >& matrixToBeSorted,
        const int referenceColumn, const bool descendingOrder )
{
    // Declare eventual output vector
    Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > sortedMatrix;
    sortedMatrix.resizeLike( matrixToBeSorted );

    // Loop over rows and assign to new matrix
    Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor > currentRow;
    for ( unsigned int i = 0; i < matrixToBeSorted.rows( ); i++ )
    {
        // Retrieve each row in 'scrambled order' and assign it to the new matrix
        currentRow = matrixToBeSorted.row( i );

        // Based on requested order
        if ( descendingOrder )
        {
            sortedMatrix.row( matrixToBeSorted.rows( ) - currentRow[ referenceColumn ] ) = currentRow;
            // no (- 1)'s are needed since both are defined starting from 1
        }
        else
        {
            sortedMatrix.row( currentRow[ referenceColumn ] - 1 ) = currentRow;
        }
    }

    // Give output
    return sortedMatrix;
}

//! Default constructor.
RarefiedFlowAnalysis::RarefiedFlowAnalysis(
        const std::string& SPARTAExecutable,
        const std::vector< std::vector< double > >& dataPointsOfIndependentVariables,
        boost::shared_ptr< TabulatedAtmosphere > atmosphereModel,
        const std::string& simulationGases,
        const std::string& geometryFileUser,
        const double referenceArea,
        const double referenceLength,
        const int referenceAxis,
        const Eigen::Vector3d& momentReferencePoint,
        const double gridSpacing,
        const double simulatedParticlesPerCell,
        const double wallTemperature,
        const double accommodationCoefficient,
        const bool printProgressInCommandWindow,
        const std::string MPIExecutable,
        const unsigned int numberOfCores ) :
    AerodynamicCoefficientGenerator< 3, 6 >(
          dataPointsOfIndependentVariables, referenceLength, referenceArea, referenceLength,
          momentReferencePoint,
          boost::assign::list_of( altitude_dependent )( mach_number_dependent )( angle_of_attack_dependent ),
          true, true ),
      SPARTAExecutable_( SPARTAExecutable ),simulationGases_( simulationGases ), referenceAxis_( referenceAxis ),
      gridSpacing_( gridSpacing ), simulatedParticlesPerCell_( simulatedParticlesPerCell ),
      wallTemperature_( wallTemperature ), accommodationCoefficient_( accommodationCoefficient ),
      printProgressInCommandWindow_( printProgressInCommandWindow ), MPIExecutable_( MPIExecutable ),
      numberOfCores_( numberOfCores ), referenceDimension_( static_cast< unsigned int >( referenceAxis_ ) )
{
    // Check inputs
    if ( referenceDimension_ > 2 )
    {
        throw std::runtime_error( "Error in SPARTA rarefied flow analysis. Reference axis makes "
                                  "no sense for a universe with 3 spacial dimensions (i.e., our universe). "
                                  "Note that the first dimension is identified with 0." );
    }

    // Analyze vehicle geometry
    analyzeGeometryFile( geometryFileUser );

    // Find atmospheric conditions based on altitude
    for ( unsigned int h = 0; h < dataPointsOfIndependentVariables_.at( 0 ).size( ); h++ )
    {
        atmosphericConditions_[ density_index ].push_back(
                    atmosphereModel->getDensity( dataPointsOfIndependentVariables_.at( 0 ).at( h ) ) );
        atmosphericConditions_[ pressure_index ].push_back(
                    atmosphereModel->getPressure( dataPointsOfIndependentVariables_.at( 0 ).at( h ) ) );
        atmosphericConditions_[ temperature_index ].push_back(
                    atmosphereModel->getTemperature( dataPointsOfIndependentVariables_.at( 0 ).at( h ) ) );
        atmosphericConditions_[ speed_of_sound_index ].push_back(
                    atmosphereModel->getSpeedOfSound( dataPointsOfIndependentVariables_.at( 0 ).at( h ) ) );
        atmosphericConditions_[ number_density_index ].push_back(
                    tudat::physical_constants::AVOGADRO_CONSTANT * atmosphericConditions_[ density_index ].at( h ) /
                    atmosphereModel->getMolarMass( dataPointsOfIndependentVariables_.at( 0 ).at( h ) ) );
    }

    // Get simulation conditions
    getSimulationConditions( );

    // Read SPARTA input template
    inputTemplate_ = readSpartaInputFileTemplate( );

    // Copy input shape file to default name
    std::string commandString = "cp " + geometryFileUser + " " + getSpartaInternalGeometryFile( );
    std::system( commandString.c_str( ) );

    // Run SPARTA simulation
    generateCoefficients( );

    // Create interpolator object
    createInterpolator( );
}

//! Open and read geometry file for SPARTA simulation.
void RarefiedFlowAnalysis::analyzeGeometryFile( const std::string& geometryFileUser )
{
    // Extract information on vehicle geometry
    std::pair< Eigen::Matrix< double, Eigen::Dynamic, 3 >, Eigen::Matrix< int, Eigen::Dynamic, 3 > >
            geometryData = readSpartaGeometryFile( geometryFileUser );
    shapePoints_ = geometryData.first;
    shapeTriangles_ = geometryData.second;
    numberOfPoints_ = shapePoints_.rows( );
    numberOfTriangles_ = shapeTriangles_.rows( );

    // Get maximum and minimum values in each dimension
    maximumDimensions_ = shapePoints_.colwise( ).maxCoeff( );
    minimumDimensions_ = shapePoints_.colwise( ).minCoeff( );

    // Compute normal to surface elements, area of surface elements and moment arm values
    Eigen::Matrix3d currentVertices;
    Eigen::Vector3d currentNormal;
    Eigen::Vector3d currentCentroid;
    double currentNormalNorm;
    elementSurfaceNormal_.resize( 3, numberOfTriangles_ );
    elementSurfaceArea_.resize( 1, numberOfTriangles_ );
    elementMomentArm_.resize( 3, numberOfTriangles_ );
    for ( int i = 0; i < numberOfTriangles_; i++ )
    {
        // Compute properties of current surface element
        for ( unsigned int j = 0; j < 3; j++ )
        {
            currentVertices.row( j ) = shapePoints_.row( shapeTriangles_( i, j ) - 1 );
        }
        currentNormal = ( currentVertices.row( 1 ) - currentVertices.row( 0 ) ).cross(
                    currentVertices.row( 2 ) - currentVertices.row( 0 ) );
        currentNormalNorm = currentNormal.norm( );
        currentCentroid = currentVertices.colwise( ).sum( ) / 3.0;

        // Find normal, area and distance to reference point
        elementSurfaceNormal_.col( i ) = currentNormal / currentNormalNorm;
        elementSurfaceArea_( i ) = 0.5 * currentNormalNorm;
        elementMomentArm_.col( i ) = currentCentroid - momentReferencePoint_;
    }

    // Compute cross-sectional area
    for ( unsigned int i = 0; i < 3; i++ )
    {
        shapeCrossSectionalArea_( i ) =
                0.5 * elementSurfaceNormal_.row( i ).cwiseAbs( ).dot( elementSurfaceArea_ );
    }

    // Check consistency with input dimensions
    const double tolerance = 1e-5;
    if ( std::fabs( shapeCrossSectionalArea_( referenceDimension_ ) - referenceArea_ ) > tolerance )
    {
        throw std::runtime_error( "Error in SPARTA geometry file. Input reference area does not match the "
                                  "combination of reference axis and geometry. Note that the first dimension "
                                  "is identified with 0. Tolerance set to: " + std::to_string( tolerance ) );
    }
}

//! Retrieve simulation conditions based on input and geometry.
void RarefiedFlowAnalysis::getSimulationConditions( )
{
    // Simulation boundary and grid
    for ( unsigned int i = 0; i < 3; i++ )
    {
        simulationBoundaries_( 2 * i ) = minimumDimensions_( i ) + 0.5 * minimumDimensions_.minCoeff( ); // add extra space around shape
        simulationBoundaries_( 2 * i + 1 ) = maximumDimensions_( i ) + 0.5 * maximumDimensions_.maxCoeff( ); // add extra space around shape
        if ( i == referenceDimension_ )
        {
            simulationBoundaries_( 2 * i ) -= 1.0; // add extra space along axis of velocity
            simulationBoundaries_( 2 * i + 1 ) += 1.0; // add extra space along axis of velocity
        }
        simulationGrid_( i ) = simulationBoundaries_( 2 * i + 1 ) - simulationBoundaries_( 2 * i );
    }
    simulationGrid_ /= gridSpacing_;

    // Convert molecular speed ratio to stream velocity and compute simulation time step and ratio of real to simulated variables
    freeStreamVelocities_.resize( dataPointsOfIndependentVariables_.at( 0 ).size( ), dataPointsOfIndependentVariables_.at( 1 ).size( ) );
    simulationTimeStep_.resize( dataPointsOfIndependentVariables_.at( 0 ).size( ), dataPointsOfIndependentVariables_.at( 1 ).size( ) );
    ratioOfRealToSimulatedParticles_.resize( dataPointsOfIndependentVariables_.at( 0 ).size( ), 1 );
    double simulationBoxLengthAlongReferenceAxis = ( simulationBoundaries_( 2 * referenceDimension_ + 1 ) -
                                                     simulationBoundaries_( 2 * referenceDimension_ ) );
    for ( unsigned int h = 0; h < dataPointsOfIndependentVariables_.at( 0 ).size( ); h++ )
    {
        for ( unsigned int m = 0; m < dataPointsOfIndependentVariables_.at( 1 ).size( ); m++ )
        {
            freeStreamVelocities_( h, m ) = dataPointsOfIndependentVariables_.at( 1 ).at( m ) *
                    atmosphericConditions_[ speed_of_sound_index ].at( h );
            simulationTimeStep_( h, m ) = 0.1 * simulationBoxLengthAlongReferenceAxis /
                    freeStreamVelocities_( h, m );
            // time step is taken as time it takes for a particle to travel for 10 % of the box
        }
        ratioOfRealToSimulatedParticles_( h ) = atmosphericConditions_[ number_density_index ].at( h ) *
                std::pow( gridSpacing_, 3 ) / simulatedParticlesPerCell_;
    }
}

//! Generate aerodynamic database.
void RarefiedFlowAnalysis::generateCoefficients( )
{
    // Inform user on progress
    std::cout << "Initiating SPARTA simulation. This may take a while." << std::endl;

    // Generate command string for SPARTA
    std::string runSPARTACommandString = "cd " + getSpartaDataPath( ) + "; ";
    if ( MPIExecutable_ != "" )
    {
        if ( numberOfCores_ < 1 )
        {
            throw std::runtime_error( "Error in SPARTA rarefied flow analysis. Number of cores needs to be "
                                      "an integer value larger or equal to one." );
        }
        runSPARTACommandString = runSPARTACommandString + MPIExecutable_ +
                " -np " + std::to_string( numberOfCores_ ) + " ";
    }
    runSPARTACommandString = runSPARTACommandString + SPARTAExecutable_ + " -echo log ";
    if ( !printProgressInCommandWindow_ )
    {
        runSPARTACommandString = runSPARTACommandString + "-screen none ";
    }
    runSPARTACommandString = runSPARTACommandString + "-in " + getSpartaInputFile( );

    // Predefine variables
    Eigen::Vector3d velocityVector;
    std::string temporaryOutputFile = getSpartaOutputPath( ) + "/coeff";
    std::vector< std::string > outputFileExtensions = { ".1000" };//{ ".400", ".600", ".800", ".1000" };
    Eigen::Matrix< double, Eigen::Dynamic, 7, Eigen::RowMajor > outputMatrix;
    Eigen::Matrix< double, 3, Eigen::Dynamic > meanPressureValues;
    Eigen::Matrix< double, 3, Eigen::Dynamic > meanShearValues;

    // Loop over simulation parameters and run SPARTA
    int systemStatus;
    // Loop over altitude
    for ( unsigned int h = 0; h < dataPointsOfIndependentVariables_.at( 0 ).size( ); h++ )
    {
        // Inform user on progress
        std::cout << std::endl << "Altitude: "
                  << dataPointsOfIndependentVariables_.at( 0 ).at( h ) / 1e3
                  << " km" << std::endl;

        // Loop over Mach numbers
        for ( unsigned int m = 0; m < dataPointsOfIndependentVariables_.at( 1 ).size( ); m++ )
        {
            // Inform user on progress
            std::cout << "Mach number: "
                      << dataPointsOfIndependentVariables_.at( 1 ).at( m )
                      << std::endl;

            // Loop over angles of attack
            for ( unsigned int a = 0; a < dataPointsOfIndependentVariables_.at( 2 ).size( ); a++ )
            {
                // Inform user on progress
                std::cout << "Angle of attack: "
                          << convertRadiansToDegrees( dataPointsOfIndependentVariables_.at( 2 ).at( a ) )
                          << " deg" << std::endl;

                // Get velocity vector
                velocityVector = Eigen::Vector3d::Zero( );
                velocityVector( referenceDimension_ ) = ( ( referenceAxis_ >= 0 ) ? - 1.0 : 1.0 ) *
                        freeStreamVelocities_( h, m );

                // Print to file
                FILE * fileIdentifier = std::fopen( getSpartaInputFile( ).c_str( ), "w" );
                std::fprintf( fileIdentifier, inputTemplate_.c_str( ),
                              simulationBoundaries_( 0 ), simulationBoundaries_( 1 ), simulationBoundaries_( 2 ),
                              simulationBoundaries_( 3 ), simulationBoundaries_( 4 ), simulationBoundaries_( 5 ),
                              simulationGrid_( 0 ), simulationGrid_( 1 ), simulationGrid_( 2 ),
                              atmosphericConditions_[ number_density_index ].at( h ), ratioOfRealToSimulatedParticles_( h ),
                              simulationGases_.c_str( ),
                              simulationGases_.c_str( ), velocityVector( 0 ), velocityVector( 1 ), velocityVector( 2 ),
                              simulationGases_.c_str( ), atmosphericConditions_[ temperature_index ].at( h ),
                              convertRadiansToDegrees( dataPointsOfIndependentVariables_.at( 2 ).at( a ) ),
                              wallTemperature_, accommodationCoefficient_,
                              simulationTimeStep_( h, m ),
                              getSpartaOutputDirectory( ).c_str( ) );
                std::fclose( fileIdentifier );

                // Run SPARTA
                systemStatus = std::system( runSPARTACommandString.c_str( ) );
                if ( systemStatus != 0 )
                {
                    throw std::runtime_error( "Error in SPARTA rarefied flow analysis. "
                                              "SPARTA Simulation failed. See the log.sparta file in "
                                              "Tudat/External/SPARTA/ for more details." );
                }

                // Loop over angles of attack
                meanPressureValues.resize( 3, numberOfTriangles_ );
                meanShearValues.resize( 3, numberOfTriangles_ );

                // Read output files and compute mean pressure and shear force values
                meanPressureValues.setZero( );
                meanShearValues.setZero( );
                for ( unsigned int i = 0; i < outputFileExtensions.size( ); i++ )
                {
                    outputMatrix = readMatrixFromFile( temporaryOutputFile + outputFileExtensions.at( i ), "\t ;,", "%", 9 );
                    outputMatrix = sortMatrixRows( outputMatrix, 0 );
                    for ( unsigned int j = 0; j < 3; j++ )
                    {
                        meanPressureValues.row( j ) += outputMatrix.col( j + 1 ).transpose( );
                        meanShearValues.row( j ) += outputMatrix.col( j + 4 ).transpose( );
                    }
                }
                meanPressureValues /= outputFileExtensions.size( );
                meanShearValues /= outputFileExtensions.size( );

                // Convert pressure and shear forces to aerodynamic coefficients
                aerodynamicCoefficients_[ h ][ m ][ a ] = computeAerodynamicCoefficientsFromPressureShearForces(
                            meanPressureValues,
                            meanShearValues,
                            atmosphericConditions_[ density_index ].at( h ),
                            atmosphericConditions_[ pressure_index ].at( h ),
                            freeStreamVelocities_( h, m ),
                            elementSurfaceNormal_,
                            elementSurfaceArea_,
                            elementMomentArm_,
                            referenceArea_,
                            referenceLength_ );

                // Clean up results folder
                std::string commandString = "rm " + getSpartaOutputPath( ) + "/coeff.*";
                std::system( commandString.c_str( ) );
            }
        }
    }

    // Inform user on progress
    std::cout << std::endl << "SPARTA simulation complete." << std::endl << std::endl;
}

//! Get aerodynamic coefficients at specific conditions.
Eigen::Vector6d RarefiedFlowAnalysis::getAerodynamicCoefficientsDataPoint(
        const boost::array< int, 3 > independentVariables )
{
    // Return requested coefficients.
    return aerodynamicCoefficients_( independentVariables );
}

} // namespace aerodynamics

} // namespace tudat

ext: cpp

max_stars_repo_name: MattTurnock/tudat

max_issues_repo_name: MattTurnock/tudat

max_forks_repo_name: MattTurnock/tudat


text:
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//
#pragma once

#include "InferenceTest.hpp"
#include "MobileNetSsdDatabase.hpp"

#include <boost/assert.hpp>
#include <boost/log/trivial.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <boost/test/tools/floating_point_comparison.hpp>

#include <vector>

namespace
{

template<typename Model>
class MobileNetSsdTestCase : public InferenceModelTestCase<Model>
{
public:
    MobileNetSsdTestCase(Model& model,
                         unsigned int testCaseId,
                         const MobileNetSsdTestCaseData& testCaseData)
        : InferenceModelTestCase<Model>(model,
                                        testCaseId,
                                        { std::move(testCaseData.m_InputData) },
                                        { k_OutputSize1, k_OutputSize2, k_OutputSize3, k_OutputSize4 })
        , m_FloatComparer(boost::math::fpc::percent_tolerance(1.0f))
        , m_DetectedObjects(testCaseData.m_ExpectedDetectedObject)
    {}

    TestCaseResult ProcessResult(const InferenceTestOptions& options) override
    {
        const std::vector<float>& output1 = boost::get<std::vector<float>>(this->GetOutputs()[0]); // bounding boxes
        BOOST_ASSERT(output1.size() == k_OutputSize1);

        const std::vector<float>& output2 = boost::get<std::vector<float>>(this->GetOutputs()[1]); // classes
        BOOST_ASSERT(output2.size() == k_OutputSize2);

        const std::vector<float>& output3 = boost::get<std::vector<float>>(this->GetOutputs()[2]); // scores
        BOOST_ASSERT(output3.size() == k_OutputSize3);

        const std::vector<float>& output4 = boost::get<std::vector<float>>(this->GetOutputs()[3]); // valid detections
        BOOST_ASSERT(output4.size() == k_OutputSize4);

        const size_t numDetections = boost::numeric_cast<size_t>(output4[0]);

        // Check if number of valid detections matches expectations
        const size_t expectedNumDetections = m_DetectedObjects.size();
        if (numDetections != expectedNumDetections)
        {
            BOOST_LOG_TRIVIAL(error) << "Number of detections is incorrect: Expected (" <<
                expectedNumDetections << ")" << " but got (" << numDetections << ")";
            return TestCaseResult::Failed;
        }

        // Extract detected objects from output data
        std::vector<DetectedObject> detectedObjects;
        const float* outputData = output1.data();
        for (unsigned int i = 0u; i < numDetections; i++)
        {
            // NOTE: Order of coordinates in output data is yMin, xMin, yMax, xMax
            float yMin = *outputData++;
            float xMin = *outputData++;
            float yMax = *outputData++;
            float xMax = *outputData++;

            DetectedObject detectedObject(
                output2.at(i),
                BoundingBox(xMin, yMin, xMax, yMax),
                output3.at(i));

            detectedObjects.push_back(detectedObject);
        }

        std::sort(detectedObjects.begin(), detectedObjects.end());
        std::sort(m_DetectedObjects.begin(), m_DetectedObjects.end());

        // Compare detected objects with expected results
        std::vector<DetectedObject>::const_iterator it = detectedObjects.begin();
        for (unsigned int i = 0; i < numDetections; i++)
        {
            if (it == detectedObjects.end())
            {
                BOOST_LOG_TRIVIAL(error) << "No more detected objects found! Index out of bounds: " << i;
                return TestCaseResult::Abort;
            }

            const DetectedObject& detectedObject = *it;
            const DetectedObject& expectedObject = m_DetectedObjects[i];

            if (detectedObject.m_Class != expectedObject.m_Class)
            {
                BOOST_LOG_TRIVIAL(error) << "Prediction for test case " << this->GetTestCaseId() <<
                    " is incorrect: Expected (" << expectedObject.m_Class << ")" <<
                    " but predicted (" << detectedObject.m_Class << ")";
                return TestCaseResult::Failed;
            }

            if(!m_FloatComparer(detectedObject.m_Confidence, expectedObject.m_Confidence))
            {
                BOOST_LOG_TRIVIAL(error) << "Confidence of prediction for test case " << this->GetTestCaseId() <<
                    " is incorrect: Expected (" << expectedObject.m_Confidence << ")  +- 1.0 pc" <<
                    " but predicted (" << detectedObject.m_Confidence << ")";
                return TestCaseResult::Failed;
            }

            if (!m_FloatComparer(detectedObject.m_BoundingBox.m_XMin, expectedObject.m_BoundingBox.m_XMin) ||
                !m_FloatComparer(detectedObject.m_BoundingBox.m_YMin, expectedObject.m_BoundingBox.m_YMin) ||
                !m_FloatComparer(detectedObject.m_BoundingBox.m_XMax, expectedObject.m_BoundingBox.m_XMax) ||
                !m_FloatComparer(detectedObject.m_BoundingBox.m_YMax, expectedObject.m_BoundingBox.m_YMax))
            {
                BOOST_LOG_TRIVIAL(error) << "Detected bounding box for test case " << this->GetTestCaseId() <<
                    " is incorrect";
                return TestCaseResult::Failed;
            }

            ++it;
        }

        return TestCaseResult::Ok;
    }

private:
    static constexpr unsigned int k_Shape       = 10u;

    static constexpr unsigned int k_OutputSize1 = k_Shape * 4u;
    static constexpr unsigned int k_OutputSize2 = k_Shape;
    static constexpr unsigned int k_OutputSize3 = k_Shape;
    static constexpr unsigned int k_OutputSize4 = 1u;

    boost::math::fpc::close_at_tolerance<float> m_FloatComparer;
    std::vector<DetectedObject>                 m_DetectedObjects;
};

template <typename Model>
class MobileNetSsdTestCaseProvider : public IInferenceTestCaseProvider
{
public:
    template <typename TConstructModelCallable>
    explicit MobileNetSsdTestCaseProvider(TConstructModelCallable constructModel)
        : m_ConstructModel(constructModel)
    {}

    virtual void AddCommandLineOptions(boost::program_options::options_description& options) override
    {
        namespace po = boost::program_options;

        options.add_options()
            ("data-dir,d", po::value<std::string>(&m_DataDir)->required(),
             "Path to directory containing test data");

        Model::AddCommandLineOptions(options, m_ModelCommandLineOptions);
    }

    virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) override
    {
        if (!ValidateDirectory(m_DataDir))
        {
            return false;
        }

        m_Model = m_ConstructModel(commonOptions, m_ModelCommandLineOptions);
        if (!m_Model)
        {
            return false;
        }
        std::pair<float, int32_t> qParams = m_Model->GetInputQuantizationParams();
        m_Database = std::make_unique<MobileNetSsdDatabase>(m_DataDir.c_str(), qParams.first, qParams.second);
        if (!m_Database)
        {
            return false;
        }

        return true;
    }

    std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) override
    {
        std::unique_ptr<MobileNetSsdTestCaseData> testCaseData = m_Database->GetTestCaseData(testCaseId);
        if (!testCaseData)
        {
            return nullptr;
        }

        return std::make_unique<MobileNetSsdTestCase<Model>>(*m_Model, testCaseId, *testCaseData);
    }

private:
    typename Model::CommandLineOptions m_ModelCommandLineOptions;
    std::function<std::unique_ptr<Model>(const InferenceTestOptions &,
                                         typename Model::CommandLineOptions)> m_ConstructModel;
    std::unique_ptr<Model> m_Model;

    std::string m_DataDir;
    std::unique_ptr<MobileNetSsdDatabase> m_Database;
};

} // anonymous namespace

ext: hpp

max_stars_repo_name: VinayKarnam/armnn

max_issues_repo_name: VinayKarnam/armnn

max_forks_repo_name: VinayKarnam/armnn


text:
#pragma once
#include <boost/filesystem.hpp>

struct PathSingleton
{
    //absolute path to the folder the current binary is in
    static boost::filesystem::path binaryFolderPath;
};


text:
/*******************************************************************************
 *
 * MIT License
 *
 * Copyright (c) 2020 Advanced Micro Devices, Inc.
 *
 * 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 <miopen/ramdb.hpp>

#include <miopen/errors.hpp>
#include <miopen/lock_file.hpp>
#include <miopen/logger.hpp>
#include <miopen/md5.hpp>

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem.hpp>

#include <chrono>
#include <ctime>
#include <fstream>
#include <limits>
#include <map>
#include <mutex>
#include <sstream>

namespace miopen {

std::string RamDb::GetTimeFilePath(const std::string& path) { return path + ".time"; }

static ramdb_clock::time_point GetDbModificationTime(const std::string& path)
{
    const auto time_file_path = RamDb::GetTimeFilePath(path);
    auto file                 = std::ifstream{time_file_path};

    if(!file)
        // Zero time from epoch, considering file ancient if time can't be retrieved.
        return {};

    ramdb_clock::rep time;
    file >> time;
    return ramdb_clock::time_point{ramdb_clock::duration{time}};
}

static void UpdateDbModificationTime(const std::string& path)
{
    MIOPEN_LOG_I2("Updating db modification time for " << path);

    const auto time           = ramdb_clock::now().time_since_epoch();
    const auto time_file_path = RamDb::GetTimeFilePath(path);
    auto file                 = std::ofstream{time_file_path};

    if(!file)
    {
        MIOPEN_LOG_E("Cannot update database modification time: " + time_file_path);
        return;
    }

    file << time.count();
}

#define MIOPEN_VALIDATE_LOCK(lock)                       \
    do                                                   \
    {                                                    \
        if(!(lock))                                      \
            MIOPEN_THROW("Db lock has failed to lock."); \
    } while(false)

static std::chrono::seconds GetLockTimeout() { return std::chrono::seconds{60}; }

using exclusive_lock = std::unique_lock<LockFile>;

RamDb::RamDb(std::string path, bool is_system) : PlainTextDb(path, is_system) {}

RamDb& RamDb::GetCached(const std::string& path, bool is_system)
{
    // NOLINTNEXTLINE (cppcoreguidelines-avoid-non-const-global-variables)
    static std::mutex mutex;
    const std::lock_guard<std::mutex> lock{mutex};

    // NOLINTNEXTLINE (cppcoreguidelines-avoid-non-const-global-variables)
    static auto instances = std::map<std::string, RamDb*>{};
    const auto it         = instances.find(path);

    if(it != instances.end())
        return *it->second;

    // The ReadonlyRamDb objects allocated here by "new" shall be alive during
    // the calling app lifetime. Size of each is very small, and there couldn't
    // be many of them (max number is number of _different_ GPU board installed
    // in the user's system, which is _one_ for now). Therefore the total
    // footprint in heap is very small. That is why we can omit deletion of
    // these objects thus avoiding bothering with MP/MT syncronization.
    // These will be destroyed altogether with heap.
    auto instance = new RamDb{path, is_system};
    instances.emplace(path, instance);
    if(!DisableUserDbFileIO)
    {
        const auto prefetch_lock = exclusive_lock(instance->GetLockFile(), GetLockTimeout());
        MIOPEN_VALIDATE_LOCK(prefetch_lock);
        instance->Prefetch();
    }
    return *instance;
}

boost::optional<DbRecord> RamDb::FindRecord(const std::string& problem)
{
    const auto lock = exclusive_lock(GetLockFile(), GetLockTimeout());
    MIOPEN_VALIDATE_LOCK(lock);

    if(!ValidateUnsafe())
    {
        MIOPEN_LOG_I2("RamDb file is newer than cache, prefetching");
        Prefetch();
    }

    return FindRecordUnsafe(problem);
}

bool RamDb::StoreRecord(const DbRecord& record)
{
    const auto& key = record.GetKey();
    MIOPEN_LOG_I2("Trying to store record at key " << key << " in cache for file "
                                                   << GetFileName());
    const auto lock = exclusive_lock(GetLockFile(), GetLockTimeout());
    MIOPEN_VALIDATE_LOCK(lock);

    if(!DisableUserDbFileIO)
    {
        if(!StoreRecordUnsafe(record))
            return false;
        UpdateDbModificationTime(GetFileName());
    }

#if MIOPEN_DB_CACHE_WRITE_THROUGH
    UpdateCacheEntryUnsafe(record);
#else
    Prefetch();
#endif
    return true;
}

bool RamDb::UpdateRecord(DbRecord& record)
{
    const auto& key = record.GetKey();
    MIOPEN_LOG_I2("Trying to update record at key " << key << " in cache for file "
                                                    << GetFileName());
    const auto lock = exclusive_lock(GetLockFile(), GetLockTimeout());
    MIOPEN_VALIDATE_LOCK(lock);

    if(!DisableUserDbFileIO)
    {
        if(!UpdateRecordUnsafe(record))
            return false;
        UpdateDbModificationTime(GetFileName());
    }

#if MIOPEN_DB_CACHE_WRITE_THROUGH
    UpdateCacheEntryUnsafe(record);
#else
    Prefetch();
#endif
    return true;
}

bool RamDb::RemoveRecord(const std::string& key)
{
    MIOPEN_LOG_I2("Trying to remove record at key " << key << " from cache for file "
                                                    << GetFileName());
    const auto lock = exclusive_lock(GetLockFile(), GetLockTimeout());
    MIOPEN_VALIDATE_LOCK(lock);

#if MIOPEN_DB_CACHE_WRITE_THROUGH
    const auto is_valid = ValidateUnsafe();
#endif

    if(!DisableUserDbFileIO)
    {
        if(!RemoveRecordUnsafe(key))
            return false;
        UpdateDbModificationTime(GetFileName());
    }

#if MIOPEN_DB_CACHE_WRITE_THROUGH
    if(is_valid)
    {
        cache.erase(key);
        file_read_time = ramdb_clock::now();
    }
#else
    Prefetch();
#endif

    return true;
}

bool RamDb::Remove(const std::string& key, const std::string& id)
{
    MIOPEN_LOG_I2("Trying to remove value at key " << key << " and id " << id
                                                   << " from cache for file " << GetFileName());
    const auto lock = exclusive_lock(GetLockFile(), GetLockTimeout());
    MIOPEN_VALIDATE_LOCK(lock);

#if MIOPEN_DB_CACHE_WRITE_THROUGH
    const auto is_valid = ValidateUnsafe();
#endif

    auto record = FindRecordUnsafe(key);

    if(!record || !record->EraseValues(id))
        return false;

    if(!DisableUserDbFileIO)
    {
        if(!StoreRecordUnsafe(*record))
            return false;
        UpdateDbModificationTime(GetFileName());
    }

#if MIOPEN_DB_CACHE_WRITE_THROUGH
    if(is_valid)
    {
        if(record->GetSize() == 0)
        {
            cache.erase(key);
        }
        else
        {
            auto it = cache.find(key);
            auto ss = std::ostringstream{};
            record->WriteIdsAndValues(ss);
            it->second.content = ss.str();
        }

        file_read_time = ramdb_clock::now();
    }
#else
    Prefetch();
#endif

    return true;
}

boost::optional<miopen::DbRecord> RamDb::FindRecordUnsafe(const std::string& problem)
{
    MIOPEN_LOG_I2("Looking for key " << problem << " in cache for file " << GetFileName());
    const auto it = cache.find(problem);

    if(it == cache.end())
        return boost::none;

    auto record = DbRecord{problem};

    if(!record.ParseContents(it->second.content))
    {
        MIOPEN_LOG_E("Error parsing payload under the key: "
                     << problem << " form file " << GetFileName() << "#" << it->second.line);
        MIOPEN_LOG_E("Contents: " << it->second.content);
        return boost::none;
    }

    return record;
}

template <class TFunc>
static void Measure(const std::string& funcName, TFunc&& func)
{
    if(!miopen::IsLogging(LoggingLevel::Info))
        func();

    const auto start = std::chrono::high_resolution_clock::now();
    func();
    const auto end = std::chrono::high_resolution_clock::now();
    MIOPEN_LOG_I("RamDb::" << funcName << " time: " << (end - start).count() * .000001f << " ms");
}

bool RamDb::ValidateUnsafe()
{
    if(DisableUserDbFileIO)
        return true;
    if(!boost::filesystem::exists(GetFileName()))
        return cache.empty();
    const auto file_mod_time     = GetDbModificationTime(GetFileName());
    const auto validation_result = file_mod_time < file_read_time;
    MIOPEN_LOG_I2("DB file is " << (validation_result ? "older" : "newer")
                                << " than cache: " << file_mod_time.time_since_epoch().count()
                                << ", " << file_read_time.time_since_epoch().count());
    return validation_result;
}

void RamDb::Prefetch()
{
    if(DisableUserDbFileIO)
        MIOPEN_THROW("Prefetch should never happen with disabled File IO");

    Measure("Prefetch", [this]() {
        auto file = std::ifstream{GetFileName()};

        if(!file)
        {
            const auto log_level =
                IsWarningIfUnreadable() ? LoggingLevel::Warning : LoggingLevel::Info;
            MIOPEN_LOG(log_level, "File is unreadable: " << GetFileName());
            return;
        }

        cache.clear();
        auto line   = std::string{};
        auto n_line = 0;

        while(std::getline(file, line))
        {
            ++n_line;

            if(line.empty())
                continue;

            const auto key_size = line.find('=');
            const bool is_key   = (key_size != std::string::npos && key_size != 0);

            if(!is_key)
            {
                MIOPEN_LOG_E("Ill-formed record: key not found: " << GetFileName() << "#"
                                                                  << n_line);
                continue;
            }

            const auto key      = line.substr(0, key_size);
            const auto contents = line.substr(key_size + 1);

            cache.emplace(key, CacheItem{n_line, contents});
        }

        file_read_time = ramdb_clock::now();
    });
}

#if MIOPEN_DB_CACHE_WRITE_THROUGH
void RamDb::UpdateCacheEntryUnsafe(const DbRecord& record)
{
    const auto is_valid = ValidateUnsafe();

    if(!DisableUserDbFileIO)
        UpdateDbModificationTime(GetFileName());

    if(is_valid)
    {
        const auto& key = record.GetKey();
        const auto it   = cache.find(key);
        auto ss         = std::ostringstream{};
        record.WriteIdsAndValues(ss);

        if(it != cache.end())
        {
            auto& item   = it->second;
            item.content = ss.str();
        }
        else
        {
            cache.emplace(key, CacheItem{-1, ss.str()});
        }
        file_read_time = ramdb_clock::now();
    }
}
#endif

} // namespace miopen


text:
//---------------------------------------------------------------------------//
//!
//! \file   MonteCarlo_CellPulseHeightEstimator.hpp
//! \author Alex Robinson
//! \brief  Cell pulse height estimator class declaration
//!
//---------------------------------------------------------------------------//

#ifndef MONTE_CARLO_CELL_PULSE_HEIGHT_ESTIMATOR_HPP
#define MONTE_CARLO_CELL_PULSE_HEIGHT_ESTIMATOR_HPP

// Boost Includes
#include <boost/mpl/vector.hpp>

// FRENSIE Includes
#include "MonteCarlo_EntityEstimator.hpp"
#include "MonteCarlo_EstimatorContributionMultiplierPolicy.hpp"
#include "MonteCarlo_ParticleEnteringCellEventObserver.hpp"
#include "MonteCarlo_ParticleLeavingCellEventObserver.hpp"
#include "Geometry_Model.hpp"
#include "Utility_Map.hpp"
#include "Utility_Set.hpp"
#include "Utility_Vector.hpp"
#include "Utility_Tuple.hpp"

namespace MonteCarlo{

/*! The pulse height entity estimator class
 * \ingroup particle_entering_cell_event
 * \ingroup particle_leaving_cell_event
 * \details This class has been set up to get correct results with multiple
 * threads. However, the commitHistoryContribution member function call
 * should only appear within an omp critical block. Use the enable thread
 * support member function to set up an instance of this class for the
 * requested number of threads. The classes default initialization is for
 * a single thread.
 */
template<typename ContributionMultiplierPolicy = WeightMultiplier>
class CellPulseHeightEstimator : public EntityEstimator,
                                 public ParticleEnteringCellEventObserver,
                                 public ParticleLeavingCellEventObserver
{
  // Typedef for the serial update tracker
  typedef std::pair<double,std::unordered_map<Geometry::Model::EntityId,std::tuple<double,double> > >
  SerialUpdateTracker;

  // Typedef for the parallel update tracker
  typedef std::vector<SerialUpdateTracker> ParallelUpdateTracker;

public:

  //! Typedef for the cell id type
  typedef Geometry::Model::EntityId CellIdType;

  //! Typedef for event tags used for quick dispatcher registering
  typedef boost::mpl::vector<ParticleEnteringCellEventObserver::EventTag,
                             ParticleLeavingCellEventObserver::EventTag>
  EventTags;

  //! Constructor
  CellPulseHeightEstimator( const Id id,
                            const double multiplier,
                            const std::vector<CellIdType>& entity_ids );

  //! Destructor
  ~CellPulseHeightEstimator()
  { /* ... */ }

  //! Check if the estimator is a cell estimator
  bool isCellEstimator() const final override;

  //! Check if the estimator is a surface estimator
  bool isSurfaceEstimator() const final override;

  //! Check if the estimator is a mesh estimator
  bool isMeshEstimator() const final override;

  //! Add current history estimator contribution
  void updateFromParticleEnteringCellEvent(
                               const ParticleState& particle,
                               const CellIdType cell_entering ) final override;

  //! Add current history estimator contribution
  void updateFromParticleLeavingCellEvent(
                                const ParticleState& particle,
                                const CellIdType cell_leaving ) final override;

  //! Commit the contribution from the current history to the estimator
  void commitHistoryContribution() final override;

  //! Print the estimator data summary
  void printSummary( std::ostream& os ) const final override;

  //! Enable support for multiple threads
  void enableThreadSupport( const unsigned num_threads ) final override;

  //! Reset the estimator data
  void resetData() final override;

private:

  // Default constructor
  CellPulseHeightEstimator();

  // Assign discretization to an estimator dimension
  void assignDiscretization( const std::shared_ptr<const ObserverPhaseSpaceDimensionDiscretization>& bins,
                             const bool range_dimension ) final override;

  // Assign response function to the estimator
  void assignResponseFunction( const std::shared_ptr<const ParticleResponse>& response_function ) final override;

  // Assign the particle type to the estimator
  void assignParticleType( const ParticleType particle_type ) final override;

  // Check and correct a positron contribution
  static void checkAndCorrectPositronEnergyContribution(
                                                 const ParticleState& particle,
                                                 double& energy_contribution );

  // Calculate the estimator contribution from the entire history
  static double calculateHistoryContribution( const double energy_deposition,
                                              const double charge_deposition,
                                              const double source_weight,
                                              WeightMultiplier );

  // Calculate the estimator contribution from the entire history
  static double calculateHistoryContribution( const double energy_deposition,
                                              const double charge_deposition,
                                              const double source_weight,
                                              WeightAndEnergyMultiplier );

  // Calculate the estimator contribution from the entire history
  static double calculateHistoryContribution( const double energy_deposition,
                                              const double charge_deposition,
                                              const double source_weight,
                                              WeightAndChargeMultiplier );

  // Add info to update tracker
  void addInfoToUpdateTracker( const unsigned thread_id,
                               const CellIdType cell_id,
                               const double source_weight,
                               const double energy_contribution,
                               const double charge_contribution );

  // Get the entity iterators from the update tracker
  void getCellIteratorFromUpdateTracker(
                const unsigned thread_id,
                typename Utility::TupleElement<1,SerialUpdateTracker>::type::const_iterator& start_cell,
                typename Utility::TupleElement<1,SerialUpdateTracker>::type::const_iterator& end_cell ) const;

  // Reset the update tracker
  void resetUpdateTracker( const unsigned thread_id );

  // Save the data to an archive
  template<typename Archive>
  void save( Archive& ar, const unsigned version ) const;

  // Load the data from an archive
  template<typename Archive>
  void load( Archive& ar, const unsigned version );

  BOOST_SERIALIZATION_SPLIT_MEMBER();

  // Declare the boost serialization access object as a friend
  friend class boost::serialization::access;

  // The entities that have been updated
  ParallelUpdateTracker d_update_tracker;

  // The generic particle state map (avoids having to make a new map for cont.)
  std::vector<Estimator::DimensionValueMap> d_dimension_values;
};

//! The weight multiplied cell pulse height estimator
typedef CellPulseHeightEstimator<WeightMultiplier> WeightMultipliedCellPulseHeightEstimator;

//! The weight and energy multiplied cell pulse height estimator
typedef CellPulseHeightEstimator<WeightAndEnergyMultiplier> WeightAndEnergyMultipliedCellPulseHeightEstimator;

//! The weight and charge multiplied cell pulse height estimator
typedef CellPulseHeightEstimator<WeightAndChargeMultiplier> WeightAndChargeMultipliedCellPulseHeightEstimator;

} // end MonteCarlo namespace

BOOST_SERIALIZATION_CLASS1_VERSION( CellPulseHeightEstimator, MonteCarlo, 0 );

//---------------------------------------------------------------------------//
// Template Includes
//---------------------------------------------------------------------------//

#include "MonteCarlo_CellPulseHeightEstimator_def.hpp"

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

#endif // end MONTE_CARLO_CELL_PULSE_HEIGHT_ESTIMATOR_HPP

//---------------------------------------------------------------------------//
// end MonteCarlo_CellPulseHeightEstimator.hpp
//---------------------------------------------------------------------------//


text:
/*
Brian Staber (brian.staber@gmail.com)
*/

#ifndef SHINOZUKAPP_LAYEREDCOMP_HPP
#define SHINOZUKAPP_LAYEREDCOMP_HPP

#include "meshpp.hpp"
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_real_distribution.hpp>
#include <boost/math/special_functions/erf.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/math/special_functions/gamma.hpp>
#include <boost/math/special_functions/beta.hpp>

class shinozuka_layeredcomp
{
public:

    shinozuka_layeredcomp();
    shinozuka_layeredcomp(int & nu, double & L1, double & L2, double & L3);
    ~shinozuka_layeredcomp();

    template<typename typearg>
    double tau_beta(typearg & beta);
    double s_tau(double & tau);

    void generator_gauss_points(Epetra_SerialDenseVector & v, mesh & Mesh, std::vector<int> & phase);
    void generator_one_gauss_point(Epetra_SerialDenseVector & v, mesh & Mesh, std::vector<int> & phase, double & xi, double & eta, double & zeta);

    void icdf_gamma(Epetra_Vector & V, Epetra_Vector & G, double & alpha, double & beta);
    void icdf_beta(Epetra_Vector & V, Epetra_Vector & B, double & tau1, double & tau2);

    int order;
    double l1;
    double l2;
    double l3;
    boost::random::mt19937 rng;
    boost::random::uniform_real_distribution<> phi_;
    boost::random::uniform_real_distribution<> psi_;
};
#endif

max_stars_repo_name: bstaber/Trilinos

max_issues_repo_name: bstaber/Trilinos

max_forks_repo_name: bstaber/Trilinos


text:
#pragma once

#ifndef LIB_INCLUDE_AT_READ_HPP_
#define LIB_INCLUDE_AT_READ_HPP_

#include "parser.hpp"

#include "logging.hpp"

#include "spdlog/spdlog.h"

#include <boost/asio/read_until.hpp>
#include <boost/throw_exception.hpp>
#include <boost/system/system_error.hpp>

#include <cstddef>
#include <string>
#include <iterator>
#include <algorithm>
#include <utility>
#include <type_traits>

namespace Ringbeller
{

namespace detail
{

template<
    typename DynamicBuffer,
    typename Body>
void populate_response_from_buffer(
    DynamicBuffer const & buffer,
    response<Body> & msg)
{
    auto b = buffer.data();

    response<string_body> placeholder;

    parse_response(
        boost::asio::buffers_begin(b),
        boost::asio::buffers_end(b),
        placeholder);

    std::visit([&](auto && p)
    {
        using T = std::decay_t<decltype (p)>;

        if constexpr (std::is_same_v<T, Ringbeller::prompt>)
        {
            msg = p;
        }
        else if constexpr (std::is_same_v<T, Ringbeller::final_result<string_body>>)
        {
            Ringbeller::final_result<Body> fr(p.rc);

            std::copy(p.text.cbegin(), p.text.cend(), std::back_inserter(fr.text));

            msg = fr;
        }
    }, placeholder);
//    result_code rc(result_code::none);
//    std::string rc_text;
//    std::vector<std::string> body;

//    parse_response(
//        boost::asio::buffers_begin(b),
//        boost::asio::buffers_end(b),
//        rc,
//        rc_text,
//        body);

//    msg.rc = rc;
//
//    std::copy(rc_text.cbegin(), rc_text.cend(), std::back_inserter(msg.rc_text));

//    for (auto const & ll : body)
//    {
//        auto & seq = msg.body;
//        seq.emplace(seq.end(), ll.cbegin(), ll.cend());
//    }
}

}

/**
 *  Read a complete message from a stream.
 *
 *  This function is used to read a complete message from a stream.
 *  The call will block until one of the following conditions is true:
 *
 *  @li The entire message is read.
 *
 *  @li An error occurs.
 *
 *  @param stream The stream from which the data is to be read.
 *  The type must support the @b SyncReadStream concept.
 *
 *  @param buffer A @b DynamicBuffer holding additional bytes
 *  read by the implementation from the stream. This is both
 *  an input and an output parameter; on entry, any data in the
 *  dynamic buffer's input sequence will be given to the parser
 *  first.
 *
 *  @param msg An object in which to store the message contents.
 *  This object should not have previous contents, otherwise
 *  the behavior is undefined.
 *
 *  @param ec Set to the error, if any occurred.
 *
 *  @return The number of bytes transferred to the parser.
 */
template<
    typename SyncReadStream,
    typename DynamicBuffer,
    typename Body>
std::size_t
read(SyncReadStream & stream, DynamicBuffer & buffer, response<Body> & msg, boost::system::error_code & ec)
{
    FN_ENTER();

    std::size_t bytes_read = boost::asio::read_until(stream, buffer, detail::match_condition, ec);

    buffer.commit(bytes_read);

    LOG_CHECK_EC(ec);

    if (not ec)
    {
        detail::populate_response_from_buffer(buffer, msg);
    }

    buffer.consume(bytes_read);

    FN_LEAVE();

    return bytes_read;
}

/**
 *  Read a complete message from a stream.
 *
 *  This function is used to read a complete message from a stream.
 *  The call will block until one of the following conditions is true:
 *
 *  @li The entire message is read.
 *
 *  @li An error occurs.
 *
 *  @param stream The stream from which the data is to be read.
 *  The type must support the @b SyncReadStream concept.
 *
 *  @param buffer A @b DynamicBuffer holding additional bytes
 *  read by the implementation from the stream. This is both
 *  an input and an output parameter; on entry, any data in the
 *  dynamic buffer's input sequence will be given to the parser
 *  first.
 *
 *  @param msg An object in which to store the message contents.
 *  This object should not have previous contents, otherwise
 *  the behavior is undefined.
 *
 *  @return The number of bytes transferred to the parser.
 *
 *  @throws system_error Thrown on failure.
 */
//template<
//    typename SyncReadStream,
//    typename DynamicBuffer,
//    typename Body,
//    typename Sequence>
//std::size_t
//read(SyncReadStream & stream, DynamicBuffer & buffer, response<Body, Sequence> & msg)
//{
//    FN_ENTER();
//
//    ec_t ec;
//
//    auto const bytes_transferred = read(stream, buffer, msg, ec);
//
//    if (ec)
//    {
//        LOG_EXCEPTION(ec);
//        BOOST_THROW_EXCEPTION(boost::system::system_error{ec});
//    }
//
//    FN_LEAVE();
//
//    return bytes_transferred;
//}

//template<
//    typename AsyncReadStream,
//    typename DynamicBuffer,
//    typename Body,
//    typename Sequence,
//    typename ReadHandler>
//BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
//    void(boost::system::error_code, std::size_t))
//async_read(
//    AsyncReadStream & stream,
//    DynamicBuffer & buffer,
//    response<Body, Sequence> & msg,
//    ReadHandler && handler)
//{
//    FN_ENTER();
//
//    boost::asio::async_read_until(stream, buffer, detail::match_condition,
//        [handler{std::move(handler)}, &buffer, &msg](boost::system::error_code const & ec, std::size_t rb)
//        {
//            FN_ENTER();
//
//            LOG_CHECK_EC(ec);
//
//            buffer.commit(rb);
//
//            if (not ec)
//            {
//                detail::populate_response_from_buffer(buffer, msg);
//            }
//
//            // call user handler
//            handler(ec, rb);
//
//            buffer.consume(rb);
//
//            FN_LEAVE();
//        }
//    );
//
//    FN_LEAVE();
//}

}  // namespace Ringbeller

#endif /* LIB_INCLUDE_AT_READ_HPP_ */

max_stars_repo_name: WojciechMigda/TCO-Ringbeller

max_issues_repo_name: WojciechMigda/TCO-Ringbeller

max_forks_repo_name: WojciechMigda/TCO-Ringbeller


text:
#include <boost/test/unit_test.hpp>

#include "util/interval.hpp"

BOOST_AUTO_TEST_SUITE(interval)

BOOST_AUTO_TEST_CASE(mix_sample_simple) {
  hisui::util::Interval i(10, 15);

  BOOST_REQUIRE(!i.isIn(0));
  BOOST_REQUIRE(!i.isIn(9));
  BOOST_REQUIRE(i.isIn(10));
  BOOST_REQUIRE(i.isIn(14));
  BOOST_REQUIRE(!i.isIn(15));

  BOOST_REQUIRE_EQUAL(0, i.getSubstructLower(10));
  BOOST_REQUIRE_EQUAL(4, i.getSubstructLower(14));
}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: kounoike/hisui

max_issues_repo_name: kounoike/hisui

max_forks_repo_name: kounoike/hisui


text:
/*
 *  Copyright (c) 2014-present, Facebook, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  LICENSE file in the root directory of this source tree. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 *
 */

#include <unistd.h>

#include <map>
#include <string>

#include <boost/algorithm/string/find.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/regex.hpp>
#include <boost/xpressive/xpressive.hpp>

#include <osquery/core/conversions.h>
#include <osquery/filesystem.h>
#include <osquery/sql.h>
#include <osquery/system.h>
#include <osquery/tables.h>

namespace xp = boost::xpressive;

namespace osquery {
namespace tables {

QueryData genOSVersion(QueryContext& context) {
  static const std::string kSysctlName = "kern.osrelease";

  auto result =
      SQL::selectAllFrom("system_controls", "name", EQUALS, kSysctlName);

  Row r;

  r["name"] = "FreeBSD";
  r["platform"] = "freebsd";
  r["version"] = result[0]["current_value"];

  auto rx = xp::sregex::compile(
      "(?P<major>[0-9]+)\\.(?P<minor>[0-9]+)-(?P<build>\\w+)-?p?(?P<patch>[0-9]+)"
      "?");

  xp::smatch matches;
  for (auto& line : osquery::split(result[0]["current_value"], "\n")) {
    if (xp::regex_search(line, matches, rx)) {
      r["major"] = INTEGER(matches["major"]);
      r["minor"] = INTEGER(matches["minor"]);
      r["build"] = matches["build"];
      r["patch"] = matches["patch"];
      break;
    }
  }

  return {r};
}

QueryData genSystemInfo(QueryContext& context) {
  Row r;
  r["hostname"] = osquery::getHostname();
  r["computer_name"] = r["hostname"];

  std::string uuid;
  r["uuid"] = (osquery::getHostUUID(uuid)) ? uuid : "";

  auto qd = SQL::selectAllFrom("cpuid");
  for (const auto& row : qd) {
    if (row.at("feature") == "product_name") {
      r["cpu_brand"] = row.at("value");
      boost::trim(r["cpu_brand"]);
    }
  }

  static long cores = sysconf(_SC_NPROCESSORS_CONF);
  if (cores > 0) {
    r["cpu_logical_cores"] = INTEGER(cores);
    r["cpu_physical_cores"] = INTEGER(cores);
  } else {
    r["cpu_logical_cores"] = "-1";
    r["cpu_physical_cores"] = "-1";
  }

  static long pages = sysconf(_SC_PHYS_PAGES);
  static long pagesize = sysconf(_SC_PAGESIZE);

  if (pages > 0 && pagesize > 0) {
    r["physical_memory"] = BIGINT((long long)pages * (long long)pagesize);
  } else {
    r["physical_memory"] = "-1";
  }

  r["cpu_type"] = "0";
  r["cpu_subtype"] = "0";

  return {r};
}

QueryData genPlatformInfo(QueryContext& context) {
  return QueryData();
}
}
}

max_stars_repo_name: msekletar/osquery

max_issues_repo_name: msekletar/osquery


text:
/*
 * Copyright (c) 2010-2012 Steffen Kieß
 *
 * 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.
 */

#ifndef HDF5_SERIALIZATION_HPP_INCLUDED
#define HDF5_SERIALIZATION_HPP_INCLUDED

#include "Serialization.forward.hpp"

// Code for HDF5 serialization. Unlike the code in Matlab.hpp the resulting
// files cannot be read by matlab. Not really tested / used currently.

#include <Core/Assert.hpp>
#include <HDF5/Type.hpp>
#include <HDF5/File.hpp>
#include <HDF5/Group.hpp>
#include <HDF5/DataSet.hpp>
#include <HDF5/SerializationKey.hpp>

#include <map>
#include <set>
#include <sstream>
#include <vector>

#include <stdint.h>

#include <boost/any.hpp>

namespace HDF5 {
  class SerializationContextHandle;

  template <typename T> struct Serializer;
  template <typename T> struct Serializer {
    static inline void h5Save (const SerializationContextHandle& handle, const T& t) {
      t.h5Save (handle);
    }
    static inline void h5Load (DeserializationContext& context, ObjectReference name, HDF5::DataSet& dataSet) {
      T::h5Load (context, name, dataSet);
    }
  };

  class SerializationContextHandle {
    SerializationContext& context_;
    SerializationKey key_;

  public:
    SerializationContextHandle (SerializationContext& context, const SerializationKey& key) : context_ (context), key_ (key) {}

    SerializationContext& context () const {
      return context_;
    }

    const SerializationKey& key () const {
      return key_;
    }

    HDF5::DataSet createDataSet (const HDF5::DataType& data_type, const HDF5::DataSpace& data_space = HDF5::DataSpace::create (H5S_SCALAR), const DataSetCreatePropList& dcpl = DataSetCreatePropList ()) const;
  };

  class SerializationContext {
    const HDF5::File file;
    HDF5::Group refGroup;
    std::set<SerializationKey> inProgress;
    std::map<SerializationKey, ObjectReference> references;
    uint64_t id;

    friend class SerializationContextHandle;

    class InProgressInserter {
      std::set<SerializationKey>& inProgress;
      SerializationKey value;

    public:
      InProgressInserter (std::set<SerializationKey>& inProgress, SerializationKey value) : inProgress (inProgress), value (value) {
        ASSERT (inProgress.count (value) == 0);
        inProgress.insert (value);
      }

      ~InProgressInserter () {
        ASSERT (inProgress.count (value) == 1);
        inProgress.erase (value);
      }
    };

    const char* groupName() {
      return "#refs#";
    }

    const HDF5::Group& getRefGroup () {
      if (!refGroup.isValid ()) {
        if (!file.rootGroup ().exists (groupName ())) {
          HDF5::Group group = HDF5::Group::create (file);
          file.rootGroup ().link (groupName (), group);
          refGroup = group;
        } else {
          refGroup = (Group) file.rootGroup ().open (groupName (), setEFilePrefix ());
        }
      }
      return refGroup;
    }

  public:
    SerializationContext (const HDF5::File& file);
    ~SerializationContext ();

    void add (const SerializationKey& key, const ObjectReference& ref) {
      ASSERT (references.count (key) == 0);
      references[key] = ref;
    }

    std::string newName () {
      ASSERT (id + 1 != 0);
      uint64_t myId = ++id;
      std::stringstream str;
      str << "object-" << myId;
      return str.str ();
    }

    template <typename T>
    ObjectReference write (const std::vector<T>& vector) {
      HDF5::DataType type = *getH5Type<T> ();
      size_t count = vector.size ();
      HDF5::DataSpace space = HDF5::DataSpace::createSimple (count);
      std::string name = newName ();
      HDF5::DataSet dataset = HDF5::DataSet::create (file, type, space, DataSetCreatePropList (), setEFilePrefix ());
      getRefGroup ().link (name, dataset);
      dataset.write (vector.data (), type);
      return dataset.reference ();
    }

    template <typename T>
    ObjectReference get (const T& object) {
      SerializationKey addr = SerializationKey::create (object);
      if (references.count (addr)) {
        return references[addr];
      } else {
        {
          InProgressInserter ipi (inProgress, addr);
          Serializer<T>::h5Save (SerializationContextHandle (*this, addr), object);
        }
        ASSERT (references.count (addr));
        return references[addr];
      }
    }
  };

  class DeserializationContext {
    const HDF5::File file;
    std::set<ObjectReference> inProgress;
    std::map<ObjectReference, boost::any> objects;

  public:
    DeserializationContext (const HDF5::File& file);
    ~DeserializationContext ();

    template <typename T>
    void registerValue (ObjectReference name, std::shared_ptr<T> ptr) {
      ASSERT (!objects.count (name));
      ASSERT (inProgress.count (name));
      objects[name] = boost::any (ptr);
      inProgress.erase (name);
    }

    template <typename T>
    std::shared_ptr<T> resolve (ObjectReference name) {
      if (objects.count (name))
        return boost::any_cast<std::shared_ptr<T> > (objects[name]);
      ASSERT (!inProgress.count (name));
      inProgress.insert (name);
      HDF5::DataSet dataSet = (DataSet) name.dereference (file);
      Serializer<T>::h5Load (*this, name, dataSet);
      ASSERT (!inProgress.count (name));
      return boost::any_cast<std::shared_ptr<T> > (objects[name]);
    }
  };

  template <typename T> inline void serialize (const FilenameType& outputFile, const std::string& name, const T& object) {
    ASSERT (name != "");
    HDF5::File file = HDF5::File::open (outputFile, H5F_ACC_RDWR | H5F_ACC_CREAT | H5F_ACC_TRUNC);
    SerializationContext context (file);
    file.rootGroup ().link (name, context.get (object).dereference (file));
  }

  template <typename T> inline std::shared_ptr<T> deserialize (const FilenameType& outputFile, const std::string& name) {
    ASSERT (name != "");
    HDF5::File file = HDF5::File::open (outputFile, H5F_ACC_RDONLY);
    DeserializationContext context (file);
    return context.resolve<T> (file.rootGroup ().open (name, setEFilePrefix ()).reference ());
  }

  inline bool isHdf5 (const FilenameType& file) {
    return HDF5::File::isHDF5 (file);
  }
}

#endif // !HDF5_SERIALIZATION_HPP_INCLUDED

max_stars_repo_name: voxie-viewer/voxie

max_issues_repo_name: voxie-viewer/voxie

max_forks_repo_name: voxie-viewer/voxie


text:
/*
 * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 * 1. Any modified source or binaries are used only with the BitShares network.
 *
 * 2. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *
 * 3. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#pragma once

#include <string>

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>

#include <fc/exception/exception.hpp>

namespace graphene { namespace wallet {

   struct method_description
   {
      std::string method_name;
      std::string brief_description;
      std::string detailed_description;
   };

   class api_documentation
   {
      typedef boost::multi_index::multi_index_container<method_description,
         boost::multi_index::indexed_by<
            boost::multi_index::ordered_unique<
               boost::multi_index::member<method_description, std::string, &method_description::method_name> > > > method_description_set;
      method_description_set method_descriptions;
   public:
      api_documentation();
      std::string get_brief_description(const std::string& method_name) const
      {
         auto iter = method_descriptions.find(method_name);
         if (iter != method_descriptions.end())
            return iter->brief_description;
         else
            FC_THROW_EXCEPTION(fc::key_not_found_exception, "No entry for method ${name}", ("name", method_name));
      }
      std::string get_detailed_description(const std::string& method_name) const
      {
         auto iter = method_descriptions.find(method_name);
         if (iter != method_descriptions.end())
            return iter->detailed_description;
         else
            FC_THROW_EXCEPTION(fc::key_not_found_exception, "No entry for method ${name}", ("name", method_name));
      }
      std::vector<std::string> get_method_names() const
      {
         std::vector<std::string> method_names;
         for (const method_description& method_description: method_descriptions)
            method_names.emplace_back(method_description.method_name);
         return method_names;
      }
   };

} } // end namespace graphene::wallet


text:
/*
Copyright 2019 Peter Asanov

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 "ts_parser.h"
#include "log_utils.h"
#include "utils.hpp"

#include <boost/endian/conversion.hpp>
#include <boost/log/trivial.hpp>

namespace mpegts
{
namespace detail
{
  namespace
  {
    bool do_checks(const ts_packet_t &ts_packet)
    {
      if (ts_packet.sync_byte != 0x47)
      {
        BOOST_LOG_TRIVIAL(trace) << "TS packet sync byte is invalid (expected 0x47), skipping";
        return false;
      }

      if (ts_packet.transport_error)
      {
        BOOST_LOG_TRIVIAL(trace) << "TS packet is corrupt, skipping";
        return {};
      }

      if (ts_packet.adaptation_field_ctl == 0x00 || ts_packet.adaptation_field_ctl == 0x02)
      {
        BOOST_LOG_TRIVIAL(trace) << "TS packet has no payload, skipping";
        return {};
      }

      if (!((ts_packet.pid >= 0x20 && ts_packet.pid <= 0x1FFA) ||
              (ts_packet.pid >= 0x1FFC && ts_packet.pid <= 0x1FFE)))
      {
        BOOST_LOG_TRIVIAL(trace) << "TS packet PID is outside of tables or PES range, skipping";
        return {};
      }

      return true;
    }
  } // namespace

  void ts_parser::handle_continuity_cnt(const ts_packet_t &ts_packet)
  {
    const auto it = _pid_to_continuity_cnt.find(ts_packet.pid);
    if (_pid_to_continuity_cnt.find(ts_packet.pid) != _pid_to_continuity_cnt.end())
    {
      if (ts_packet.continuity_cnt - it->second != 1 && it->second != 15 &&
          ts_packet.continuity_cnt != 0)
      {
        BOOST_LOG_TRIVIAL(warning)
            << "TS packet loss detected, PID: " << utils::num_to_hex(ts_packet.pid, true);
      }
    }

    _pid_to_continuity_cnt[ts_packet.pid] = ts_packet.continuity_cnt;
  }

  ts_packet_opt ts_parser::parse(ts_packet_t ts_packet)
  {
    // converting to big endian because using big endian masks from the documentation:
    // https://en.wikipedia.org/wiki/MPEG_transport_stream#Important_elements_of_a_transport_stream

    auto header = boost::endian::native_to_big(ts_packet.header);

    ts_packet.sync_byte = (header & 0xff000000) >> 24;
    ts_packet.transport_error = (header & 0x800000);
    ts_packet.continuity_cnt = (header & 0xf);
    ts_packet.pusi = static_cast<bool>(header & 0x400000);
    ts_packet.pid = (header & 0x1fff00) >> 8;
    ts_packet.adaptation_field_ctl = (header & 0x30) >> 4;

    if (!do_checks(ts_packet))
    {
      log_utils::log_ts_packet(ts_packet, _ts_packet_num++);
      return {};
    }

    if (ts_packet.adaptation_field_ctl == 0x3)
    {
      uint8_t adaptaion_field_len = ts_packet.data[0];
      ts_packet.pes_offset = sizeof(uint8_t) + adaptaion_field_len;
    }
    else
    {
      ts_packet.pes_offset = 0;
    }

    log_utils::log_ts_packet(ts_packet, _ts_packet_num++);

    handle_continuity_cnt(ts_packet);

    return ts_packet;
  }
} // namespace detail
} // namespace mpegts

max_stars_repo_name: leaxdc/mpeg-ts-split

max_issues_repo_name: leaxdc/mpeg-ts-split

max_forks_repo_name: leaxdc/mpeg-ts-split