Skip to content

Commit

Permalink
Merge pull request #29 from ISSMteam/train_only_with_data
Browse files Browse the repository at this point in the history
Move all the default lower and upper bound to contant.py , add a dumm…
  • Loading branch information
Cheng Gong authored May 15, 2024
2 parents d3be2d4 + 0a673e1 commit b52c04b
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 7 deletions.
4 changes: 3 additions & 1 deletion pinnicle/physics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .constants import Constants
from .equationbase import *
from .physics import *
from .stressbalance import *

from .continuity import *
from .dummy import *
from .stressbalance import *
8 changes: 8 additions & 0 deletions pinnicle/physics/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ def __init__(self, **kwargs):
self.g = 9.81 # gravitational force (m/s^2)
self.yts = 3600.0*24*365 # year to second (s)

# Typical range of variables
self.variable_lb = {'u': -1.0e4/self.yts, 'v':-1.0e4/self.yts, 's':-1.0e3, 'H':10.0, 'C':0.01, 'a': -5.0/self.yts}
self.variable_ub = {'u': 1.0e4/self.yts, 'v':1.0e4/self.yts, 's':2.5e3, 'H':2500.0, 'C':1.0e4, 'a': 5.0/self.yts}
# add more if needed
self.variable_lb['u_base'] = -1.0e4/self.yts
self.variable_ub['u_base'] = 1.0e4/self.yts
self.variable_lb['v_base'] = -1.0e4/self.yts
self.variable_ub['v_base'] = 1.0e4/self.yts
4 changes: 2 additions & 2 deletions pinnicle/physics/continuity.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def __init__(self, param_dict={}):
def set_default(self):
self.input = ['x', 'y']
self.output = ['u', 'v', 'a', 'H']
self.output_lb = [-1.0e4/self.yts, -1.0e4/self.yts, -5.0/self.yts, 10.0]
self.output_ub = [ 1.0e4/self.yts, 1.0e4/self.yts, 5/self.yts, 2500.0]
self.output_lb = [self.variable_lb[k] for k in self.output]
self.output_ub = [self.variable_ub[k] for k in self.output]
self.data_weights = [1.0e-8*self.yts**2, 1.0e-8*self.yts**2, 1.0*self.yts**2, 1.0e-6]
self.residuals = ["f"+self._EQUATION_TYPE]
self.pde_weights = [1.0e8]
Expand Down
55 changes: 55 additions & 0 deletions pinnicle/physics/dummy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import deepxde as dde
from . import EquationBase, Constants
from ..parameter import EquationParameter

class DummyEquationParameter(EquationParameter, Constants):
""" default parameters for Dummy PDE
"""
_EQUATION_TYPE = 'DUMMY'
def __init__(self, param_dict={}):
# load necessary constants
Constants.__init__(self)
super().__init__(param_dict)

def set_default(self):
self.input = ['x', 'y']
self.output = []
self.output_lb = []
self.output_ub = []
self.data_weights = []
self.residuals = []
self.pde_weights = []
self.scalar_variables = {}

def update(self):
""" set all the weights to 1, and load all the lb and ub is not given
"""
self.data_weights = [1.0 for ou in self.output]
if not self.output_lb:
self.output_lb = [self.variable_lb[k] for k in self.output]
if not self.output_ub:
self.output_ub = [self.variable_ub[k] for k in self.output]

def check_consistency(self):
""" output can not be empty
"""
if len(self.output) != len(self.output_lb):
raise ValueError("The size of the output is not consistent with size of the lower bound")
if len(self.output) != len(self.output_ub):
raise ValueError("The size of the output is not consistent with size of the upper bound")

class Dummy(EquationBase): #{{{
"""
"""
_EQUATION_TYPE = 'DUMMY'
def __init__(self, parameters=DummyEquationParameter()):
super().__init__(parameters)

def pde(self, nn_input_var, nn_output_var):
""" Dummy PDE returns nothing, to train the NN with data only
Args:
nn_input_var: global input to the nn
nn_output_var: global output from the nn
"""
return [] #}}}
8 changes: 4 additions & 4 deletions pinnicle/physics/stressbalance.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def __init__(self, param_dict={}):
def set_default(self):
self.input = ['x', 'y']
self.output = ['u', 'v', 's', 'H', 'C']
self.output_lb = [-1.0e4/self.yts, -1.0e4/self.yts, -1.0e3, 10.0, 0.01]
self.output_ub = [ 1.0e4/self.yts, 1.0e4/self.yts, 2.5e3, 2000.0, 1.0e4]
self.output_lb = [self.variable_lb[k] for k in self.output]
self.output_ub = [self.variable_ub[k] for k in self.output]
self.data_weights = [1.0e-8*self.yts**2.0, 1.0e-8*self.yts**2.0, 1.0e-6, 1.0e-6, 1.0e-8]
self.residuals = ["f"+self._EQUATION_TYPE+"1", "f"+self._EQUATION_TYPE+"2"]
self.pde_weights = [1.0e-10, 1.0e-10]
Expand Down Expand Up @@ -96,8 +96,8 @@ def __init__(self, param_dict={}):
def set_default(self):
self.input = ['x', 'y']
self.output = ['u', 'v', 'u_base', 'v_base', 's', 'H', 'C']
self.output_lb = [-1.0e4/self.yts, -1.0e4/self.yts, -1.0e4/self.yts, -1.0e4/self.yts, -1.0e3, 10.0, 0.01]
self.output_ub = [ 1.0e4/self.yts, 1.0e4/self.yts, 1.0e4/self.yts, 1.0e4/self.yts, 2.5e3, 2000.0, 1.0e4]
self.output_lb = [self.variable_lb[k] for k in self.output]
self.output_ub = [self.variable_ub[k] for k in self.output]
self.data_weights = [1.0e-8*self.yts**2.0, 1.0e-8*self.yts**2.0, 1.0e-8*self.yts**2.0, 1.0e-8*self.yts**2.0, 1.0e-6, 1.0e-6, 1.0e-8]
self.residuals = ["f"+self._EQUATION_TYPE+" 1", "f"+self._EQUATION_TYPE+" 2", "f"+self._EQUATION_TYPE+" base 1", "f"+self._EQUATION_TYPE+" base 2"]
self.pde_weights = [1.0e-10, 1.0e-10, 1.0e-10, 1.0e-10]
Expand Down
16 changes: 16 additions & 0 deletions tests/test_physics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def test_Constants():
assert phy.equations[0].rhow == 1023.0
assert phy.equations[0].g == 9.81
assert phy.equations[0].yts == 3600.0*24*365
assert phy.equations[0].variable_lb.keys() == phy.equations[0].variable_ub.keys()
assert all([phy.equations[0].variable_lb[k] < phy.equations[0].variable_ub[k] for k in phy.equations[0].variable_lb])

def test_update_cid():
p = SSAEquationParameter({"scalar_variables":{"B":1}})
Expand Down Expand Up @@ -158,4 +160,18 @@ def test_operator():
assert phy.operator('SSA')
assert phy.operator('ssa')

def test_Physics_dummy():
dummy = {}
dummy["output"] = ['u', 's', 'C']
hp = {}
hp["equations"] = {"DUMMY":dummy}
phy = Physics(PhysicsParameter(hp))

assert phy.input_var == ['x', 'y']
assert phy.output_var == ['u', 's', 'C']
assert phy.residuals == []
assert phy.equations[0].local_output_var == {'u': 0, 's': 1, 'C': 2}
assert len(phy.output_lb) == 3
assert len(phy.output_ub) == 3
assert len(phy.data_weights) == 3
assert len(phy.pde_weights) == 0
16 changes: 16 additions & 0 deletions tests/test_pinn.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,27 @@ def test_save_and_load_setting(tmp_path):
experiment2 = pinn.PINN(loadFrom=tmp_path)
assert experiment.params.param_dict == experiment2.params.param_dict

def test_train_only_data(tmp_path):
hp["is_parallel"] = False
hp["is_save"] = False
hp["num_collocation_points"] = 100
issm["data_size"] = {"u":100, "v":100, "s":100, "H":100}
hp["num_neurons"] = [4,10];
hp["data"] = {"ISSM": issm}
dummy = {}
dummy["output"] = ['v', 'H']
hp["equations"] = {"DUMMY":dummy}
experiment = pinn.PINN(params=hp)
experiment.compile()
experiment.train()
assert experiment.loss_names == ['v', 'H']

def test_train(tmp_path):
hp["is_save"] = False
hp["num_collocation_points"] = 100
issm["data_size"] = {"u":100, "v":100, "s":100, "H":100, "C":None, "vel":100}
hp["data"] = {"ISSM": issm}
hp["equations"] = {"SSA":SSA}
experiment = pinn.PINN(params=hp)
experiment.compile()
experiment.train()
Expand Down

0 comments on commit b52c04b

Please sign in to comment.