angularOscillatingDisplacementPointPatchVectorField::
angularOscillatingDisplacementPointPatchVectorField
(
    const pointPatch& p,
    const DimensionedField<vector, pointMesh>& iF,
    const dictionary& dict
)
:
    fixedValuePointPatchVectorField(p, iF, dict),
    axis_(dict.lookup("axis")),
    origin_(dict.lookup("origin")),
    angle0_(readScalar(dict.lookup("angle0"))),
    amplitude_(readScalar(dict.lookup("amplitude"))),
    omega_(readScalar(dict.lookup("omega")))
{
    if (!dict.found("value"))
    {
        updateCoeffs();
    }

    if (dict.found("p0"))
    {
        p0_ = vectorField("p0", dict , p.size());
    }
    else
    {
        p0_ = p.localPoints();
    }
}
void Foam::isentropicTotalTemperatureFvPatchScalarField::updateCoeffs
(
    const vectorField& Up
)
{
    updateCoeffs();
}
oscillatingVelocityPointPatchVectorField::
oscillatingVelocityPointPatchVectorField
(
    const pointPatch& p,
    const DimensionedField<vector, pointMesh>& iF,
    const dictionary& dict
)
:
    fixedValuePointPatchVectorField(p, iF, dict),
    amplitude_(dict.lookup("amplitude")),
    omega_(readScalar(dict.lookup("omega")))
{
    if (!dict.found("value"))
    {
        updateCoeffs();
    }

    if (dict.found("p0"))
    {
        p0_ = vectorField("p0", dict , p.size());
    }
    else
    {
        p0_ = p.localPoints();
    }
}
timeVaryingMappedFixedValueFvPatchField<Type>::
timeVaryingMappedFixedValueFvPatchField
(
    const fvPatch& p,
    const DimensionedField<Type, volMesh>& iF,
    const dictionary& dict
)
:
    fixedValueFvPatchField<Type>(p, iF),
    fieldTableName_(iF.name()),
    setAverage_(readBool(dict.lookup("setAverage"))),
    perturb_(dict.lookupOrDefault("perturb", 1E-5)),
    referenceCS_(NULL),
    nearestVertex_(0),
    nearestVertexWeight_(0),
    sampleTimes_(0),
    startSampleTime_(-1),
    startSampledValues_(0),
    startAverage_(pTraits<Type>::zero),
    endSampleTime_(-1),
    endSampledValues_(0),
    endAverage_(pTraits<Type>::zero)
{
    dict.readIfPresent("fieldTableName", fieldTableName_);

    if (dict.found("value"))
    {
        fvPatchField<Type>::operator==(Field<Type>("value", dict, p.size()));
    }
    else
    {
        updateCoeffs();
    }
}
engineTimeVaryingTotalPressureFvPatchScalarField::engineTimeVaryingTotalPressureFvPatchScalarField
(
    const fvPatch& p,
    const DimensionedField<scalar, volMesh>& iF,
    const dictionary& dict
)
:
    fixedValueFvPatchScalarField(p, iF),
    UName_(dict.lookup("U")),
    phiName_(dict.lookup("phi")),
    rhoName_(dict.lookup("rho")),
    psiName_(dict.lookup("psi")),
    gamma_(readScalar(dict.lookup("gamma"))),
    p0_("p0", dict, p.size()),
    timeDataFileName_(fileName(dict.lookup("timeDataFileName")).expand()),
    timeDataPtr_(NULL),
    engineDB_((refCast<const engineTime>(this->db().time())))
{
    if (dict.found("value"))
    {
        fvPatchField<scalar>::operator=
        (
            scalarField("value", dict, p.size())
        );
    }
    else
    {
        updateCoeffs();
        fvPatchField<scalar>::operator=(p0_);
    }
}
Foam::
timeVaryingMappedFixedValuePointPatchField<Type>::
timeVaryingMappedFixedValuePointPatchField
(
    const pointPatch& p,
    const DimensionedField<Type, pointMesh>& iF,
    const dictionary& dict
)
:
    fixedValuePointPatchField<Type>(p, iF),
    fieldTableName_(iF.name()),
    setAverage_(readBool(dict.lookup("setAverage"))),
    perturb_(dict.lookupOrDefault("perturb", 1E-5)),
    mapperPtr_(NULL),
    sampleTimes_(0),
    startSampleTime_(-1),
    startSampledValues_(0),
    startAverage_(pTraits<Type>::zero),
    endSampleTime_(-1),
    endSampledValues_(0),
    endAverage_(pTraits<Type>::zero)
{
    dict.readIfPresent("fieldTableName", fieldTableName_);
    updateCoeffs();
}
void Foam::totalPressureFvPatchScalarField::updateCoeffs()
{
    updateCoeffs
    (
        p0(),
        patch().lookupPatchField<volVectorField, vector>(UName())
    );
}
Ejemplo n.º 8
0
PeakController::PeakController( Model * _parent, 
		PeakControllerEffect * _peak_effect ) :
	Controller( Controller::PeakController, _parent, tr( "Peak Controller" ) ),
	m_peakEffect( _peak_effect ),
	m_currentSample( 0.0f )
{
	setSampleExact( true );
	if( m_peakEffect )
	{
		connect( m_peakEffect, SIGNAL( destroyed( ) ),
			this, SLOT( handleDestroyedEffect( ) ) );
	}
	connect( Engine::mixer(), SIGNAL( sampleRateChanged() ), this, SLOT( updateCoeffs() ) );
	connect( m_peakEffect->attackModel(), SIGNAL( dataChanged() ), this, SLOT( updateCoeffs() ) );
	connect( m_peakEffect->decayModel(), SIGNAL( dataChanged() ), this, SLOT( updateCoeffs() ) );
	m_coeffNeedsUpdate = true;
}
void wedgeFvPatchField<scalar>::evaluate(const Pstream::commsTypes)
{
    if (!updated())
    {
        updateCoeffs();
    }

    operator==(patchInternalField());
}
Ejemplo n.º 10
0
void Foam::fvPatchField<Type>::evaluate(const Pstream::commsTypes)
{
    if (!updated_)
    {
        updateCoeffs();
    }

    updated_ = false;
}
void Foam::basicSymmetryFvPatchField<Foam::scalar>::evaluate
(
    const Pstream::commsTypes
)
{
    if (!updated())
    {
        updateCoeffs();
    }

    scalargpuField::operator=(patchInternalField());
    transformFvPatchField<scalar>::evaluate();
}
void SRFTotalTemperatureFvPatchScalarField::updateCoeffs()
{
    const fvPatchVectorField& U =
        patch().lookupPatchField<volVectorField, vector>(UName_);

    // If relative, include the effect of the SRF
    if (relative_)
    {
        // Get reference to the SRF model
        const SRF::SRFModel& srf =
            db().lookupObject<SRF::SRFModel>("SRFProperties");

        // Determine patch velocity due to SRF
        const vectorField SRFSurfaceNormalVelocity =
            srf.velocity(patch().Cf());

        updateCoeffs(U - SRFSurfaceNormalVelocity);
    }
    else
    {
        updateCoeffs(U);
    }
}
Foam::translatingWallVelocityFvPatchVectorField::
translatingWallVelocityFvPatchVectorField
(
    const fvPatch& p,
    const DimensionedField<vector, volMesh>& iF,
    const dictionary& dict
)
:
    fixedValueFvPatchField<vector>(p, iF),
    U_(Function1<vector>::New("U", dict))
{
    // Evaluate the wall velocity
    updateCoeffs();
}
rotatingWallVelocityFvPatchVectorField::rotatingWallVelocityFvPatchVectorField
(
    const fvPatch& p,
    const DimensionedField<vector, volMesh>& iF,
    const dictionary& dict
)
:
    fixedValueFvPatchField<vector>(p, iF),
    origin_(dict.lookup("origin")),
    axis_(dict.lookup("axis")),
    omega_(readScalar(dict.lookup("omega")))
{
    // Evaluate the wall velocity
    updateCoeffs();
}
oscillatingDisplacementPointPatchVectorField::
oscillatingDisplacementPointPatchVectorField
(
    const pointPatch& p,
    const DimensionedField<vector, pointMesh>& iF,
    const dictionary& dict
)
:
    fixedValuePointPatchVectorField(p, iF, dict),
    amplitude_(dict.lookup("amplitude")),
    omega_(readScalar(dict.lookup("omega")))
{
    if (!dict.found("value"))
    {
        updateCoeffs();
    }
}
void Foam::fluxCorrectedVelocityFvPatchVectorField::evaluate
(
    const Pstream::commsTypes
)
{
    if (!updated())
    {
        updateCoeffs();
    }

    zeroGradientFvPatchVectorField::evaluate();

    const surfaceScalarField& phi =
        db().lookupObject<surfaceScalarField>(phiName_);

    const fvsPatchField<scalar>& phip =
        patch().patchField<surfaceScalarField, scalar>(phi);

    const vectorgpuField n(patch().nf());
    const gpuField<scalar>& magS = patch().magSf();

    if (phi.dimensions() == dimVelocity*dimArea)
    {
        operator==(*this - n*(n & *this) + n*phip/magS);
    }
    else if (phi.dimensions() == dimDensity*dimVelocity*dimArea)
    {
        const fvPatchField<scalar>& rhop =
            patch().lookupPatchField<volScalarField, scalar>(rhoName_);

        operator==(*this - n*(n & *this) + n*phip/(rhop*magS));
    }
    else
    {
        FatalErrorIn
        (
            "fluxCorrectedVelocityFvPatchVectorField::evaluate()"
        )
            << "dimensions of phi are incorrect\n"
            << "    on patch " << this->patch().name()
            << " of field " << this->dimensionedInternalField().name()
            << " in file " << this->dimensionedInternalField().objectPath()
            << exit(FatalError);
    }
}
Foam::waveDisplacementPointPatchVectorField::
waveDisplacementPointPatchVectorField
(
    const pointPatch& p,
    const DimensionedField<vector, pointMesh>& iF,
    const dictionary& dict
)
:
    fixedValuePointPatchField<vector>(p, iF, dict),
    amplitude_(dict.lookup("amplitude")),
    omega_(readScalar(dict.lookup("omega"))),
    waveNumber_(dict.lookupOrDefault<vector>("waveNumber", vector::zero))
{
    if (!dict.found("value"))
    {
        updateCoeffs();
    }
}
Ejemplo n.º 18
0
Foam::timeVaryingUniformFixedValueFvPatchField<Type>::
timeVaryingUniformFixedValueFvPatchField
(
    const fvPatch& p,
    const DimensionedField<Type, volMesh>& iF,
    const dictionary& dict
)
:
    fixedValueFvPatchField<Type>(p, iF),
    timeSeries_(dict)
{
   if (dict.found("value"))
   {
       fvPatchField<Type>::operator==(Field<Type>("value", dict, p.size()));
   }
   else
   {
       updateCoeffs();
   }
}
sixDoFRigidBodyDisplacementPointPatchVectorField::
sixDoFRigidBodyDisplacementPointPatchVectorField
(
    const pointPatch& p,
    const DimensionedField<vector, pointMesh>& iF,
    const dictionary& dict
)
:
    fixedValuePointPatchField<vector>(p, iF, dict),
    motion_(dict),
    rhoInf_(1.0),
    rhoName_(dict.lookupOrDefault<word>("rhoName", "rho")),
    lookupGravity_(-1),
    g_(vector::zero),
    relaxationFactor_(dict.lookupOrDefault<scalar>("relaxationFactor", 1))
{
    if (rhoName_ == "rhoInf")
    {
        rhoInf_ = readScalar(dict.lookup("rhoInf"));
    }

    if (dict.readIfPresent("g", g_))
    {
        lookupGravity_ = -2;
    }

    if (!dict.found("value"))
    {
        updateCoeffs();
    }

    if (dict.found("initialPoints"))
    {
        initialPoints_ = vectorField("initialPoints", dict , p.size());
    }
    else
    {
        initialPoints_ = p.localPoints();
    }
}
timeVaryingMappedFixedValueFvPatchField<Type>::
timeVaryingMappedFixedValueFvPatchField
(
    const fvPatch& p,
    const DimensionedField<Type, volMesh>& iF,
    const dictionary& dict
)
:
    fixedValueFvPatchField<Type>(p, iF),
    setAverage_(readBool(dict.lookup("setAverage"))),
    referenceCS_(NULL),
    nearestVertex_(0),
    nearestVertexWeight_(0),
    sampleTimes_(0),
    startSampleTime_(-1),
    startSampledValues_(0),
    startAverage_(pTraits<Type>::zero),
    endSampleTime_(-1),
    endSampledValues_(0),
    endAverage_(pTraits<Type>::zero)
{
    if (debug)
    {
        Pout<< "timeVaryingMappedFixedValue : construct from dictionary"
            << endl;
    }

    if (dict.found("value"))
    {
        fvPatchField<Type>::operator==(Field<Type>("value", dict, p.size()));
    }
    else
    {
        updateCoeffs();
    }
}
void engineTimeVaryingTotalPressureFvPatchScalarField::updateCoeffs()
{
    updateCoeffs(lookupPatchField<volVectorField, vector>(UName_));
}
int main (int argc, char* argv[])
{  
  SAMPLE sample;  /* training sample */
  LEARN_PARM learn_parm;
  KERNEL_PARM kernel_parm;
  STRUCT_LEARN_PARM struct_parm;
  STRUCTMODEL structmodel;
  int alg_type;

  svm_struct_learn_api_init(argc,argv);

  read_input_parameters(argc,argv,trainfile,modelfile,&verbosity,
			&struct_verbosity,&struct_parm,&learn_parm,
			&kernel_parm,&alg_type);

  if(struct_verbosity>=1) {
    //verbose = true;
    printf("Reading training examples..."); fflush(stdout);
  }
  /* read the training examples */
  sample=read_struct_examples(trainfile,&struct_parm);
  if(struct_verbosity>=1) {
    printf("done\n"); fflush(stdout);
  }

  string config_tmp;
  bool update_loss_function = false;
  if(Config::Instance()->getParameter("update_loss_function", config_tmp)) {
    update_loss_function = config_tmp.c_str()[0] == '1';
  }
  printf("[Main] update_loss_function=%d\n", (int)update_loss_function);
  if(!update_loss_function) {
    printf("update_loss_function should be true\n");
    exit(-1);
  }  

  eUpdateType updateType = UPDATE_NODE_EDGE;
  if(Config::Instance()->getParameter("update_type", config_tmp)) {
    updateType = (eUpdateType)atoi(config_tmp.c_str());
  }
  printf("[Main] update_type=%d\n", (int)updateType);

  mkdir(loss_dir, 0777);

  // check if parameter vector files exist
  const char* parameter_vector_dir = "parameter_vector";
  int idx = 0;
  string parameter_vector_dir_last = findLastFile(parameter_vector_dir, "", &idx);
  string parameter_vector_file_pattern = parameter_vector_dir_last + "/iteration_";
  int idx_1 = 1;
  string parameter_vector_file_last = findLastFile(parameter_vector_file_pattern, ".txt", &idx_1);
  printf("[Main] Checking parameter vector file %s\n", parameter_vector_file_last.c_str());

  // vectors used to store RF weights
  vector<double>* alphas = new vector<double>[sample.n];
  vector<double>* alphas_edge = 0;
  if(updateType == UPDATE_NODE_EDGE) {
    alphas_edge = new vector<double>[sample.n];
  }
  int alphas_idx = 0;

  if(fileExists("alphas.txt") && fileExists(parameter_vector_file_last)) {

    // Loading alpha coefficients
    ifstream ifs("alphas.txt");
    string line;
    int lineIdx = 0;
    while(lineIdx < sample.n && getline(ifs, line)) {
      vector<string> tokens;
      splitString(line, tokens);
      for(vector<string>::iterator it = tokens.begin();
          it != tokens.end(); ++it) {
        alphas[lineIdx].push_back(atoi(it->c_str()));
      }
      ++lineIdx;
    }
    ifs.close();
    if(lineIdx > 0) {
      alphas_idx = alphas[0].size();
    }

    // Loading parameters
    printf("[Main] Found parameter vector file %s\n", parameter_vector_file_last.c_str());
    struct_parm.ssvm_iteration = idx + 1;
    update_output_dir(struct_parm.ssvm_iteration);

    //EnergyParam param(parameter_vector_file_last.c_str());
    //updateCoeffs(sample, param, struct_parm, updateType, alphas, alphas_idx);
    //alphas_idx = 1;

  } else {
    struct_parm.ssvm_iteration = 0;

    // insert alpha coefficients for first iteration
    for(int i = 0; i < sample.n; ++i) {
      alphas[i].push_back(1);
    }

    ofstream ofs("alphas.txt", ios::app);
    int i = 0;
    for(; i < sample.n - 1; ++i) {
      ofs << alphas[i][alphas_idx] << " ";
    }
    if(i < sample.n) {
      ofs << alphas[i][alphas_idx];
    }
    ofs << endl;
    ofs.close();

    // edges
    for(int i = 0; i < sample.n; ++i) {
      alphas_edge[i].push_back(1);
    }

    ofstream ofse("alphas_edge.txt", ios::app);
    i = 0;
    for(; i < sample.n - 1; ++i) {
      ofse << alphas_edge[i][alphas_idx] << " ";
    }
    if(i < sample.n) {
      ofse << alphas_edge[i][alphas_idx];
    }
    ofse << endl;
    ofse.close();

    ++alphas_idx;
  }

  const int nMaxIterations = 5;
  bool bIterate = true;
  do {

    printf("-----------------------------------------SSVM-ITERATION-%d-START\n",
           struct_parm.ssvm_iteration);

    struct_parm.iterationId = 1;

    /* Do the learning and return structmodel. */
    if(alg_type == 0)
      svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,NSLACK_ALG);
    else if(alg_type == 1)
      svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,NSLACK_SHRINK_ALG);
    else if(alg_type == 2)
      svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_PRIMAL_ALG);
    else if(alg_type == 3)
      svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_DUAL_ALG);
    else if(alg_type == 4)
      svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_DUAL_CACHE_ALG);
    else if(alg_type == 9)
      svm_learn_struct_joint_custom(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel);
    else
      exit(1);

    char _modelfile[BUFFER_SIZE];
    //sprintf(_modelfile, "%s_%d", modelfile, struct_parm.ssvm_iteration);
    sprintf(_modelfile, "%s_%d", modelfile, struct_parm.ssvm_iteration);
    printf("[Main] Writing learned model to %s\n", _modelfile);
    write_struct_model(_modelfile, &structmodel, &struct_parm);

    // Run inference on training data and increase loss for misclassified points
    printf("[Main] Loading learned model to %s\n", _modelfile);
    EnergyParam param(_modelfile);

    updateCoeffs(sample, param, struct_parm, updateType, alphas, alphas_edge,
                 struct_parm.ssvm_iteration + 1);

    ofstream ofs("alphas.txt", ios::app);
    int i = 0;
    for(; i < sample.n - 1; ++i) {
      ofs << alphas[i][alphas_idx] << " ";
    }
    if(i < sample.n) {
      ofs << alphas[i][alphas_idx];
    }
    ofs << endl;
    ofs.close();

    ofstream ofse("alphas_edge.txt", ios::app);
    i = 0;
    for(; i < sample.n - 1; ++i) {
      ofse << alphas_edge[i][alphas_idx] << " ";
    }
    if(i < sample.n) {
      ofse << alphas_edge[i][alphas_idx];
    }
    ofse << endl;
    ofse.close();

    ++alphas_idx;

    printf("-----------------------------------------SSVM-ITERATION-%d-END\n",
           struct_parm.ssvm_iteration);

    ++struct_parm.ssvm_iteration;

    bIterate = (nMaxIterations == -1 || struct_parm.ssvm_iteration < nMaxIterations);

  } while(bIterate);

  // Output final segmentation for all examples
  long nExamples = sample.n;
  int nRFs = struct_parm.ssvm_iteration;
  double* lossPerLabel = 0;
  labelType* groundTruthLabels = 0;

  for(int i = 0; i < nExamples; i++) { /*** example loop ***/

    Slice_P* slice = sample.examples[i].x.slice;
    Feature* feature = sample.examples[i].x.feature;
    //map<sidType, nodeCoeffType>* nodeCoeffs = sample.examples[i].x.nodeCoeffs;
    //map<sidType, edgeCoeffType>* edgeCoeffs = sample.examples[i].x.edgeCoeffs;
    map<sidType, nodeCoeffType>* nodeCoeffs = 0;
    map<sidType, edgeCoeffType>* edgeCoeffs = 0;
    int nNodes = slice->getNbSupernodes();

    stringstream soutPb;
    soutPb << loss_dir;
    soutPb << "pb_";
    soutPb << getNameFromPathWithoutExtension(slice->getName());
    soutPb << "_";
    soutPb << "combined";
    //soutPb << setw(5) << setfill('0') << ssvm_iteration;
    soutPb << ".tif";
    printf("[Main] Exporting %s\n", soutPb.str().c_str());

    labelType* inferredLabels =
      computeCombinedLabels(slice, feature, groundTruthLabels,
                            lossPerLabel, nRFs, alphas, i,
                            nodeCoeffs, edgeCoeffs, soutPb.str().c_str());

    stringstream sout;
    sout << loss_dir;
    sout << getNameFromPathWithoutExtension(slice->getName());
    sout << "_";
    sout << "combined";
    //sout << setw(5) << setfill('0') << ssvm_iteration;
    sout << ".tif";
    printf("[Main] Exporting %s\n", sout.str().c_str());
    slice->exportOverlay(sout.str().c_str(), inferredLabels);

    stringstream soutBW;
    soutBW << loss_dir;
    soutBW << getNameFromPathWithoutExtension(slice->getName());
    soutBW << "_";
    soutBW << "combined";
    //soutBW << setw(5) << setfill('0') << ssvm_iteration;
    soutBW << "_BW.tif";
    printf("[Main] Exporting %s\n", soutBW.str().c_str());        
    slice->exportSupernodeLabels(soutBW.str().c_str(),
                                 struct_parm.nClasses,
                                 inferredLabels, nNodes,
                                 &(struct_parm.labelToClassIdx));

    delete[] inferredLabels;
  }

  free_struct_sample(sample);
  free_struct_model(structmodel);

  svm_struct_learn_api_exit();

  return 0;
}
void Foam::timeVaryingMappedTotalPressureFvPatchScalarField::updateCoeffs()
{
    updateCoeffs(lookupPatchField<volVectorField, vector>(UName_));
}
void Foam::timeVaryingUniformTotalPressureFvPatchScalarField::updateCoeffs()
{
    updateCoeffs(patch().lookupPatchField<volVectorField, vector>(UName_));
}