/* fieldData.dat is the file containing the field values and nNodes is the number of values in the file */ ifstream is("../../examples-tutorial/fieldData.dat"); int nNodes; is >> nNodes; /* check for consistency between the mesh and the data */ TEUCHOS_TEST_FOR_EXCEPTION(mesh.numCells(0) != nNodes, RuntimeError, "number of nodes in data file fieldData.dat is " << nNodes << " but number of nodes in mesh is " << mesh.numCells(0)); /* read the data, putting each entry into its correct place in the vector as indexed by the dof number, NOT the node number. Here I'm assuming one field value per node. */ Array<int> dofs(1); double fVal; for (int i=0; i<nNodes; i++) { /* look up the dof for the 0-th function on this node */ dofMap->getDOFsForCell(0, i, 0, dofs); int dof = dofs[0]; is >> fVal; vec.setElement(dof, fVal); }
which reads the data from the file to account for any variations in file formatting.
RefCountPtr<Array<Array<double> > > fVal;
for (int i = 0; i < (*fVal).size(); i++) { map->getDOFsForCell(0, i, 0, dofs); vec.setElement(dofs[0], (*fVal)[i][0]); map->getDOFsForCell(0, i, 1, dofs); vec.setElement(dofs[0], (*fVal)[i][1]); }
The values are read from a data file in nodal order, then the degrees of freedom (DOF) map is consulted. The field values are reordered into a DiscreteFunction associated with the DOF in the mesh rather than the nodes, as this is how Sundance stores everything under the hood. At this point the field variable can be used within a weak form and integrated over any subset of the mesh. If one needs to access individual elements of a vector-valued field in the weak form, the square-bracket notation is used (i.e., u0[0] from the ReadField.cpp example).