Field¶
Construct a block field¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <Cubism/Block/Field.h>
int main(void)
{
// a custom field state data structure
struct MyFieldState {
size_t myID; // custom field state
};
constexpr size_t dim = 2; // 2D problem
using CellField = Cubism::Block::CellField<double, dim, MyFieldState>;
using IRange = typename CellField::IndexRangeType;
using MIndex = typename IRange::MultiIndex;
MIndex cells(16); // number of cells in field (256 cells)
IRange cell_domain(cells); // index range spanned by cell domain
CellField cf(cell_domain); // cell field (memory untouched)
cf.getState().myID = 101; // set my custom state
for (auto &c : cf) { // cell c in cell field cf
c = 0;
}
return 0;
}
|
Create a view to a block field¶
A view is a cheap way to create a copy of another block field. Copies for
views are shallow and therefore a view never owns memory. A field view type
inherits from its underlying field type such that all of the field interface is
available for field views as well with different behavior for copy assignment.
A field view always performs shallow copy assignment. This may not be desirable
if the user actually wants to copy data from a field to the field being viewed
at. To enforce a deep copy the copyData()
method can be used which must be
provided by the base field type. A soft view provides a copy()
method to
return a deep copy of the viewed field.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <Cubism/Block/Field.h>
int main(void)
{
// CUBISM_DIMENSION-ional field
using CellField = Cubism::Block::CellField<float>;
using FieldView = Cubism::Block::FieldView<CellField>; // a CellField view
using IRange = typename CellField::IndexRangeType;
using MIndex = typename IRange::MultiIndex;
MIndex cells(16); // number of cells in field (256 cells)
IRange cell_domain(cells); // index range spanned by cell domain
CellField cf(cell_domain); // cell field (memory untouched, owner)
CellField co(cell_domain); // some other cell field, memory owner
FieldView cv(cf); // cv views into cf
cv.setView(co); // cv now views into co (cheap)
cv.copyData(cf); // data of cf is copied into co (expensive)
CellField cc = cv.copy(); // create full copy of co in cc (move assignment)
FieldView ccv(cc); // another view
cf = ccv; // assign to field from view
return 0;
}
|