[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: system of differential equations



Dear Ruut,

It seems to me that you should create one Type to simulate a physical
component and not one Type per equation. 

Solving differential equations in a TRNSYS Type is an easy task, but it may
look complex because you have many different ways to do it. I will try to
summarize the available options, but I also recommend you to read the manual
section 3 (especially sections 3.3.2, 3.7 and  3.8).

I also want to draw your attention on a very common problem when people
write their first TRNSYS Types solving differential equations: TRNSYS uses
AVERAGED values on the time step, NOT INSTANTANEOUS values. So if you
compare results from TRNSYS to other programs or hand calculations, you
often have to convert "end-of-time-step" values to averaged values.

First, let's assume you want to solve one first-order differential equation.
You have (at least) 3 options: 

1. Use the DTDT and T arrays
   T and DTDT are managed by the TRNSYS solver and are 
   available to any TRNSYS Types. To use them, you have
   to specify a number of "DERIVATIVES" in the TRNSYS 
   deck. The principle is that you assign values to DTDT in
   your Type and the TRNSYS solver will give you the 
   result of the integration (T). It is a pure numerical
   solution so there is no restriction on the linearity.
   It is probably the easiest approach because you don't 
   have to store values at the end of a time step and get 
   them back at next time step, everything is done for you.
   The downside is that you may have to use small time steps 
   to reach a stable solution in some cases.

2. Use DIFFEQ
   DIFFEQ is a subroutine available to TRNSYS Types, which 
   provides analytical solutions to first-order linear 
   differential equations. It is easy to use if you can 
   formulate your equation as dT/dt=AT+B and it is more 
   accurate than the numerical solution provided by DTDT and T.
   However, you have to store some values in the S array at the 
   end of a time step to use them as initial conditions for the 
   next time step. Since there is no way to know that an iteration 
   will be the last one, you basically store at every iteration 
   and use the stored value only at the first iteration of a new 
   time step. Bottom line: more accurate and generally more stable, 
   but you have a few lines more in your Type to play with the S array. 
   You can have a look to Type 88 for an example (there are 2 
   independent differential equations)

3. Do everything yourself
   If you have an equation like dT/dt= f(T,t) where f is a non-linear
   function, you can approximate the solution as: T(n+1) = g(T(n),T(n+1),t)
   (n refers to a time step). Again, be careful that in TRNSYS T(n) 
   means "average from n-1 to n". 
   You can have the following iterative sequence in your Type:
   - Assume a value for T (e.g. last time step)
   - Compute g
   - Compute T(n+1)
   - Compute g again ...
   Sometimes this will give you more flexibility and a better stability 
   than using T and DTDT because you control the iterative process 
  (for example you can introduce some "damping" in the iterations)

Now, let's go back to the system of differential equations. 

1. Solution 1 (T and DTDT) can be directly expanded to the multi-variable 
   case. You can use several elements in T and DTDT and for example use T(2)
   in the equation for DTDT(4) without any problem.

2. Solution 2 requires some adaptation to be applied to the multi-variable 
   case. You can actually combine DIFFEQ and DTDT to solve a system of 
   first-order linear differential equations. The principle is explained 
   in the manual (see the sections I mentioned before) and is implemented in

   Type 4. The principle is as follows:
   - Solve each equation independently  using DIFFEQ. You give Tinitial and
     you get Tfinal for each variable. 
   - Use (Tfinal-Tinitial) for variable k as DTDT(k) and you let TRNSYS
solve 
     the system of equations, which will give you T(k)
   (If it does not make sense, please have a look to Type 4!)
   That method combines an analytical solution to individual equations to a
   numerical coupling, with the associated potential instabilities.

3. The 3rd method is easily expanded to the multi-variable case. You just
   have to iterate on all variables instead of one. You can obviously couple
   equations by using T(n,i) in the equation for T(n+1,j) (n is the timestep

   and i,j refer to different variables.


Notes:
- The "usual" differential equations in TRNSYS are for temperatures, 
  hence the name of some variables (T and DTDT) but you can solve any
  differential equation of course
- As you mention it yourself, you have even more options because you 
  can linearize differential equations by assuming some coefficients are
  constant and let those "constants" vary during TRNSYS iterations...

Conclusion: There is no "right" solution for your problem, you have to
select the approach that seems easier and more reasonable to you. I would
suggest to start using T and DTDT arrays because it seems to be the easiest
solution. You can still change to another method if you find out that you
cannot reach a stable solution without using unpractical time steps. But
that is just my personal impression!

Regards,

Michaël


> -----Original Message-----
> From: owner-trnsys@relay.doit.wisc.edu 
> [mailto:owner-trnsys@relay.doit.wisc.edu] On Behalf Of Ruut Brandsma
> Sent: Wednesday, March 12, 2003 05:36
> To: Trnsys mailing list (E-mail)
> Subject: system of differential equations
> 
> 
> Dear list members,
> 
> I am building a greenhouse Trnsys-component in Fortran. I made two 
> different implementations for solving a system of differential 
> equations. I would like to have your opinion on what is the best 
> implementation. To explain the two implementations. I will give a 
> simplified example set of differential equations: dTair / dt = A * 
> Tair
> + B * Tcover + C dTcover / dt = D * Tcover + E * Tair + F +
> G(Tcover) where G(Tcover) = c1 + c2 * Tcover + c3 * Tcover^2
> + c4 * Tcover^3 where A, B, C, D, E, F, c1, c2, c3 and c4 are
> numbers.
> 
> Implementation 1:
> Build three component each solving one equation:
> *	Component 1 should contain the line: CALL DIFFEQ(time, 
> A, B * Tcover
> + C, S(index), Tair, dummy)
> *	where Tcover is an input and Tair is an output, S(index) is the
> value of Tair at the end of the previous timestep
> *	Component 2 should contain the line: CALL DIFFEQ(time, 
> D, E * Tair +
> F + G, S(index2), Tcover, dummy)
> *	where G and Tair are inputs, Tcover is an output, 
> S(index2) is the
> value of Tcover at the end of the previous timestep.
> *	G depends on Tcover, is it therefore allowed to pass it 
> as the third
> argument on the CALL to DIFFEQ ? 
> *	Component 3 should contain the line: G = c1 + c2 * Tcover + c3 *
> Tcover^2 + c4 * Tcover^3
> 	where Tcover is an input and G is an output
> 
> Implementation 2 (not implemented yet)
> Build one component solving the three equations:
> Writing the set of equations in matrix format (use courier font if
> necessary) gives:
> /            \   /         \   /      \   /        \
> |  dTair / dt|   |A    B   |   |Tair  |   | C      |
> |dTcover / dt| = |E    D+c2| * |Tcover| + | F + c1 |
> \            /   \         /   \      /   \        /
> in which the G(Tcover) polynomial has been linerealised
> by applying a co-ordinate transformation the 2 by 2 matrix can be 
> diagonalised, which would result in two independent differential 
> equations, which could be solved by calling the DIFFEQ subroutine 
> twice. After that, the reverse co-ordinate transformation is 
> performed.
> 
> The advantage of creating one component is that the IISiBat user has 
> to insert only one greenhouse component to the desktop. If several 
> components have to be inserted the IISiBat user must know exactly how 
> the components are connected to each other.
> 
> In Trnsys each component corresponds to a physical component in 
> reality. As a result each component should be able to work without any 
> other components. In the first implementation the components depend on 
> each other in such a way that they could not work on their own. This 
> is not in line with the Trnsys perspective.
> 
> I would really appreciate it, if I could receive some feedback.
> 
> 
> With kind regards,
> 
> Ruut Brandsma
> Modelling specialist
> Ecofys b.v
> Our Mission "A sustainable energy supply for everyone"
> ---------------------------------------------
> P.O. Box 8408
> NL-3503 RK Utrecht
> Kanaalweg 16-G
> NL- 3526 KL Utrecht
> The Netherlands
> ---------------------------------------------
> tel. +31 (0)30 280 84 35
> cell.+31 (0)6 1500 22 05
> fax +31 (0)30 280 83 01
> email R.Brandsma@ecofys.nl
> ---------------------------------------------
> Visit our Homepage!	 		http://www.ecofys.com
> Monitor your Energy Usage!		http://www.Enerlyser.com
> Make use of Solar Energy!		http://www.BeldeZon.nl
> Green energy in Europe		http://www.greenprices.com 
> 2002: the year of the urban turbine!	http://www.urbanturbines.com 
> For a sunny choice:			http://www.mysolar.com  
>  
>