Fish - FiberLib for VISH 0.3
Fish - The Fiber Bundle API for the Vish Visualization Shell
141-LambdaBinding.cpp File Reference

[← Previous Example] [Next Example → 142-AsyncLambdaFragments.cpp ]. More...

#include <fiber/bundle/Bundle.hpp>
#include <fiber/finit/FinitAPI.h>
#include <fiber/field/ArrayRef.hpp>
#include <fiber/field/UniformCartesianArray.hpp>
#include <fiber/field/FunctionalCreator.hpp>

Functions

int main ()

Detailed Description

[← Previous Example] [Next Example → 142-AsyncLambdaFragments.cpp ].

In FiberLib Tutorial

#include <fiber/bundle/Bundle.hpp>
#include <fiber/finit/FinitAPI.h>
#include <fiber/field/ArrayRef.hpp>
#include <fiber/field/UniformCartesianArray.hpp>
#include <fiber/field/FunctionalCreator.hpp>
using namespace Fiber;
using namespace Eagle::PhysicalSpace;
int main()
{
// Initialize I/O layers (only required for standalone binaries)
Finit();
//
// Create a bundle object with a grid at T=1.0, named "DemoGrid", in
// a three-dimensional Cartesian representation, using the default
// coordinate system.
//
Representation&myCartesianRepresentation = BP[1.0]["DemoGrid"].makeCartesianRepresentation(3);
//
// Save the bundle to a file, even future data
// that are added to the bundle later. This feature
// is called "binding" a file to the Bundle.
//
BP->bindTo("141_LambdaBinding.f5");
//
// Create two fields, no data or fragmentation specified yet
//
RefPtr<Field>&Positions = myCartesianRepresentation[ FIBER_POSITIONS ];
RefPtr<Field>&Values = myCartesianRepresentation["Values"];
Eagle::point3 BBoxMin(-1.0, -1.0, -0.5), BBoxMax( 1.0, 1.0, 0.5);
// MODIFY HERE FOR CHANGING NUMBER OF FRAGMENTS
//const int nTilesX = 50, nTilesY = 30;
const int nTilesX = 5, nTilesY = 3;
Eagle::tvector3 FragmentDiagonal = (BBoxMax - BBoxMin).multiply( 1.0/ nTilesX, 1.0/ nTilesY, 1.0);
//
// Produce a couple of fragments
//
for(int tileY = 0; tileY < nTilesY; tileY++)
for(int tileX = 0; tileX < nTilesX; tileX++)
{
//
// Generate a unique name for the fragment
// The name is of no relevance and could also
// be just a random string.
//
string FragmentName = "tile" + std::to_string(tileX) + "x" + std::to_string(tileY);
//
// Define the size of the fragment to be used henceforth
//
const MultiIndex<3> Dims = MIndex(11,13,17);
Eagle::point3 FragmentBBoxMin = BBoxMin + FragmentDiagonal.multiply( tileX, tileY, 0.0);
//
// Produce uniform coordinates for the given fragment.
//
Coordinates = new UniformCartesianArray(Dims, FragmentBBoxMin,
FragmentDiagonal.multiply(1.0/(Dims[0]-1), 1.0/(Dims[1]-1), 1.0/(Dims[2]-1) ) );
Positions->createCreator(Coordinates, FragmentName);
Positions->setFragmentOffset( FragmentName,
MIndex( tileX*Dims[0], tileY*Dims[1], 0*Dims[2] ) );
// enable for writing field information in addition to coordinate info
// 1.
// Conventional version
if (false)
{
ArrayRef<double, 3> GridValues(Dims);
UniformCartesianArray&Pts = *Coordinates;
for(MultiIndex<3> I : Dims)
{
double& value = GridValues[ I ];
Eagle::point3 P = Pts[ I ];
//
// Some analytic function here used to produce some
// halfway interestingly looking scalar field.
//
value = P.x()*P.x() - P.y()*P.y() + P.z()*P.z();
}
Values->createCreator(GridValues, FragmentName);
}
// 2.
// Lambda version, as function
if (false)
{
auto LambdaFunction =
[](const MultiIndex<3>&Dims, const RefPtr<UniformCartesianArray>&Coordinates)
{
ArrayRef<double, 3> GridValues(Dims);
UniformCartesianArray&Pts = *Coordinates;
for(MultiIndex<3> I : Dims)
{
double& value = GridValues[ I ];
Eagle::point3 P = Pts[ I ];
//
// Some analytic function here used to produce some
// halfway interestingly looking scalar field.
//
value = P.x()*P.x() - P.y()*P.y() + P.z()*P.z();
}
return GridValues;
};
Values->createCreator(LambdaFunction(Dims, Coordinates), FragmentName);
}
// 3.
// Lambda version, as functor
if (false)
{
auto LambdaFunctor =
[Dims, Coordinates]()
{
ArrayRef<double, 3> GridValues(Dims);
UniformCartesianArray&Pts = *Coordinates;
for(MultiIndex<3> I : Dims)
{
double& value = GridValues[ I ];
Eagle::point3 P = Pts[ I ];
//
// Some analytic function here used to produce some
// halfway interestingly looking scalar field.
//
value = P.x()*P.x() - P.y()*P.y() + P.z()*P.z();
}
return GridValues;
};
if (false)
Values->createCreator(LambdaFunctor(), FragmentName);
else // 4.
Values->setCreator(newFunctionalCreator(LambdaFunctor),
Positions->getFragmentID(FragmentName) );
}
// 5.
// And finally, the same functionality in one expression:
if (true)
{
Values->setCreator(newFunctionalCreator(
[Dims, Coordinates]()
{
ArrayRef<double, 3> GridValues(Dims);
UniformCartesianArray&Pts = *Coordinates;
for(MultiIndex<3> I : Dims)
{
double& value = GridValues[ I ];
Eagle::point3 P = Pts[ I ];
//
// Some analytic function here used to produce some
// halfway interestingly looking scalar field.
//
value = P.x()*P.x() - P.y()*P.y() + P.z()*P.z();
}
return GridValues;
} ),
Positions->getFragmentID(FragmentName) );
}
}
return 0;
}
int main()
Demonstrates minimal usage of the FiberLib Create Bundle, insert a time slice, and safe it to a file.
Definition 010-SimpleSave.cpp:13
An array reference class, which is a convenience class for reference pointers to multidimensional mem...
Definition ArrayRef.hpp:28
Convenience class that implements a pointer to a Bundle object but adds some useful member funtions t...
Definition Bundle.hpp:779
A multidimensional index that is automatically a lower-dimensional index via recursion.
Definition MultiIndex.hpp:449
A Representation is a set of Field objects, each of them accessed via some FieldID identifier.
Definition Representation.hpp:101
Convenience class for procedural linear arrays in cartesian coordinates.
Definition UniformCartesianArray.hpp:15
StrongPtr< Object, ObjectBase > RefPtr
Given a fragmented field of curvilinear coordinates, (3D array of coordinates), build a uniform Grid ...
Definition FAQ.dox:2
string to_string(const Eagle::FixedArray< ElementType, N > &A, const char *OpenBrace="{", const char *CloseBrace="}", const char *Separator=",")