WassimTenachi / PhySO

Physical Symbolic Optimization
https://physo.readthedocs.io/
MIT License
1.81k stars 249 forks source link

How to add new units? #28

Closed BoqianBIT closed 1 year ago

BoqianBIT commented 1 year ago

Hello! Thank you very much for the excellent work! I am recently studying your code and plan to apply it to scientific problems, but I am now encountering a problem with the units:

Apart from the existing L, T, M units, how can I add new units (like temperature K) to make the program applicable to solve thermodynamic or aerodynamic problems? and how to represent it in the dict "library_config" ?

WassimTenachi commented 1 year ago

Hi @Jamie-Z,

Thank you !

You can simply add it to the units vector ! The units vector can be of any length you want depending on your needs. In my example I only use 3 dimensions length, time and mass but you can use up to 7 as long as the length is consistent across X,y and constants.

Here is a practical example : In your case adding a third temperature variable and a Boltzmann constant of free value to the mechanical energy example could look like this (with length being the 1st value, time the 2nd, mass the 3rd and temperature the 4th).

args_make_tokens = {
                # operations
                "op_names"             : ["mul", "add", "sub", "div", "inv", "n2", "sqrt", "exp", "log", "sin", "cos"],
                "use_protected_ops"    : True,
                # input variables
                "input_var_ids"        : {"z" : 0            , "v" : 1             , "T" : 2            },
                "input_var_units"      : {"z" : [1, 0, 0, 0] , "v" : [1, -1, 0, 0] , "T" : [0, 0, 0, 1] },
                "input_var_complexity" : {"z" : 1.           , "v" : 1.            , "T" : 1            },
                # constants
                "constants"            : {"1" : const1       , },
                "constants_units"      : {"1" : [0, 0, 0, 0] , },
                "constants_complexity" : {"1" : 1.           , },
                # free constants
                "free_constants"            : {"m"                 , "g"                 , "kb"                 ,},
                "free_constants_init_val"   : {"m" : 1.            , "g" : 1.            , "kb" : 1.            ,},
                "free_constants_units"      : {"m" : [0, 0, 1, 0]  , "g" : [1, -2, 0, 0] , "kb" : [2, -2, 1, 1] ,},
                "free_constants_complexity" : {"m" : 1.            , "g" : 1.            , "kb" : 1.            ,},
                    }

library_config = {"args_make_tokens"  : args_make_tokens,
                  "superparent_units" : [2, -2, 1, 0],
                  "superparent_name"  : "E",
                }

Please tell me if that answers your question !

Cheers !

Wassim

PS: For dimensional analysis purposes the order in which you give your units (eg. L, T, M or M, T, L etc.) has no importance so you can feed them in any order you want when running physo as long as you order them consistently (across X, y and constants).

WassimTenachi commented 1 year ago

Can you please tell me if that answers your question @Jamie-Z so I can close this issue ? :) Thanks !

BoqianBIT commented 1 year ago

Sorry for the late reply. Thank you very much for your explanation! Now I am able to customize new units to solve problems in my field.

Thank you again!

WassimTenachi commented 1 year ago

Great ! Don't hesitate to re-open this issue if you need further help !