Skip to content

Conversation

@pelesh
Copy link
Collaborator

@pelesh pelesh commented Feb 24, 2025

Add a definition for a variable type that tracks its dependencies. This dependency tracking is helpful when computing e.g. Jacobian sparsity pattern.

For example, consider function template

            template <typename T>
            void residualFunction(std::vector<T>& f,
                                  const std::vector<T>& x,
                                  const std::vector<T>& p)
            {
                const T y = x[0]*x[1];
                
                f[0] = 10.0*(x[1] - x[0]); 
                f[1] = x[0]*(8.0/3.0 - x[2]) - x[1];
                f[2] = y - 28.0*x[2];
            }

Substituting template parameter GridKit::Sparse::Variable<double> would produce a function that computes vector f such that each element of f contains numerical value and the list of elements of x that are used to compute that value. For example, with values of x set as

  x[0]() = 8.0; x[1]() = 20.0; x[2]() = 2.0/3.0;

f[0] would contain double floating point value 120.0 and indices 0 and 1. See here for more details.

This PR only provides dependency tracking data type and a simple unit test.

CC @sklus

@pelesh pelesh added the enhancement New feature or request label Feb 24, 2025
@pelesh pelesh self-assigned this Feb 24, 2025
@shakedregev
Copy link
Collaborator

Add a definition for a variable type that tracks its dependencies. This dependency tracking is helpful when computing e.g. Jacobian sparsity pattern.

For example, consider function template

            template <typename T>
            void residualFunction(std::vector<T>& f,
                                  const std::vector<T>& x,
                                  const std::vector<T>& p)
            {
                const T y = x[0]*x[1];
                
                f[0] = 10.0*(x[1] - x[0]); 
                f[1] = x[0]*(8.0/3.0 - x[2]) - x[1];
                f[2] = y - 28.0*x[2];
            }

Substituting template parameter GridKit::Sparse::Variable<double> would produce a function that computes vector f such that each element of f contains numerical value and the list of elements of x that are used to compute that value. For example, f[0] would contain double floating point value 120. and indices 0 and 1. See here for more details.

This PR only provides dependency tracking data type and a simple unit test.

CC @sklus

Can you explain why the value is 120? I understand the indices.

Copy link
Collaborator

@shakedregev shakedregev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No major comments and tests pass, so I'm approving. I will branch off this to make some quick fixes.

Copy link
Collaborator

@nkoukpaizan nkoukpaizan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR works as advertised. It will be helpful to expand the documentation of the functionality, so people won't have to read the paper to understand what's going on.

I am also getting many warnings I did not use to see, but I don't think these are related to this PR, but to an earlier one. e.g.,

In file included from /lustre/orion/scratch/nkouk/stf006/Codes/GridKit/tests/UnitTests/PhasorDynamics/runBranchTests.cpp:1:
In file included from /lustre/orion/scratch/nkouk/stf006/Codes/GridKit/tests/UnitTests/PhasorDynamics/BranchTests.hpp:6:
In file included from /lustre/orion/scratch/nkouk/stf006/Codes/GridKit/src/Model/PhasorDynamics/Branch/Branch.hpp:11:
/lustre/orion/scratch/nkouk/stf006/Codes/GridKit/src/Model/PhasorDynamics/Component.hpp:51:22: warning: 'nnz' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]
        virtual IdxT nnz()

In file included from /lustre/orion/scratch/nkouk/stf006/Codes/GridKit/tests/UnitTests/PhasorDynamics/runBranchTests.cpp:1:
In file included from /lustre/orion/scratch/nkouk/stf006/Codes/GridKit/tests/UnitTests/PhasorDynamics/BranchTests.hpp:6:
In file included from /lustre/orion/scratch/nkouk/stf006/Codes/GridKit/src/Model/PhasorDynamics/Branch/Branch.hpp:11:
/lustre/orion/scratch/nkouk/stf006/Codes/GridKit/src/Model/PhasorDynamics/Component.hpp:56:22: warning: 'hasJacobian' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]
        virtual bool hasJacobian()

@pelesh
Copy link
Collaborator Author

pelesh commented Feb 27, 2025

Can you explain why the value is 120? I understand the indices.

Updated PR description to clarify this.

@pelesh pelesh merged commit ee4c1f4 into develop Feb 27, 2025
2 checks passed
@pelesh pelesh deleted the sparsity-pattern-dev branch March 14, 2025 03:36
pelesh added a commit that referenced this pull request Apr 14, 2025
* Add dependency-tracking variable data type.

* Fix compiler warnings.

* Test chain rule with local variable.

---------

Co-authored-by: shakedregev <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants