Loading...
Searching...
No Matches
aux::Gradient Class Reference

Methods to calculate gradients (derivatives) with respect to different directions. More...

#include <gpr/auxiliary/Gradient.h>

Public Member Functions

 Gradient ()
virtual ~Gradient ()
void calculateDerivativesSameDim (const gpr::Coord &x1, const gpr::Coord &x2, const gpr::AtomsConfiguration &conf_info, const gpr::FieldMatrixd &lengthScale, const gpr::FieldMatrixIdx &dims, uint8_t calc_options, gpr::Derivatives< std::vector< gpr::FieldMatrixd > > &derivatives, gpr::Derivatives< std::vector< gpr::FieldMatrixd > > &derivatives_pt)
 Evaluate gradient of covariance function with respect to both input variables x and x2 (in same dimension).
void setFarFieldRadius (double radius)
double farFieldRadius () const
void calculateDerivativesDiffDim (const gpr::Coord &x1, const gpr::Coord &x2, const gpr::AtomsConfiguration &conf_info, const gpr::FieldMatrixd &lengthScale, const gpr::FieldMatrixIdx &dims1, const gpr::FieldMatrixIdx &dims2, const uint8_t calc_options, const uint8_t direction_x2, gpr::Derivatives< std::vector< gpr::FieldMatrixd > > &derivatives, gpr::Derivatives< std::vector< gpr::FieldMatrixd > > &derivatives_pt)
 Evaluate gradient of covariance function, of which has been taken partial derivatives with respect to both input variables x and x2 with respect to parameters (if specified).

Detailed Description

Methods to calculate gradients (derivatives) with respect to different directions.

Definition at line 28 of file Gradient.h.

Constructor & Destructor Documentation

◆ Gradient()

aux::Gradient::Gradient ( )
inline

Definition at line 30 of file Gradient.h.

30{ }

◆ ~Gradient()

virtual aux::Gradient::~Gradient ( )
inlinevirtual

Definition at line 31 of file Gradient.h.

31{ }

Member Function Documentation

◆ calculateDerivativesDiffDim()

void aux::Gradient::calculateDerivativesDiffDim ( const gpr::Coord & x1,
const gpr::Coord & x2,
const gpr::AtomsConfiguration & conf_info,
const gpr::FieldMatrixd & lengthScale,
const gpr::FieldMatrixIdx & dims1,
const gpr::FieldMatrixIdx & dims2,
const uint8_t calc_options,
const uint8_t direction_x2,
gpr::Derivatives< std::vector< gpr::FieldMatrixd > > & derivatives,
gpr::Derivatives< std::vector< gpr::FieldMatrixd > > & derivatives_pt )

Evaluate gradient of covariance function, of which has been taken partial derivatives with respect to both input variables x and x2 with respect to parameters (if specified).

Warning
derivatives_pt sums every coordinate pair named by dims1 and dims2. derivatives.D1/D2/D12 hold one block per pair.
Parameters
x1Matrix of gpr::Coordinates
x2Matrix of gpr::Coordinates
conf_infoConfiguration data for the GP model
lengthScaleLength-scale
dims1Coordinate-dimension indices differentiated in x1.
dims2Coordinate-dimension indices differentiated in x2.
calc_optionsGeneral type of the options to be calculated
direction_x2Direction for the derivative calculation for x2 field.
derivativesField of derivatives calculated at each dims (one block per coordinate)
derivatives_ptField of pairtype derivatives

Definition at line 655 of file Gradient.cpp.

