If I have to output gradients of a variable, the straightforward approach would be to evaluate the gradients over quadrature points and then average. This gives me an average value of my gradient per cell.
Do you know how does data DataPostProcessor evaluates the value of the gradient at the mesh nodes? How does it it work under the hood? Do you mind explaining?
Deepika,
This is a strain post processor. Create a function that does this:
template <int dim>
class StrainPostprocessor : public DataPostprocessorTensor<dim>
{
public:
StrainPostprocessor()
: DataPostprocessorTensor<dim>("strain",
update_gradients)
{
}
virtual void evaluate_vector_field(const DataPostprocessorInputs::Vector<dim> &input_data, std::vector<Vector<double>> &computed_quantities) const override
{
AssertDimension(input_data.solution_gradients.size(),
computed_quantities.size());
for (unsigned int p = 0; p < input_data.solution_gradients.size(); ++p)
{
AssertDimension(computed_quantities[p].size(),
(Tensor<2, dim>::n_independent_components));
for (unsigned int d = 0; d < dim; ++d)
for (unsigned int e = 0; e < dim; ++e)
computed_quantities[p][Tensor<2, dim>::component_to_unrolled_index(TableIndices<2>(d, e))] = (input_data.solution_gradients[p][d][e] +
input_data.solution_gradients[p][e][d]) /
2;
}
}
};
Then in data output you can do this:
void CG_Elasticity_Linear<dim>::output_results(unsigned int refinement_level, unsigned int cycle) const
{
std::vector<DataComponentInterpretation::DataComponentInterpretation>
interpretation(dim,
DataComponentInterpretation::component_is_part_of_vector);
std::vector<std::string> solution_names(dim, "u");
DataOut<dim> data_out;
data_out.add_data_vector(dof_handler, solution, solution_names, interpretation);
const StrainPostprocessor<dim> strain;
data_out.add_data_vector(dof_handler, solution, strain);
data_out.build_patches();
std::ofstream output("solution" + std::to_string(refinement_level) + "_" + std::to_string(cycle) + ".vtu");
data_out.write_vtu(output);
}
For averages you'll have to loop in the DataPostProcessor derived class and average your quantity.
Best,
Abbas