leavesandlemmas / stardew-tree-sim

A simulation and mathematical of tree dynamics, based on stardew valley.
1 stars 0 forks source link

Questions about the c++ implementation #3

Closed leavesandlemmas closed 3 months ago

leavesandlemmas commented 3 months ago

@KedoKudo I finally have time to ask some questions about the C++ implementation / code,

First some questions about files:

  1. What does the .clang-format file do? This was generated automatically? How ?
  2. What does CMakeLists.txt do?
  3. I understand that the environment.yml file specifies a python environment (libraries used here in), but what's its purpose? Was this file generated automatically?
  4. I understand the purpose of .gitignore; just learned that at the new job. How did you generate this one? I am guessing you copied one from another project?

Questions about the implementation in main.cpp

So you declare the parameters as a struct , then update the struct with a json file?

  1. What's the purpose of the forward declarations? Is that a style thing ? Basically a list of functions and their types?
  2. Several of the headers like tbb.h and H5Cpp.h aren't in the c++ standard library right? My compiler (MinGW-W64 x86_64-msvcrt-posix-seh,13.1.0) fails to compile.
  3. Did you write comments to work with doxygen? I learned about that here, I was wondering what the comments like:
    /**
    * @brief Main function to run the simulation
    */

    are for.

Could you explain or point me to a reference for the syntax here? I know that auto automatically deduces the correct type.

 // Set all cells to zero initially
  for (auto& slice : grid) {
    for (auto& row : slice) {
      std::fill(row.begin(), row.end(), 0);
    }
  }

Or is this some notation for std::vector ?

I will probably have more later (or follow ups).

KedoKudo commented 3 months ago

That's a lot of questions, I will try to answer them one at a time.

KedoKudo commented 3 months ago

What does the .clang-format file do? This was generated automatically? How ?

This is a configuration file for a tool called clang-format (https://clang.llvm.org/docs/ClangFormat.html). It can automatically format your c++ source code so that you don't have to worry about keeping the source code clean and easy to read.

KedoKudo commented 3 months ago

What does CMakeLists.txt do?

This is a configuration file for a build system called CMake (https://cmake.org). When building application that needs to be cross-platform, we like to use CMake to generate the build configurations (such as a Makefile) that is suitable for the targeting system. It also help handles the libraries dependencies as well as running tests (as part of the development cycle).

KedoKudo commented 3 months ago

I understand that the environment.yml file specifies a python environment (libraries used here in), but what's its purpose? Was this file generated automatically?

In our group, we like to use conda to create a sandboxed environment to avoid dependencies issues that often plagues the development process. This file is manually generated based on the software needs, and can be used to create a conda environment with a command like conda env create -f environment.yml (on UNIX-like system). This is very handy when you are working on multiple projects with different (if not conflicting) dependencies.

KedoKudo commented 3 months ago

I understand the purpose of .gitignore; just learned that at the new job. How did you generate this one? I am guessing you copied one from another project?

Depending on the project, I often ask my developers to use Github's template for single language project. For more complicated ones, we like to use https://www.toptal.com/developers/gitignore/.

KedoKudo commented 3 months ago

So you declare the parameters as a struct , then update the struct with a json file?

Basically yes, the struct data structure helps organize simulation configuration, making it easier to be updated with an input file as you requested.

KedoKudo commented 3 months ago

What's the purpose of the forward declarations? Is that a style thing ? Basically a list of functions and their types?

When compiler is trying to compile a source code into an object, it is reading the source code from top to bottom. Forward declaration basically tells the compiler that I have this function defined somewhere else, so just trust me when you encounter them later in this file even if you haven't seen it. Without it, you will have to make sure the function is fully defined in the source code before you can call them.

KedoKudo commented 3 months ago

Several of the headers like tbb.h and H5Cpp.h aren't in the c++ standard library right? My compiler (MinGW-W64 x86_64-msvcrt-posix-seh,13.1.0) fails to compile.

You need to use the environment.yml to create a sandboxed development environment, and conda will install both tbb (thread building block, feature from intel's one api) and HDF5.

leavesandlemmas commented 3 months ago

So if a function is declared, then called but not yet defined, the compiler will still fill in the correction definition?

KedoKudo commented 3 months ago

Did you write comments to work with doxygen? I learned about that here, I was wondering what the comments like:

Yes, this will help with the documentation generation where the API documentation can be generated automatically

KedoKudo commented 3 months ago

So if a function is declared, then called but not yet defined, the compiler will still fill in the correction definition?

Yes, the compiler will trust the developer that the function will be defined somewhere and will be made available during runtime

KedoKudo commented 3 months ago

Could you explain or point me to a reference for the syntax here? I know that auto automatically deduces the correct type.

Yes, auto is the modern c++ way to simplify the complex c++ syntax. If you learn c++ in the 90s or early 2000, you should really read up on modern c++ syntax. Here is a good starting point: https://github.com/AnthonyCalandra/modern-cpp-features

KedoKudo commented 3 months ago

for (auto& slice : grid):

KedoKudo commented 3 months ago

I think I have answer all the questions there are for the moment. Ping me if you have any additional questions.

leavesandlemmas commented 3 months ago

Could you explain or point me to a reference for the syntax here? I know that auto automatically deduces the correct type.

Yes, auto is the modern c++ way to simplify the complex c++ syntax. If you learn c++ in the 90s or early 2000, you should really read up on modern c++ syntax. Here is a good starting point: https://github.com/AnthonyCalandra/modern-cpp-features

I dont think ever I really learned c++ syntax... just bits and pieces from reading code and messing around with it.

leavesandlemmas commented 3 months ago

Thanks @KedoKudo Ill make a new issue if i have more questions.