Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #5779 > unrolled thread

Clean up code to run faster

Started byMichelle Maul <michellemaul312@gmail.com>
First post2011-10-22 10:10 +0000
Last post2011-10-29 11:12 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Clean up code to run faster Michelle Maul <michellemaul312@gmail.com> - 2011-10-22 10:10 +0000
    Re: Clean up code to run faster Ray Koopman <koopman@sfu.ca> - 2011-10-28 09:43 +0000
    Re: Clean up code to run faster A Retey <awnl@gmx-topmail.de> - 2011-10-29 11:12 +0000

#5779 — Clean up code to run faster

FromMichelle Maul <michellemaul312@gmail.com>
Date2011-10-22 10:10 +0000
SubjectClean up code to run faster
Message-ID<j7u4rb$t67$1@smc.vnet.net>
Hello Mathematica friends,

I am new to Mathematica and I have learned that it is a steep learning
curve, I've spent a week trying to program an example problem from a
heat transfer book.  I need it to solve for several hundred time steps
and my computer is only allowing me to do about 15 in an hour of
solving.  How can I clean up my code and what are some general tips
that you can give me for the future when I need to do more hard core
iteration problems and stuff?  PS, I'm a mechanical engineer so I'm
not very good at understanding how Mathematica thinks in computer
language (anything is helpful).

I am solving for the temperatures on the inside and outside of the
wall while the sun is heating the outside at certain times of the day.

