Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Julia function for processing Equation file. #2

Open
douglowe opened this issue Dec 15, 2023 · 9 comments
Open

Julia function for processing Equation file. #2

douglowe opened this issue Dec 15, 2023 · 9 comments
Assignees

Comments

@douglowe
Copy link
Contributor

douglowe commented Dec 15, 2023

Make sure we can read the equation files into a Julia program

@hidonkey2018 hidonkey2018 self-assigned this Dec 18, 2023
@hidonkey2018
Copy link

Equations can span multiple lines according to KPP files. Therefore, in python version, it read the whole equation file at once.
In Julia version, we try to read the equation line by line. The data will store in IO Buffer. Then we keep checking the IO Buffer data to find out a valid KPP pattern. When it return a pattern match, we can do the equation parse. After parse complete, we then remove that equation from IO buffer.

@hidonkey2018
Copy link

Code different between Python and Julia:
Python index start form 0, Julia index start from 1
Python can use + to contract string. Julia can't use +, you should use *

@hidonkey2018
Copy link

In Python, the scope of a variable declared within a try block extends to the entire function or module, even if the variable is assigned a value within the try block. eg: PyBox variable: stoich (Python does not have block-level scope for variables)

In Julia, variables declared inside a try block are scoped to that block. If you want a variable declared inside a try block to be accessible outside of it, you need to declare the variable before the try block.

@hidonkey2018
Copy link

hidonkey2018 commented Jan 8, 2024

Julia don't have 'collections.defaultdict()' to create a 'collect' with default value. Therefore there is a big change of this this python code:
if equation_step in loss_dict[reactant]:
loss_dict[reactant][equation_step]+=stoich
else:
loss_dict[reactant][equation_step]=stoich

In Julia, we need to use haskey() to check if there is an entry for the given key of the outter dictionary, and if not, we need to create one along with an inner dictionary. We apply similar checking to the inner dictionary to exam the: equatoin_step

Another way to do the smiliar logic is using build-in function: get!(f::Union{Function, Type}, collection, key):
get!(get!(loss_dict, reactant, Dict{Integer, Float64}()), equation_step, stoich)

@hidonkey2018
Copy link

Python's regx has findall() function. However, Julia doesn't has the same thing.
We can do the similar thing by using eachmatch(). The idea is we find out the map match data, loop the data set and get the real match values. Then store them into a collection
This is an example:
collect(m.match for m in eachmatch(r"[-+]?\d*.\d+|\d+|\d+", product))

@hidonkey2018
Copy link

hidonkey2018 commented Jan 11, 2024

Question:

  1. if reactant not in species_dict.values() / if reactant not in species_hess_loss_data , is it checking the data exist in a collection?

  2. if equation_step in gain_dict[reactant]:
    gain_dict[product][equation_step]+=stoich
    should it be if equation_step in gain_dict[product]?
    *The output result is different between gain_dict[reactant] / gain_dict[product]

  3. python's dictionary start from 0. Julia start from 1. And python: species_step start from 0.

@hidonkey2018
Copy link

Pyhton has defaultdict(), eg: {0: defaultdict(None, {0: 'BCARY', 1: 'NO3'})}
The 'None' value assigned to the defaultdict when it's created without specifying a default factory. In this case, when you access a key that doesn't exist, it will return None. However, In Julia build-in library, it doesn't has defaultdict(). If you attemp to access the dictionary with a non-existnet key, it will result in a 'Key Error'. To handle such cases, you can use the get() function or the get!() function with a default value.

There is a library called: DataStructures.jl, that allows specification of a default value to return when a requested key is not in a dictionary.

@hidonkey2018
Copy link

hidonkey2018 commented Feb 23, 2024

Basic Project structure:

Project root
|
|-- src
|    |-- project_source_code.jl  # Project soruce code (function, module) put in here
|    |-- main.jl  # main entry point of the program
|-- test
|    |
|    |-- test_case.jl   # Unit test put in here
|    |-- Manifest.toml
|    |-- Project.toml # Package to run your test
|-- Manifest.toml
|-- Project.toml # Package to run your project

@hidonkey2018
Copy link

Create a unit test case to test the parse equation function
To run the test:

  1. Open the command prompt
  2. Then go inside project/test folder
  3. Execute this command to run the test: julia --project=Project.toml runtest.jl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants