// Copyright (c) 2010-2023, Lawrence Livermore National Security, LLC. Produced // at the Lawrence Livermore National Laboratory. All Rights reserved. See files // LICENSE and NOTICE for details. LLNL-CODE-806117. // // This file is part of the MFEM library. For more information and source code // availability visit https://mfem.org. // // MFEM is free software; you can redistribute it and/or modify it under the // terms of the BSD-3 license. We welcome feedback and contributions, see file // CONTRIBUTING.md for details. #include "admfem.hpp" #include "mfem.hpp" template class DiffusionFunctional { public: TDataType operator() (TParamVector& vparam, TStateVector& uu) { MFEM_ASSERT(state_size==4,"ExampleFunctor state_size should be equal to 4!"); MFEM_ASSERT(param_size==2,"ExampleFunctor param_size should be equal to 2!"); auto kappa = vparam[0]; // diffusion coefficient auto load = vparam[1]; // volumetric influx TDataType rez = kappa*(uu[0]*uu[0]+uu[1]*uu[1]+uu[2]*uu[2])/2.0 - load*uu[3]; return rez; } }; template class DiffusionResidual { public: void operator ()(TParamVector& vparam, TStateVector& uu, TStateVector& rr) { MFEM_ASSERT(residual_size==4, "DiffusionResidual residual_size should be equal to 4!"); MFEM_ASSERT(state_size==4,"ExampleFunctor state_size should be equal to 4!"); MFEM_ASSERT(param_size==2,"ExampleFunctor param_size should be equal to 2!"); auto kappa = vparam[0]; // diffusion coefficient auto load = vparam[1]; // volumetric influx rr[0] = kappa * uu[0]; rr[1] = kappa * uu[1]; rr[2] = kappa * uu[2]; rr[3] = -load; } }; int main(int argc, char *argv[]) { #ifdef MFEM_USE_ADFORWARD std::cout<<"MFEM_USE_ADFORWARD == true"< adf; mfem::Vector rr0(4); mfem::DenseMatrix hh0(4,4); mfem::Vector rr1(4); mfem::DenseMatrix hh1(4,4); MFEM_PERF_BEGIN("Grad"); adf.Grad(param,state,rr0); MFEM_PERF_END("Grad"); MFEM_PERF_BEGIN("Hessian"); adf.Hessian(param, state, hh0); MFEM_PERF_END("Hessian"); // dump out the results std::cout<<"FunctionAutoDiff"< rdf; MFEM_PERF_BEGIN("Jacobian"); rdf.Jacobian(param, state, hh1); MFEM_PERF_END("Jacobian"); std::cout<<"ResidualAutoDiff"< fdr(func); MFEM_PERF_BEGIN("JacobianV"); fdr.Jacobian(param,state, hh1); // computes the gradient of func and stores the result in hh1 MFEM_PERF_END("JacobianV"); std::cout<<"LambdaAutoDiff"< fdr01(func01); MFEM_PERF_BEGIN("Jacobian1"); fdr01.Jacobian(param,state,hh1); MFEM_PERF_END("Jacobian1"); std::cout<<"LambdaAutoDiff 01"<