<< AutomaticUnits`;

masonry = {WallThickness -> 1. Foot, \[Alpha] -> 4.78*10^-6 Foot^2/
Second,
   k -> 0.4 BTU/(Hour*Foot*Fahrenheit), \[Kappa] -> 0.77,
   Subscript[h, out] -> .7 BTU/(Hour*Foot^2*Fahrenheit),
   Subscript[T, in] -> 70 Fahrenheit,
   Subscript[h, in] ->
    1.8 BTU/(Hour*Foot^2*Fahrenheit), \[CapitalDelta]x -> .2 Foot,
   Subscript[T, out, initial] -> 30 Fahrenheit};

Subscript[q, solar] =
  Piecewise[{{114 BTU/(Hour*Foot^2), 0 <= x <= 12}, {242 BTU/
(Hour*Foot^2),
     12 < x <= 24}, {178 BTU/(Hour*Foot^2), 24 < x <= 36}}];
Subscript[T, out] =
  Piecewise[{{33 Fahrenheit, 0 <= x <= 12}, {43 Fahrenheit,
     12 < x <= 24}, {45 Fahrenheit, 24 < x <= 36}, {37 Fahrenheit,
     36 < x <= 48}, {32 Fahrenheit, 48 < x <= 60}, {27 Fahrenheit,
     60 < x <= 72}, {26 Fahrenheit, 72 < x <= 84}, {25 Fahrenheit,
     84 < x <= 96}}];

Plot[{Subscript[q, solar], Subscript[T, out]}, {x, 0, 96}]


Nodes = (WallThickness/\[CapitalDelta]x + 1) /.
   masonry;  (*Number of Nodes for analysis*)


*Nodes will be numbered from 0 to 5 with Node 0 on the inside surface
of the wall and Node 5 on the exterior surface of the wall*


Initial Conditions are found by the assumption that the temperature
varies linearly inside the wall.

Temperatures are given in the format:  T[node# , time step]   (ex
T[0,1] is the interior node at time i=1)


tempstep = (Subscript[T, in] - Subscript[T, out, initial])/(Nodes -
1) /.
   masonry; (* Temperature change between each node *)

T[0, 0] = Subscript[T, in] /. masonry; T[1, 0] = T[0, 0] - tempstep;
T[2, 0] = T[1, 0] - tempstep; T[3, 0] = T[2, 0] - tempstep;
T[4, 0] = T[3, 0] - tempstep; T[5, 0] = T[4, 0] - tempstep;


EQUATIONS


(* Node 0 has convection, can use Eqn 5-51 from book *)

T[0, t_] := (1 - 2 \[Tau] - (2 \[Tau] Subscript[h, in] \
[CapitalDelta]x)/k) T[
      0, t - 1] +
    2 \[Tau] T[1, t - 1] + (2 \[Tau] Subscript[h, in] \
[CapitalDelta]x)/k*
     Subscript[T, in] /. masonry;


(* Nodes 1-4 take the more general form, use Eqn 5-47 *)

T[m_, t_] := \[Tau] (T[m - 1, t - 1] + T[m + 1, t - 1]) + (1 - 2 \
[Tau]) T[m,
      t - 1] /. masonry;


(* Node 5 is exposed to convection and heat flux outside *)

T[5, t_] := (1 - 2 \[Tau] -
       2 \[Tau] (Subscript[h, out] \[CapitalDelta]x)/k) T[5, t - 1] +
    2 \[Tau] T[4, t - 1] +
    2 \[Tau] (Subscript[h, out] \[CapitalDelta]x)/
     k*(Subscript[T, out] /. {x -> t}) +
    2 \[Tau] (\[Kappa] *(Subscript[q, solar] /. {x -> t}) \
[CapitalDelta]x)/
     k /. masonry;


\[CapitalDelta]t = .25 Hour;

\[Tau] = 0.10755

T[0, t_] := (1 - 2 \[Tau] - (2 \[Tau] Subscript[h, in] \
[CapitalDelta]x)/k) T[
      0, t - 1] +
    2 \[Tau] T[1, t - 1] + (2 \[Tau] Subscript[h, in] \
[CapitalDelta]x)/k*
     Subscript[T, in] /. masonry;

T[m_, t_] := \[Tau] (T[m - 1, t - 1] + T[m + 1, t - 1]) + (1 - 2 \
[Tau]) T[m,
      t - 1] /. masonry;

T[5, t_] := (1 - 2 \[Tau] -
       2 \[Tau] (Subscript[h, out] \[CapitalDelta]x)/k) T[5, t - 1] +
    2 \[Tau] T[4, t - 1] +
    2 \[Tau] (Subscript[h, out] \[CapitalDelta]x)/
     k*(Subscript[T, out] /. {x -> t}) +
    2 \[Tau] (\[Kappa] *(Subscript[q, solar] /. {x -> t}) \
[CapitalDelta]x)/
     k /. masonry;

Transpose[Table[T[n, x], {n, 0, 5}, {x, 0, 100}]] // MatrixForm


Thank you for taking the time to look at this

[toc] | [next] | [standalone]


#5965

FromRay Koopman <koopman@sfu.ca>
Date2011-10-28 09:43 +0000
Message-ID<j8dtfp$kft$1@smc.vnet.net>
In reply to#5779
"Memoize" your function definitions: instead  of

T[0 , t_] := ... ;
T[m_, t_] := ... ;
T[5 , t_] := ... ;

use

T[0 , t_] := T[0,t] = ... ;
T[m_, t_] := T[m,t] = ... ;
T[5 , t_] := T[5,t] = ... ;

Then the table generation is virtually instantaneous.

On Oct 22, 3:10 am, Michelle Maul <michellemaul...@gmail.com> wrote:
> Hello Mathematica friends,
>
> I am new to Mathematica and I have learned that it is a steep learning
> curve, I've spent a week trying to program an example problem from a
> heat transfer book.  I need it to solve for several hundred time steps
> and my computer is only allowing me to do about 15 in an hour of
> solving.  How can I clean up my code and what are some general tips
> that you can give me for the future when I need to do more hard core
> iteration problems and stuff?  PS, I'm a mechanical engineer so I'm
> not very good at understanding how Mathematica thinks in computer
> language (anything is helpful).
>
> I am solving for the temperatures on the inside and outside of the
> wall while the sun is heating the outside at certain times of the day.
>
> << AutomaticUnits`;
>
> masonry= {WallThickness -> 1. Foot, \[Alpha] -> 4.78*10^-6 Foot^2/
> Second,
>    k -> 0.4 BTU/(Hour*Foot*Fahrenheit), \[Kappa] -> 0.77,
>    Subscript[h, out] -> .7 BTU/(Hour*Foot^2*Fahrenheit),
>    Subscript[T, in] -> 70 Fahrenheit,
>    Subscript[h, in] ->
>     1.8 BTU/(Hour*Foot^2*Fahrenheit), \[CapitalDelta]x -> .2 Foot,
>    Subscript[T, out, initial] -> 30 Fahrenheit};
>
> Subscript[q, solar] =
>   Piecewise[{{114 BTU/(Hour*Foot^2), 0 <= x <= 12}, {242 BTU/
> (Hour*Foot^2),
>      12 < x <= 24}, {178 BTU/(Hour*Foot^2), 24 < x <= 36}}];
> Subscript[T, out] =
>   Piecewise[{{33 Fahrenheit, 0 <= x <= 12}, {43 Fahrenheit,
>      12 < x <= 24}, {45 Fahrenheit, 24 < x <= 36}, {37 Fahrenheit,
>      36 < x <= 48}, {32 Fahrenheit, 48 < x <= 60}, {27 Fahrenheit,
>      60 < x <= 72}, {26 Fahrenheit, 72 < x <= 84}, {25 Fahrenheit,
>      84 < x <= 96}}];
>
> Plot[{Subscript[q, solar], Subscript[T, out]}, {x, 0, 96}]
>
> Nodes = (WallThickness/\[CapitalDelta]x + 1) /.
>    masonry;  (*Number of Nodes for analysis*)
>
> *Nodes will be numbered from 0 to 5 with Node 0 on the inside surface
> of the wall and Node 5 on the exterior surface of the wall*
>
> Initial Conditions are found by the assumption that the temperature
> varies linearly inside the wall.
>
> Temperatures are given in the format:  T[node# , time step]   (ex
> T[0,1] is the interior node at time i=1)
>
> tempstep = (Subscript[T, in] - Subscript[T, out, initial])/(Nodes -
> 1) /.
>    masonry; (* Temperature change between each node *)
>
> T[0, 0] = Subscript[T, in] /.masonry; T[1, 0] = T[0, 0] - tempstep;
> T[2, 0] = T[1, 0] - tempstep; T[3, 0] = T[2, 0] - tempstep;
> T[4, 0] = T[3, 0] - tempstep; T[5, 0] = T[4, 0] - tempstep;
>
> EQUATIONS
>
> (* Node 0 has convection, can use Eqn 5-51 from book *)
>
> T[0, t_] := (1 - 2 \[Tau] - (2 \[Tau] Subscript[h, in] \
> [CapitalDelta]x)/k) T[
>       0, t - 1] +
>     2 \[Tau] T[1, t - 1] + (2 \[Tau] Subscript[h, in] \
> [CapitalDelta]x)/k*
>      Subscript[T, in] /.masonry;
>
> (* Nodes 1-4 take the more general form, use Eqn 5-47 *)
>
> T[m_, t_] := \[Tau] (T[m - 1, t - 1] + T[m + 1, t - 1]) + (1 - 2 \
> [Tau]) T[m,
>       t - 1] /.masonry;
>
> (* Node 5 is exposed to convection and heat flux outside *)
>
> T[5, t_] := (1 - 2 \[Tau] -
>        2 \[Tau] (Subscript[h, out] \[CapitalDelta]x)/k) T[5, t - 1] +
>     2 \[Tau] T[4, t - 1] +
>     2 \[Tau] (Subscript[h, out] \[CapitalDelta]x)/
>      k*(Subscript[T, out] /. {x -> t}) +
>     2 \[Tau] (\[Kappa] *(Subscript[q, solar] /. {x -> t}) \
> [CapitalDelta]x)/
>      k /.masonry;
>
> \[CapitalDelta]t = .25 Hour;
>
> \[Tau] = 0.10755
>
> T[0, t_] := (1 - 2 \[Tau] - (2 \[Tau] Subscript[h, in] \
> [CapitalDelta]x)/k) T[
>       0, t - 1] +
>     2 \[Tau] T[1, t - 1] + (2 \[Tau] Subscript[h, in] \
> [CapitalDelta]x)/k*
>      Subscript[T, in] /.masonry;
>
> T[m_, t_] := \[Tau] (T[m - 1, t - 1] + T[m + 1, t - 1]) + (1 - 2 \
> [Tau]) T[m,
>       t - 1] /.masonry;
>
> T[5, t_] := (1 - 2 \[Tau] -
>        2 \[Tau] (Subscript[h, out] \[CapitalDelta]x)/k) T[5, t - 1] +
>     2 \[Tau] T[4, t - 1] +
>     2 \[Tau] (Subscript[h, out] \[CapitalDelta]x)/
>      k*(Subscript[T, out] /. {x -> t}) +
>     2 \[Tau] (\[Kappa] *(Subscript[q, solar] /. {x -> t}) \
> [CapitalDelta]x)/
>      k /.masonry;
>
> Transpose[Table[T[n, x], {n, 0, 5}, {x, 0, 100}]] // MatrixForm
>
> Thank you for taking the time to look at this

[toc] | [prev] | [next] | [standalone]


#5978

FromA Retey <awnl@gmx-topmail.de>
Date2011-10-29 11:12 +0000
Message-ID<j8gn2o$58e$1@smc.vnet.net>
In reply to#5779
Hi,

> I am new to Mathematica and I have learned that it is a steep learning
> curve, I've spent a week trying to program an example problem from a
> heat transfer book.  I need it to solve for several hundred time steps
> and my computer is only allowing me to do about 15 in an hour of
> solving.  How can I clean up my code and what are some general tips
> that you can give me for the future when I need to do more hard core
> iteration problems and stuff?  PS, I'm a mechanical engineer so I'm
> not very good at understanding how Mathematica thinks in computer
> language (anything is helpful).

I think you got some hints about how to speed up your actual 
calculation. I just wanted to make sure you understand that there is 
actually no need to solve such a simple problem with such a low level 
approach: Mathematica can solve many partial differential equations out 
of the box. Only if you are taking advantage of those feature you will 
find Mathematica to be a powerful tool once you have learned how to use 
it. Here is an example which solves a 1d heat transfer problem with 
NDSolve, straight from the documentation of NDSolve:

NDSolve[{D[u[t, x], t] == D[u[t, x], x, x], u[0, x] == 0,
   u[t, 0] == Sin[t], u[t, 5] == 0}, u, {t, 0, 10}, {x, 0, 5}]

Plot3D[Evaluate[u[t, x] /. %], {t, 0, 10}, {x, 0, 5},
  PlotRange -> All]

Of course you would have to adopt the boundary condition to your case. 
If you are just using this as an example for learning Mathematica, you 
might want to use such a solution to check your results...

hth,

albert

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web