mkiewat |
February 12, 2016 09:49 |
I'm dealing with a similar problem. I managed to extract the entire field to std namespace vectors using the object registry to get the components of the vectorfield (u_,v_,w_), then I copy them cell by cell (which is probably not a good idea at all) into the std vector structure (u,v,w).
Code:
const volScalarField u_(obr().objectRegistry::lookupObject<volVectorField>("U").component(vector::X));
const volScalarField v_(obr().objectRegistry::lookupObject<volVectorField>("U").component(vector::Y));
const volScalarField w_(obr().objectRegistry::lookupObject<volVectorField>("U").component(vector::Z));
const int nCells(v_.size());
std::vector<double> u(nCells,0);
std::vector<double> v(nCells,0);
std::vector<double> w(nCells,0);
forAll(u_, cellI)
{
u[cellI]=u_[cellI];
v[cellI]=v_[cellI];
w[cellI]=w_[cellI];
}
My current problem is, that I want to extract only a certain sub-volume of cells to such a data structure. For this I copied the cellSource functionObject and started to look for the right location to implement that data extraction and transfer. I figured, the function Foam::fieldValues::cellSource::writeValues(const word& fieldName) from cellSourceTemplates.C might be a good place, since the sub-volume field data is already available by the variable "values". The problem: how do I extract the respective velocity components (or pressure values) from this structure? The values variable can contain: <scalar> <vector> <sphericalTensor> <symmTensor> <tensor>. Depending on the function call "writeValues" the type of values is different. I would like to extract the numbers stored in "values". At the end I want to transfer a p-vector and vectors for each velocity component. Any help is appreciated.
Here is the function that I want to edit and use:
Code:
template<class Type>
bool Foam::fieldValues::cellToMat::writeValues(const word& fieldName)
{
const bool ok = validField<Type>(fieldName);
if (ok)
{
Field<Type> values(setFieldValues<Type>(fieldName));
scalarField V(filterField(mesh().V()));
scalarField weightField(values.size(), 1.0);
if (weightFieldName_ != "none")
{
weightField = setFieldValues<scalar>(weightFieldName_, true);
}
// Combine onto master
combineFields(values);
combineFields(V);
combineFields(weightField);
// apply weight field
values *= weightField;
if (Pstream::master())
{
Type result = processValues(values, V, weightField);
// add to result dictionary, over-writing any previous entry
resultDict_.add(fieldName, result, true);
if (valueOutput_)
{
IOField<Type>
(
IOobject
(
fieldName + "_" + sourceTypeNames_[source_] + "-"
+ sourceName_,
obr_.time().timeName(),
obr_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
values
).write();
}
file()<< tab << result;
Info(log_)<< " " << operationTypeNames_[operation_]
<< "(" << sourceName_ << ") for " << fieldName
<< " = " << result << endl;
//----- moving field to MATLAB starts here --------//
// available variables:
// - sourceName_ = "sampleBox" -> cellZone name
// - fieldName = "U" -> name of the currently processed field
// - operationTypeNames_[operation_] = "none" -> operation to be applied to the field
// (can be:"none","sum","average","weightedAverage"
// "volAverage","volIntegrate","min","max","CoV")
// - result = (0 0 0) -> result from operation
// - obr_.time().timeName() = "100.01"
Info << nl << "Transferring " << fieldName
<< " to MATLAB at t= " << obr_.time().timeName() << endl;
if(fieldName == "U")
{
//const scalarField& test(values.component(vector::X));
const volScalarField u_(values.component(vector::X));
//Info << nl << "!!!!now U!!! " << values[0].component(vector::X) << endl;
//int* temp = values[0];
//ptr->DoSomething(); // Use the object in some way.
}
else if(fieldName == "p")
{
Info << nl << "!!!!now p!!!" << values[0] << endl;
}
//const DimensionedField<Type,volMesh>& iField = this->dimensionedInternalField();
//const Field<Type>& iField = this->internalField();
// if (!iField.name().compare("U"))
// {
// Info << nl << "SUCCESS " << endl;
// }
}
}
return ok;
}
|