663{
664 if (conf_info.pair_cutoff > 0.0 || far_field_radius_ > 0.0) {
665 std::vector<std::pair<Eigen::Index, Eigen::Index>> dimensions;
666 for (Eigen::Index i = 0; i < dims1.size(); ++i)
667 for (Eigen::Index j = 0; j < dims2.size(); ++j)
668 dimensions.emplace_back(dims1.data()[i] - 1,
669 dims2.data()[j] - 1);
670 calculateCutoffDerivativeFields(
671 x1, x2, conf_info, lengthScale, far_field_radius_, dimensions,
672 calc_options, derivatives, derivatives_pt);
673 return;
674 }
675
676 // thread_local scratch: D1/D2/D12/s2 are resized + (re)initialized
677 // on every call below, so their Eigen storage is reused across
678 // calls value-identically; thread_local keeps each thread private
679 // under the OpenMP serial-oracle region.
680 thread_local gpr::FieldMatrixd D1;
681 thread_local gpr::FieldMatrixd D2;
682 thread_local gpr::FieldMatrixd D12;
683 thread_local gpr::FieldMatrixd s2;
684 thread_local std::vector<double> invr2_scratch;
685 thread_local std::vector<double> deriv2_scratch;
686 Eigen::Index n1 = x1.rows();
687 Eigen::Index n2 = x2.rows();
688 Eigen::Index N_mov = conf_info.atoms_mov.type.cols();
689 Eigen::Index N_fro = conf_info.atoms_froz_active.type.cols();
690
691 D1.resize(n1, n2);
692 D1.setZero();
693 D2.resize(n1, n2);
694 D2.setZero();
695 D12.resize(n1, n2);
696 D12.setZero();
697 s2.resize(1, lengthScale.size());
698
699 resizeDerivativeFields(x1, x2, conf_info, calc_options,
701 derivatives_pt.D1);
702
703 resizeDerivativeFields(x1, x2, conf_info, calc_options,
705 derivatives_pt.D2);
706
707 resizeDerivativeFields(x1, x2, conf_info, calc_options,
709 derivatives_pt.D12);
710
711 // Reserve space to avoid reallocations during push_back
712 const auto total_dims =
713 static_cast<size_t>(dims1.size()) * static_cast<size_t>(dims2.size());
714 if (calc_options & OptionsForGradCalculation::D1)
715 derivatives.D1.reserve(total_dims);
716 if (calc_options & OptionsForGradCalculation::D2)
717 derivatives.D2.reserve(total_dims);
718 if (calc_options & OptionsForGradCalculation::D12)
719 derivatives.D12.reserve(total_dims);
720
721 for (Eigen::Index n = 0; n < s2.size(); ++n)
722 *(s2.data() + n) =
723 1. / (*(lengthScale.data() + n) * *(lengthScale.data() + n));
724
725 for (Eigen::Index dim_counter_1 = 0; dim_counter_1 < dims1.size();
726 ++dim_counter_1) {
727 for (Eigen::Index dim_counter_2 = 0; dim_counter_2 < dims2.size();
728 ++dim_counter_2) {
729 // A later coordinate pair must not keep an earlier pair.
730 // D12 holds only this pair. The pair type field is the running
731 // sum, so the two negations below flip only this pair.
732 D1.setZero();
733 D2.setZero();
734 D12.setZero();
735 gpr::Index_t i_dim_1 = *(dims1.data() + dim_counter_1);
736 gpr::Index_t i_1 = ceil(i_dim_1 / 3.) - 1;
737 gpr::Index_t i_dim_2 = *(dims2.data() + dim_counter_2);
738 gpr::Index_t i_2 = ceil(i_dim_2 / 3.) - 1;
739 gpr::Index_t xyz_1 = i_dim_1 - i_1 * 3 - 1;
740 gpr::Index_t xyz_2 = i_dim_2 - i_2 * 3 - 1;
741 gpr::Index_t i;
742
743 if (i_1 != i_2) {
744 i = i_1;
745 if (N_mov > 1) {
746 for (Eigen::Index j = 0; j < N_mov; ++j) {
747 if (i != j) {
748 calculateGradientBetweenMovingAtoms(
749 x1, x2, conf_info, lengthScale, xyz_1, xyz_1,
750 {i, j},
751 calc_options &
754 {&D1, &D2, &D12}, derivatives_pt, invr2_scratch,
755 deriv2_scratch);
756 }
757 }
758 }
759
760 if (N_fro > 0) {
761 for (Eigen::Index j = 0; j < N_fro; ++j) {
762 calculateGradientBetweenMovingAndFrozenAtoms(
763 x1, x2, conf_info, lengthScale, xyz_1, xyz_1,
764 {i, j},
765 calc_options & (OptionsForGradCalculation::D1 |
767 {&D1, &D2, &D12}, derivatives_pt, invr2_scratch,
768 deriv2_scratch);
769 }
770 accumulateFarFieldDerivatives(
771 x1, x2, conf_info, lengthScale, i, xyz_1, xyz_1,
772 calc_options & (OptionsForGradCalculation::D1 |
774 D1, D2, D12, derivatives_pt);
775 }
776
777 i = i_2;
778 if (N_mov > 1) {
779 for (Eigen::Index j = 0; j < N_mov; ++j) {
780 if (i != j) {
781 calculateGradientBetweenMovingAtoms(
782 x1, x2, conf_info, lengthScale, xyz_2, xyz_2,
783 {i, j},
784 calc_options &
787 {&D1, &D2, &D12}, derivatives_pt, invr2_scratch,
788 deriv2_scratch);
789 }
790 }
791 }
792
793 if (N_fro > 0) {
794 for (Eigen::Index j = 0; j < N_fro; ++j) {
795 calculateGradientBetweenMovingAndFrozenAtoms(
796 x1, x2, conf_info, lengthScale, xyz_2, xyz_2,
797 {i, j},
798 calc_options & (OptionsForGradCalculation::D2 |
800 {&D1, &D2, &D12}, derivatives_pt, invr2_scratch,
801 deriv2_scratch);
802 }
803 accumulateFarFieldDerivatives(
804 x1, x2, conf_info, lengthScale, i, xyz_2, xyz_2,
805 calc_options & (OptionsForGradCalculation::D2 |
807 D1, D2, D12, derivatives_pt);
808 }
809
810 // The pair-type field sums every coordinate pair. Negate it
811 // before this pair is added and again afterwards, so only
812 // this pair changes sign. One negation would also reverse
813 // every earlier pair.
814 if (calc_options & OptionsForGradCalculation::D12_pt) {
815 for (size_t n = 0; n < derivatives_pt.D12.size(); ++n) {
816 for (Eigen::Index m = 0;
817 m < derivatives_pt.D12[n].size(); ++m) {
818 *(derivatives_pt.D12[n].data() + m) *= -1.;
819 }
820 }
821 }
822
823 calculateGradientBetweenMovingAtoms(
824 x1, x2, conf_info, lengthScale, xyz_1, xyz_2, {i_1, i_2},
825 calc_options & (OptionsForGradCalculation::D12 |
827 {&D1, &D2, &D12}, derivatives_pt, invr2_scratch,
828 deriv2_scratch);
829
830 // The cross derivative of two different atoms comes back
831 // with the opposite sign from the Matlab convention. D12
832 // holds only this pair, so one negation corrects it.
833 for (Eigen::Index n = 0; n < D12.size(); ++n) {
834 *(D12.data() + n) *= -1;
835 }
836
837 if (calc_options & OptionsForGradCalculation::D12_pt) {
838 for (size_t n = 0; n < derivatives_pt.D12.size(); ++n) {
839 for (Eigen::Index m = 0;
840 m < derivatives_pt.D12[n].size(); ++m) {
841 *(derivatives_pt.D12[n].data() + m) *= -1.;
842 }
843 }
844 }
845 } else {
846 i = i_1;
847
848 if (N_mov > 1) {
849 for (Eigen::Index j = 0; j < N_mov; ++j) {
850 if (i != j) {
851 calculateGradientBetweenMovingAtoms(
852 x1, x2, conf_info, lengthScale, xyz_1, xyz_2,
853 {i, j}, calc_options, {&D1, &D2, &D12},
854 derivatives_pt, invr2_scratch, deriv2_scratch);
855 }
856 }
857 }
858
859 if (N_fro > 0) {
860 for (Eigen::Index j = 0; j < N_fro; ++j) {
861 calculateGradientBetweenMovingAndFrozenAtoms(
862 x1, x2, conf_info, lengthScale, xyz_1, xyz_2,
863 {i, j}, calc_options, {&D1, &D2, &D12},
864 derivatives_pt, invr2_scratch, deriv2_scratch);
865 }
866 accumulateFarFieldDerivatives(
867 x1, x2, conf_info, lengthScale, i, xyz_1, xyz_2,
868 calc_options, D1, D2, D12, derivatives_pt);
869 }
870 }
871
872 pushBackDerivativeField(calc_options, OptionsForGradCalculation::D1,
873 D1, derivatives.D1);
874
875 pushBackDerivativeField(calc_options, OptionsForGradCalculation::D2,
876 D2, derivatives.D2);
877
878 pushBackDerivativeField(calc_options,
880 derivatives.D12);
881 }
882 }
883}
@ D1_pt
Definition Enums.h:23
@ D1
Definition Enums.h:19
@ D12
Definition Enums.h:21
@ D2_pt
Definition Enums.h:24
@ D2
Definition Enums.h:20
@ D12_pt
Definition Enums.h:25
uint32_t Index_t
Definition Macros.h:17
Eigen::Matrix< double, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixd
Definition Macros.h:25
AtomsPositionAndType atoms_froz_active
Info on active frozen atoms.
Definition Structures.h:841
AtomsPositionAndType atoms_mov
Info on moving atoms.
Definition Structures.h:844

◆ calculateDerivativesSameDim()

void aux::Gradient::calculateDerivativesSameDim ( const gpr::Coord & x1,
const gpr::Coord & x2,
const gpr::AtomsConfiguration & conf_info,
const gpr::FieldMatrixd & lengthScale,
const gpr::FieldMatrixIdx & dims,
uint8_t calc_options,
gpr::Derivatives< std::vector< gpr::FieldMatrixd > > & derivatives,
gpr::Derivatives< std::vector< gpr::FieldMatrixd > > & derivatives_pt )

Evaluate gradient of covariance function with respect to both input variables x and x2 (in same dimension).

The first member of derivatives will contain a vector of fields for every derivative calculated for x (analog of D1). The second member of derivatives will contain derivatives calculated for active pairtypes.

Warning
derivatives_pt sums every coordinate named in dims. derivatives.D1/D2/D12 hold one block per coordinate.
Parameters
x1Matrix of gpr::Coordinates
x2Matrix of gpr::Coordinates
conf_infoConfiguration data for the GP model
lengthScaleLength-scale
dimsDerivative dimensions evaluated for both inputs
calc_optionsDetermines which derivative to calculate (D1, D2, D12)
derivativesOutput derivative fields (one block per coordinate)
derivatives_ptOutput derivatives for active pair types

Definition at line 555 of file Gradient.cpp.

562{
563 if (conf_info.pair_cutoff > 0.0 || far_field_radius_ > 0.0) {
564 std::vector<std::pair<Eigen::Index, Eigen::Index>> dimensions;
565 for (Eigen::Index i = 0; i < dims.size(); ++i)
566 dimensions.emplace_back(dims.data()[i] - 1, dims.data()[i] - 1);
567 calculateCutoffDerivativeFields(
568 x1, x2, conf_info, lengthScale, far_field_radius_, dimensions,
569 calc_options, derivatives, derivatives_pt);
570 return;
571 }
572
573 // thread_local scratch: the accumulator matrices and the per-x2
574 // distance/derivative vectors are resized + zeroed on every call
575 // below (via resizeDerivativeFields and the pre-x1 loop in
576 // calculateGradientBetweenMovingAtoms), so reusing their Eigen/std
577 // storage across calls is value-identical while eliminating the
578 // per-call heap allocation. thread_local (not static) keeps each
579 // thread's scratch private under the OpenMP serial-oracle region.
580 thread_local gpr::FieldMatrixd D1;
581 thread_local gpr::FieldMatrixd D2;
582 thread_local gpr::FieldMatrixd D12;
583 thread_local std::vector<double> invr2_scratch;
584 thread_local std::vector<double> deriv2_scratch;
585 Eigen::Index N_mov = conf_info.atoms_mov.type.cols();
586 Eigen::Index N_fro = conf_info.atoms_froz_active.type.cols();
587
588 resizeDerivativeFields(x1, x2, conf_info, calc_options,
590 derivatives_pt.D1);
591
592 resizeDerivativeFields(x1, x2, conf_info, calc_options,
594 derivatives_pt.D2);
595
596 resizeDerivativeFields(x1, x2, conf_info, calc_options,
598 derivatives_pt.D12);
599
600 // Reserve space to avoid reallocations during push_back
601 const auto n_dims = static_cast<size_t>(dims.size());
602 if (calc_options & OptionsForGradCalculation::D1)
603 derivatives.D1.reserve(n_dims);
604 if (calc_options & OptionsForGradCalculation::D2)
605 derivatives.D2.reserve(n_dims);
606 if (calc_options & OptionsForGradCalculation::D12)
607 derivatives.D12.reserve(n_dims);
608
609 for (Eigen::Index dim_counter = 0; dim_counter < dims.size();
610 ++dim_counter) {
611 // A later coordinate must not keep the pairs of an earlier one.
612 D1.setZero();
613 D2.setZero();
614 D12.setZero();
615 gpr::Index_t i_dim = *(dims.data() + dim_counter);
616 gpr::Index_t i = ceil(i_dim / 3.) - 1;
617 gpr::Index_t xyz = i_dim - i * 3 - 1;
618
619 // Gradient between moving atoms
620 if (N_mov > 1) {
621 for (Eigen::Index j = 0; j < N_mov; ++j) {
622 if (i != j) {
623 calculateGradientBetweenMovingAtoms(
624 x1, x2, conf_info, lengthScale, xyz, xyz, {i, j},
625 calc_options, {&D1, &D2, &D12}, derivatives_pt,
626 invr2_scratch, deriv2_scratch);
627 }
628 }
629 }
630
631 // Gradient from moving atoms to active frozen atoms
632 if (N_fro > 0) {
633 for (Eigen::Index j = 0; j < N_fro; ++j) {
634 calculateGradientBetweenMovingAndFrozenAtoms(
635 x1, x2, conf_info, lengthScale, xyz, xyz, {i, j},
636 calc_options, {&D1, &D2, &D12}, derivatives_pt,
637 invr2_scratch, deriv2_scratch);
638 }
639 accumulateFarFieldDerivatives(x1, x2, conf_info, lengthScale, i,
640 xyz, xyz, calc_options, D1, D2, D12,
641 derivatives_pt);
642 }
643
644 pushBackDerivativeField(calc_options, OptionsForGradCalculation::D1, D1,
645 derivatives.D1);
646
647 pushBackDerivativeField(calc_options, OptionsForGradCalculation::D2, D2,
648 derivatives.D2);
649
650 pushBackDerivativeField(calc_options, OptionsForGradCalculation::D12,
651 D12, derivatives.D12);
652 }
653}

◆ farFieldRadius()

double aux::Gradient::farFieldRadius ( ) const
inlinenodiscard

Definition at line 72 of file Gradient.h.

73 {
74 return far_field_radius_;
75 }

◆ setFarFieldRadius()

void aux::Gradient::setFarFieldRadius ( double radius)

Same radius convention as Distance::setFarFieldRadius.

Parameters
radiusFar-field radius.

Definition at line 885 of file Gradient.cpp.

886{
887 if (!(radius >= 0.0) || !std::isfinite(radius)) {
888 throw std::invalid_argument(
889 "far-field radius must be finite and non-negative");
890 }
891 far_field_radius_ = radius;
892}

The documentation for this class was generated from the following files: