Commit 4d7ed901 authored by HUANG Wei Chih's avatar HUANG Wei Chih
Browse files

replace variable initialization by symbol table API

parent f4f95119
......@@ -254,6 +254,7 @@ create_ode(FunctionList, OdeSystem) :-
% post process of PIVP compilation
create_ode(_, OdeSystem) :-
add_ode_initialization( OdeSystem ),
add_fast_annihilation( OdeSystem ),
remove_symbol( _ ),
fail.
......@@ -471,16 +472,17 @@ replace_symbol( Symbol, AttributeList ) :-
default_symbol_attr( Variable, AttributeList ) :-
\+ number( Variable ),
default_symbol_attr( Variable, no, no, 0, 0, 0, AttributeList ).
default_symbol_attr( Variable, no, no, 0, 0, 0, 0, AttributeList ).
default_symbol_attr( Constant, AttributeList ) :-
number( Constant ),
default_symbol_attr( Constant, yes, no, Constant, Constant, Constant, AttributeList ).
default_symbol_attr( Constant, yes, no, Constant, Constant, Constant, Constant, AttributeList ).
default_symbol_attr( Symbol, Const, Dual, Init, Max, Min,
default_symbol_attr( Symbol, Const, Dual, Value, Init, Max, Min,
[ const: Const,
dual: Dual,
value: Symbol,
name: Symbol,
value: Value,
init: Init,
max: Max,
min: Min
......@@ -522,7 +524,14 @@ new_symbol( Symbol, InitAttributeList ) :-
new_symbol( Symbol, dual, Init ) :-
number( Init ),
!,
new_symbol( Symbol, dual, [ init: Init ] ).
(
Init >= 0
->
P is Init, N is 0
;
P is 0, N is -Init
),
new_symbol( Symbol, dual, [ init: [ P, N ] ] ).
new_symbol( Symbol, dual, InitAttributeList ) :-
list( InitAttributeList ),
......@@ -533,19 +542,47 @@ new_symbol( Symbol, dual, InitAttributeList ) :-
(
Symbol >= 0
->
P = Symbol, N = 0
P is Symbol, N is 0
;
SymbolN is -Symbol,
P = 0, N = SymbolN
P is 0, N is -Symbol
)
;
get_symbol_name( Symbol ),
atom_concat( Symbol, '_p', P ),
atom_concat( Symbol, '_n', N )
),
append( InitAttributeList, [ dual: yes, value: [ P, N ] ], NewInitAttributeList ),
DualAttributeList = [ dual: yes, name: [ P, N ] ],
(
member( [ init: _ ], InitAttributeList )
->
NewDualAttributeList = DualAttributeList
;
append( DualAttributeList, [ init: [ 0, 0 ] ], NewDualAttributeList )
),
append( InitAttributeList, NewDualAttributeList, NewInitAttributeList ),
new_symbol( Symbol, NewInitAttributeList ).
change_symbol_attr( Symbol, ChangeList ) :-
(
symbol( Symbol, AttributeList )
->
SymbolReal = Symbol,
ChangeListReal = ChangeList
;
get_symbol_attr( SymbolReal, [ name: [ Symbol, _ ] ] ),
symbol( SymbolReal, AttributeList ),
(
member( init: Init, ChangeList )
->
get_symbol_attr( SymbolReal, [ init: [ _, NInit ] ] ),
select( init: _, ChangeList, init: [ Init, NInit ], ChangeListReal )
;
ChangeListReal = ChangeList
)
),
replace_attr( AttributeList, ChangeListReal, ResultList ),
replace_symbol( SymbolReal, ResultList ).
replace_attr( AttributeList, [], AttributeList ).
replace_attr( AttributeList, [ Key: Value | ReplaceList ], ResultList ) :-
......@@ -576,48 +613,11 @@ init_variable( OdeSystem, VariableName, Value ) :-
(
Value >= 0
->
init_variable( OdeSystem, VariableName, Value, 0 )
PInit is Value, NInit is 0
;
ValueN is -Value,
init_variable( OdeSystem, VariableName, 0, ValueN )
).
init_variable( OdeSystem, VariableName, PInit, NInit ) :-
% init_variable( +OdeSystem, +VariableName, +PInit, +NInit )
type( OdeSystem, "ode system" ),
type( VariableName, name ),
type( PInit, number ),
type( NInit, number ),
devdoc( "Initialize variable with name \\emph{VariableName} in the \\emph{OdeSystem}." ),
devdoc( "The positive part is initialized to \\emph{PInit}, and the negative part is initialized to \\emph{NInit}." ),
variable( VariableName, [ P, N ] ),
set_ode_initial_value( OdeSystem, P, PInit ),
set_ode_initial_value( OdeSystem, N, NInit ).
% }}}
% get ode initial value{{{
:- devdoc( "\\item get ode initial value" ).
get_ode_initial_value( _, [ ConstantP, ConstantN ], Value ) :-
% get_ode_initial_value( ?OdeSystem, +Variable, -Value )
number( ConstantP ),
number( ConstantN ),
Value = [ ConstantP, ConstantN ].
get_ode_initial_value( OdeSystem, [ P, N ], [ PInit, NInit ] ) :-
% get_ode_initial_value( +OdeSystem, +Variable, -ValueDiff )
variable( _, [ P, N ] ),
item( [ parent: OdeSystem, kind: initial_concentration, item: init( P = PInit ) ] ),
item( [ parent: OdeSystem, kind: initial_concentration, item: init( N = NInit ) ] ).
% this is for documentation
get_ode_initial_value( OdeSystem, Variable, Value ) :-
type( OdeSystem, "ode system" ),
type( Variable, "differential semantic" ),
type( Value, "differential semantic" ),
devdoc( "Get the initial value of variable \\emph{Variable} in the ode system \\emph{OdeSystem}, and store in \\emph{Value}." ),
fail.
PInit is 0, NInit is -Value
),
change_symbol_attr( VariableName, [ init: [ PInit, NInit ] ] ).
% }}}
% add ode{{{
:- devdoc( "\\item add ode" ).
......@@ -667,7 +667,7 @@ get_ode( OdeSystem, [ P, N ], DP, DN ) :-
Here are some functions to handle the variable database." ).
variable( VariableName, VariableDiff ) :-
get_symbol_attr( VariableName, [ value: VariableDiff ] ).
get_symbol_attr( VariableName, [ name: VariableDiff ] ).
variable( [], [], list ).
......@@ -686,7 +686,7 @@ get_variable_positive( VariableName, VariableP ) :-
(
var( VariableName )
->
get_symbol_name( VariableP )
new_symbol( VariableP )
;
variable( VariableName, [ VariableP, _ ] )
).
......@@ -723,7 +723,7 @@ new_variable( OdeSystem, Name ) :-
devdoc( "Generate variable with name \\emph{Name} and set initial value to 0 in the ode system \\emph{OdeSystem}." ),
\+ list( Name ),
new_variable( OdeSystem, Name, 0, 0 ).
new_variable( OdeSystem, Name, 0 ).
new_variable( OdeSystem, [ Name | NameList ] ) :-
% new_variable( +OdeSystem, ?NameList )
......@@ -747,18 +747,6 @@ new_variable( OdeSystem, Name, Value ) :-
new_variable( Name ),
init_variable( OdeSystem, Name, Value ).
new_variable( OdeSystem, Name, PInit, NInit ) :-
% new_variable( +OdeSystem, ?Name, +PInit:int, +NInit:int )
type( OdeSystem, "ode system" ),
type( Name, name ),
type( PInit, number ),
type( NInit, number ),
devdoc( "Generate variable with name \\emph{Name} and set initial value in the ode system \\emph{OdeSystem}." ),
devdoc( "The positive part is initialized to \\emph{PInit}, and the negative part is initialized to \\emph{NInit}." ),
new_variable( Name ),
init_variable( OdeSystem, Name, PInit, NInit ).
% }}}
% condition{{{
:- devdoc( "\\item condition" ).
......@@ -813,11 +801,9 @@ clear_condition( Condition, Name ) :-
nnary_expression_to_ODE( OdeSystem, [], [], Z ) :-
new_variable( Z ),
\+ (
variable( Z, [ ZP, ZN ] ),
get_initial_concentration( ZP, ConcentrationP ),
get_initial_concentration( ZN, ConcentrationN ),
ConcentrationP is 0,
ConcentrationN is 0,
get_symbol_attr( Z, [ init: [ PInit, NInit ] ] ),
PInit is 0,
NInit is 0,
\+ (
get_initial_concentration( Z, Concentration ),
init_variable( OdeSystem, Z, Concentration )
......@@ -948,6 +934,26 @@ condition_expression_to_ODE( OdeSystem, ExpressionX, ExpressionY, X, Y, Z ) :-
condition_expression_to_ODE( OdeSystem, [ ExpressionX, ExpressionY ], [ X, Y ], Z ).
% }}}
% add ode initialization{{{
add_ode_initialization( OdeSystem ) :-
forall(
get_symbol_attr( Variable, [ const: no ] ),
add_ode_initialization( OdeSystem, Variable )
).
add_ode_initialization( OdeSystem, Variable ) :-
get_symbol_attr( Variable, [ name: Name, init: Init ] ),
(
get_symbol_attr( Variable, [ dual: yes ] )
->
Name = [ P, N ],
Init = [ PInit, NInit ],
set_ode_initial_value( OdeSystem, P, PInit ),
set_ode_initial_value( OdeSystem, N, NInit )
;
set_ode_initial_value( OdeSystem, Name, Init )
).
% }}}
% add fast annihilation{{{
:- devdoc( "\\item add fast annihilation" ).
:- devdoc( "Generate fast annihilation ode for a variable.
......@@ -960,7 +966,7 @@ add_fast_annihilation( OdeSystem ) :-
devdoc( "Generate all the fast annihilation ode for variable database and add to the ode system \\emph{OdeSystem}." ),
forall(
get_variable( Variable ),
get_symbol_attr( _, [ const:no, dual:yes, name: Variable ] ),
add_fast_annihilation( OdeSystem, Variable )
).
......@@ -993,15 +999,6 @@ add_fast_annihilation( OdeSystem, Variable, Fast ) :-
devdoc( "Generate fast annihilation ode for variable \\emph{Variable} with fast coefficient \\emph{Fast} and add to the ode system \\emph{OdeSystem}." ),
fail.
% }}}
get_variable( Variable ) :-% {{{
% get_variable( --Variable )
type( Variable, "differential semantic" ),
devdoc( "Get a non constant variable from the database." ),
devdoc( "This is a helper function for 'add_fast_annihilation'." ),
variable( VariableName, Variable ),
name( VariableName ).
% }}}
% is_variable_in_expression( +Expression, +VariableName ){{{
:- devdoc( "\\item is variable in expression" ).
......@@ -1516,7 +1513,6 @@ expression_to_ODE( OdeSystem, X, Z ) :-
!,
unary_expression_to_ODE( OdeSystem, X, T, Z ),
assign_to_PIVP( OdeSystem, T, Z ).
% end variable
% }}}
% this is only for documentation{{{
......@@ -2317,14 +2313,14 @@ hill_to_PIVP( OdeSystem, X, H, NH ) :- % non differential symantic
% hill_to_PIVP( +OdeSystem, +VariableXName, ?VariableHName, ?VariableNHName )
!,
constant( true, Scale ),
init_hill( OdeSystem, Scale, H, NH, G ),
init_hill( Scale, H, NH, G ),
hill_to_ODE( OdeSystem, Scale, G, X, H, NH ).
init_hill( OdeSystem, Scale, H, NH, G ) :-
% init_hill( +OdeSystem, +Scale, ?VariableHName, ?VariableNHName, -VariableGName )
get_symbol_name( H ), set_ode_initial_value( OdeSystem, H, 0 ),
get_symbol_name( NH ), set_ode_initial_value( OdeSystem, NH, Scale ),
get_symbol_name( G ), set_ode_initial_value( OdeSystem, G, 0 ).
init_hill( Scale, H, NH, G ) :-
% init_hill( +Scale, ?VariableHName, ?VariableNHName, -VariableGName )
new_symbol( H, 0 ),
new_symbol( NH, Scale ),
new_symbol( G, 0 ).
hill_to_ODE( OdeSystem, K, G, X, H, NH ) :-
% hill_to_ODE( +OdeSystem, +K, +VariableGName, +VariableXName, +VariableHName, +VariableNHName )
......@@ -2669,10 +2665,10 @@ switch_to_PIVP( OdeSystem, SX, SY, X, Y, Options ) :- % differential semantic
switch_to_PIVP( OdeSystem, SXP, SYP, XP, YP, Options ).
switch_to_PIVP( OdeSystem, SX, SY, X, Y, Options ) :- % non differential semantic
switch_to_PIVP( _, SX, SY, X, Y, Options ) :- % non differential semantic
% switch_to_reaction( +OdeSystem, +VariableSXName, +VariableSYName, +VariableXName, +VariableYName, +Options )
!,
init_switch( OdeSystem, X, Y, B, Options ),
init_switch( X, Y, B, Options ),
switch_core_to_reaction( X, Y, B ),
......@@ -2691,8 +2687,8 @@ switch_to_PIVP( OdeSystem, SX, SY, X, Y, Options ) :- % non differential semanti
switch_control_to_reaction( SX, Y, X, B )
).
init_switch( OdeSystem, X, Y, B, Options ) :-
% init_switch( +OdeSystem, +VariableXName, +VariableYName, +VariableBName, +Options )
init_switch( X, Y, B, Options ) :-
% init_switch( +VariableXName, +VariableYName, +VariableBName, +Options )
(
option( x_init( XInit ), Options )
->
......@@ -2708,10 +2704,9 @@ init_switch( OdeSystem, X, Y, B, Options ) :-
constant( false, YInit )
),
get_symbol_name( B ),
set_ode_initial_value( OdeSystem, X, XInit ),
set_ode_initial_value( OdeSystem, Y, YInit ),
set_ode_initial_value( OdeSystem, B, 0 ).
new_symbol( B, 0 ),
change_symbol_attr( X, [ init: XInit ] ),
change_symbol_attr( Y, [ init: YInit ] ).
switch_core_to_reaction( X, Y, B ) :-
% switch_core_to_reaction( +VariableXName, +VariableYName, +VairableBName )
......@@ -2797,7 +2792,7 @@ zero_order_to_PIVP( OdeSystem, Threshold, SY, X, Y, InitX, InitY, Inverse ) :- %
gk_to_reaction( KcatSX, KmSX, KcatSY, KmSY, SX, SY, X, Y ).
init_zero_order( OdeSystem, Threshold, X, Y, SX, KcatSX, KcatSY, KmSX, KmSY, InitX, InitY, Inverse ) :-
init_zero_order( _, Threshold, X, Y, SX, KcatSX, KcatSY, KmSX, KmSY, InitX, InitY, Inverse ) :-
% init_zero_order( +OdeSystem, +Threshold:int, +VariableXName, +VariableYName, -VariableSXName, -KcatSX:real, -KcatSY:real, -KmSX:real, -KmSY:real, +Inverse:bool )
constant( true, True ),
constant( false, False ),
......@@ -2807,7 +2802,7 @@ init_zero_order( OdeSystem, Threshold, X, Y, SX, KcatSX, KcatSY, KmSX, KmSY, Ini
KmSY is 0.001 * True,
SXInit is Threshold / KcatSX,
get_symbol_name( SX ),
new_symbol( SX, SXInit ),
(
Inverse == true
->
......@@ -2820,18 +2815,17 @@ init_zero_order( OdeSystem, Threshold, X, Y, SX, KcatSX, KcatSY, KmSX, KmSY, Ini
(
InitY == true
->
set_ode_initial_value( OdeSystem, Y, YInit )
change_symbol_attr( Y, [ init: YInit ] )
;
true
),
(
InitX == true
->
set_ode_initial_value( OdeSystem, X, XInit )
change_symbol_attr( X, [ init: XInit ] )
;
true
),
set_ode_initial_value( OdeSystem, SX, SXInit ).
).
gk_to_reaction( KcatSX, KmSX, KcatSY, KmSY, SX, SY, X, Y ) :-
% gk_to_reaction( +KcatSX:real, +KmSX:real, +KcatSY:real, +KmSY:real, +VariableSXName, +VariableSYName, +VariableXName, +VariableYName )
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment