|
ROOT
ROOT project
|
ROOT, but not the particle physics one. Project submission for MATH-458: Programming concepts in scientific computing.
The project bundles a header-only C++ library (libROOT) implementing root-finding algorithms and a CLI application (root_cli) to read and parse input, run the algorithms implemented in libROOT, and write the output to a file of specific format.
The project uses the recommended Canonical Project Structure for C++ projects.
Apart from being divided into a library and a user-facing application/executable, the design on the project is concretely split into four phases.
The CLI application is written (and should be written) within the main function. The main function further calls the Reader, Solver, and Writer classes (in this order) on the input passed through the CLI application.
Reading and parsing is handled by the ReaderBase and FunctionParserBase daughter classes. Adding a new reading method should include writing a new ReaderBase daughter class and adding functionality to parse a new type of function should include writing a new FunctionParserBase daughter class. The information read is stored by the ConfigBase daughter classes (these are data classes to be specific, and can ideally by just structs, but they use some object-oriented features, requiring them to be classes). Adding a new stepper type should include adding a new ConfigBase daughter class. The read method of the ReaderBase daughter classes accept a pointer of the type CLI::App and return a pointer to an object of the type of one of the daughter classes of ConfigBase. The Reader classes further implement helper methods for reading and parsing different things, and functions for constructing the ConfigBase object itself. Similarly, the parse function of the FunctionParser classes takes in a string and returns a C++ function (parses a specific type of function). The classes also include helper methods for parsing functions, and a method (in FunctionParserBase) to infer the type of the function (from the string) and dispatch the appropriate daughter class objects (and methods) to parse the function. In the future, it would make sense to add a wrapper around Reader classes to abstract Config creation and pointer manipulation from the main function (just how it is done by the Solver and Writer classes).
Solving non-linear equation is completely handled by two classes: Solver and StepperBase. StepperBase has specialized child classes for each method (for now: Newton-Raphson, Bisection, Chords, Fixed Point). The Solver class is constructed with the data stored in ConfigBase child classes, and has methods to manage the high-level API involved in solving an equation. The solve method of the Solver class comprises of multiple internal calls, mainly involving convergence check, results saving, instantiating an object of one of the specialized StepperBase child classes, and calling the relevant method to compute single step of the numerical method.
Solver has no child classes but it could be refactored to be child of a SolverBase class (refactoring and abstracting common steps, such as the convergence check and the solve loop). The refactored SolverNonLinear class would inherit all the methods from the abstract class and add arguments for the functions and the boolean to require Aitken's acceleration. The new SolverNonLinear could have child classes for solving single equations (our current Solver) or systems of equations, which would differ just in the type of the arguments saved (e.g. derivative/jacobian for Newton-Raphson). This draft idea, which could be substituted by a fully templated version of the SolverNonLinear class, comes from the fact that templating is already used to define the different kinds of initial guesses allowed, and it is not possible (in C++) to partially specialize different templates. Another more brute-force idea could be to define all the different arguments as matrices and then use them as 1 X 1 matrices (or vectors) for the single equation case, without creating two daughter classes. All of these ideas would have to be adapted for the Stepper classes too.
Solver::solve declares a StepperBase pointer and later instantiates it to point to an object of one of its child class, passing down all the required arguments to use for a single step computation. The only public method executed by the Steppers is compute_step, which computes a single step of the numerical method and returns the results. To allow more numerical methods, it is possible to simply define new child classes with different compute_step algorithms and potentially different arguments to store.
The writing part of the project is handled by two classes: Writer and PrinterBase, with PrinterBase having child classes for each output type. The output type and other relevant information is carried down through the ConfigBase classes (defined by the Readers). Importantly, these classes are not only defined for our specific project, but can write anything correctly passed (potentially with slight refactoring of the code). The classes' methods are implemented just for the type required in our project, but different typed versions would be easy to add.
Writer stores what to write and how, the writing method, and implements a high-level write method to instantiate an object of one of the PrinterBase child classes. PrinterBase has child classes specialized for certain output types, all of which have an overwritten write_values method which prints a given input on a stored output. To allow different writing destinations, new child classes can be defined inheriting from the existing ones.
The required dependencies are included within the project as git submodules and are pinned to specific versions for reproducibility.
CLI11 (v2.6.1): for constructing the CLI interface for the user-facing root_cli application.Eigen3 (v5.0.1): for matric and vector usage / calculations.These can be installed by a user and are not installed through the project's build system.
gnuplot: for plotting resultsgnuplot must be installed before building the project with -DTEST=ON. GoogleTest is installed automatically if the project is built with -DTEST=ON.
GoogleTest (v1.17.0): for all tests.gnuplot: for testing gnuplot related code.These can be installed by a user and are not installed through the project's build system.
doxygen: for generating the documentation.graphviz: for generating hierarchy and flow diagrams in the documentation.To build the project, clone the repository locally:
And build the library and the application using:
The library (libROOT - header-only) and the application (root_cli) can be installed using:
Which will put the library's header files and the application <install_path>/include/libROOT, and <install_path>/bin respectively.
TLDR;
Assuming that root_cli is on your PATH, the program can be executed with:
In order to print out more information about the arguments and the subcommands:
Every additional needed function must be added together with the function to find the root of. Here's a list of examples of possible execution syntax:
CLI input, CLI output, Newton-Raphson method to find the root of x^2-4, starting from initial guess 1 (default tolerance and maximum iterations):
DAT input file called input.dat, DAT output file called output.dat, Bisection method to find the root of x^3-1, with initial interval [-2,2], and verbose output (given tolerance and maximum iterations):
where input.dat is:
CSV input file called input.csv with first row which is a header and "," separating different values, CSV output file called output.csv, Fixed Point Method to find the root of x^2-x, with initial guess 0.5, fixed point function x^2, and verbose output (given tolerance and maximum iterations)::
where input.csv is:
Same as above but with aitken acceleration (will converge faster):
where input.csv is:
CLI input, DAT output file called output.dat, gnuplot writing method (a GNU Plot named output.png is created), Chords method to solve the equation x^3-8 starting from the two initial points 1 and 3:
Input reading is handled by a CLI implemented using CLI11, which passes the read options to the appropriate ReaderBase daughter class. The read method of the ReaderBase daughter classes construct and return a ConfigBase daughter class object. The ReaderBase daughter classes also use the FunctionParserBase daughter classes internally to parse the function (and derivation + g function) inputted by user (string to a C++ function). The information stored in ConfigBase daughter classes is then passed down to the Solver class to run the algorithm.
The solve method of Solver constructs a StepperBase child class object, handles its methods calls, and finally returns the matrix of the results of the computation performed. compute_step method of each StepperBase child class gets the previous iteration and computes and returns the new guess, which will be saved and checked by Solver's methods. The final results returned by solve are then passed down to the Writer class to write them.
The write method of Writer construct a PrinterBase child class object, and handles its methods calls. write_values method of each StepperBase child class gets a certain value to be printed and prints it out in a defined destination.
Tests for the library (libROOT) can be found in libROOT/tests, and the tests for the application (root_cli) can be found in ROOT/tests. We follow test-driven development (TDD), which means that each code changing PR must have tests to validate the changes.
All the following tests and checks run as part of a Continuous Integration pipeline on GitHub Actions (on every PR and push to main).
To build the tests, make sure your submodules are up-to-date (more specifically, the googletest submodule):
And build the code with -DTEST=ON:
To run the tests, use the ctest command:
The project uses clang-tidy to lint C++ and related files (except test files).
One can generate make files with clang-tidy enabled which will enable cmake to run clang-tidy along while compiling the files.
The project also uses a set of pre-commit hooks (including ClangFormat and generic linters and formatters) to perform static analysis and formatting on the code. The hooks can be installed locally using:
This would run the checks every time a commit is created locally. The checks will only run on the files modified by that commit, but the checks can be triggered for all the files using:
If you would like to skip the failing checks and push the code for further discussion, use the --no-verify option with git commit.
The project uses Doxygen to document the code. The project also provides CMake integration for building documentation.
As with the tests, each code change should be accompanied with an updated documentation.
One can build the documentation locally using:
which will write the HTML files to docs/html.
The documentation is automatically built (on every PR) and deployed (on every push to main here - https://saransh-cpp.github.io/ROOT/) to GH Pages using the build-and-deploy-docs workflow.
Most of the limitations and problems can be found as independent issues in the issue tracker on GitHub, or in the previous Project Structure section.