11"""
2- Copyright 2019 Marco Dal Molin et al.
2+ Copyright 2020 Marco Dal Molin et al.
33
44Licensed under the Apache License, Version 2.0 (the "License");
55you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@ class BaseElement():
3737 """
3838 Number of downstream elements
3939 """
40-
40+
4141 _num_upstream = None
4242 """
4343 Number of upstream elements
@@ -469,7 +469,7 @@ class ODEsElement(StateParameterizedElement):
469469
470470 dS/dt = input - output
471471 """
472-
472+
473473 _num_upstream = 1
474474 """
475475 Number of upstream elements
@@ -485,7 +485,25 @@ class ODEsElement(StateParameterizedElement):
485485 List of states used by the solver of the differential equation
486486 """
487487
488- def __init__ (self , parameters , states , solver , id ):
488+ _fluxes = []
489+ """
490+ This attribute contains a list of methods (one per differential equation)
491+ that calculate the values of the fluxes needed to solve the differential
492+ equations that control the element. The single functions must return the
493+ fluxes as a list where incoming fluxes are positive and outgoing are
494+ negative. Here is a list of the required outputs of the single functions:
495+
496+ list(floats)
497+ Values of the fluxes given states, inputs, and parameters.
498+ float
499+ Minimum value of the state. Used, sometimes, by the numerical solver
500+ to search for the solution.
501+ float
502+ Maximum value of the state. Used, sometimes, by the numerical solver
503+ to search for the solution.
504+ """
505+
506+ def __init__ (self , parameters , states , approximation , id ):
489507 """
490508 This is the initializer of the abstract class ODEsElement.
491509
@@ -498,10 +516,8 @@ def __init__(self, parameters, states, solver, id):
498516 states : dict
499517 Initial states of the element. Depending on the element the states
500518 can be either a float or a numpy.ndarray.
501- solver : superflexpy.utils.root_finder.RootFinder
502- Solver used to find the root(s) of the differential equation(s).
503- Child classes may implement their own solver, therefore the type
504- of the solver is not enforced.
519+ approximation : superflexpy.utils.numerical_approximation.NumericalApproximator
520+ Numerial method used to approximate the differential equation
505521 id : str
506522 Identifier of the element. All the elements of the framework must
507523 have an id.
@@ -510,7 +526,7 @@ def __init__(self, parameters, states, solver, id):
510526 StateParameterizedElement .__init__ (self , parameters = parameters ,
511527 states = states , id = id )
512528
513- self ._solver = solver
529+ self ._num_app = approximation
514530
515531 def set_timestep (self , dt ):
516532 """
@@ -534,7 +550,7 @@ def get_timestep(self):
534550 """
535551 return self ._dt
536552
537- def define_solver (self , solver ):
553+ def define_numerical_approximation (self , approximation ):
538554 """
539555 This method define the solver to use for the differential equation.
540556
@@ -546,7 +562,7 @@ def define_solver(self, solver):
546562 of the solver is not enforced.
547563 """
548564
549- self ._solver = solver
565+ self ._num_app = approximation
550566
551567 def _solve_differential_equation (self , ** kwargs ):
552568 """
@@ -559,29 +575,20 @@ def _solve_differential_equation(self, **kwargs):
559575 message = '{}the attribute _solver_states must be filled' .format (self ._error_message )
560576 raise ValueError (message )
561577
562- self .state_array = self ._solver .solve (fun = self ._differential_equation ,
563- S0 = self ._solver_states ,
564- dt = self ._dt ,
565- ** self .input ,
566- ** {k [len (self ._prefix_parameters ):]: self ._parameters [k ] for k in self ._parameters },
567- ** kwargs )
568-
569- def _differential_equation (self ):
570- """
571- To be implemented by any child class. This method sets the differential
572- equation(s) to be solved by the solver. The method must be implemented
573- in order to satisfy the requirements of the solver.
574- """
575-
576- raise NotImplementedError ('The _differential_equation method must be implemented' )
578+ self .state_array = self ._num_app .solve (fun = self ._fluxes ,
579+ S0 = self ._solver_states ,
580+ dt = self ._dt ,
581+ ** self .input ,
582+ ** {k [len (self ._prefix_parameters ):]: self ._parameters [k ] for k in self ._parameters },
583+ ** kwargs )
577584
578585 def __copy__ (self ):
579586 p = self ._parameters # Only the reference
580587 s = deepcopy (self ._states ) # Create a new dictionary
581588 ele = self .__class__ (parameters = p ,
582589 states = s ,
583590 id = self .id ,
584- solver = self ._solver )
591+ approximation = self ._num_app )
585592 ele ._prefix_states = self ._prefix_states
586593 ele ._prefix_parameters = self ._prefix_parameters
587594 return ele
@@ -592,7 +599,7 @@ def __deepcopy__(self, memo):
592599 ele = self .__class__ (parameters = p ,
593600 states = s ,
594601 id = self .id ,
595- solver = self ._solver )
602+ approximation = self ._num_app )
596603 ele ._prefix_states = self ._prefix_states
597604 ele ._prefix_parameters = self ._prefix_parameters
598605 return ele
@@ -728,7 +735,7 @@ def _solve_lag(weight, lag_state, input):
728735 """
729736 This method distributes the input fluxes according to the weight array
730737 and the initial state.
731-
738+
732739 Parameters
733740 ----------
734741 weight : list(numpy.ndarray)
@@ -737,7 +744,7 @@ def _solve_lag(weight, lag_state, input):
737744 List of the initial states of the lag.
738745 input : list(numpy.ndarray)
739746 List of fluxes
740-
747+
741748 Returns
742749 -------
743750 numpy.ndarray
@@ -761,12 +768,12 @@ def _init_lag_state(self, lag_time):
761768 """
762769 This method sets the initial state of the lag to arrays of proper
763770 length.
764-
771+
765772 Parameters
766773 ----------
767774 lag_time : list(float)
768775 List of lag times
769-
776+
770777 Returns
771778 -------
772779 list(numpy.ndarray)
0 commit comments