Message Boards Message Boards

0
|
2321 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

While loop for power iteration of a diffusion problem

Posted 2 years ago

I am trying to use a While loop to solve for a diffusion problem using the power iteration method. The code is supposed to loop through three equations (flux, source, and k) until the fluxdiff > 0.0001. I am trying get this code to plot the flux and print a table of the flux, k, source, and fluxdiff. Right now the code does not produce any outputs and I am not sure where the problem(s) is/are. If there's a way to use a "Table" to solve this problem, I am willing to try it.

sigmaa = .1532;
\[Nu]sigmaf = .1570;
d = 1/(3 (.0362));

nodes3 = 4;
r1 = 1;
dr1 = r1/nodes3

a = 
Table[If[i == j, sigmaa*i*dr1*dr1 + (2 i dr1 d)/dr1, 
If[i - j == 1, -d/dr1 (i*dr1 - dr1/2), 
If[i - j == -1, -d/dr1 (i*dr1 + dr1/2), 0]]], {i, 0, 3}, {j, 0, 
3}];
anew = ReplacePart[a, {1, 1} -> d/2 + sigmaa dr1^2/8]

b =  Table[If[i == 1, \[Nu]sigmaf/2, \[Nu]sigmaf], {i, 1, nodes3}, {j, 1}]

flux = {{1}, {1}, {1}, {1}};
k = 1.0000;
source = b flux

fluxdiff = 1;
iteration = 0;

oldflux = flux;
oldk = k;
oldsource = source;

While[fluxdiff > 0.0001,
Print[iteration, flux, source, k, fluxdiff];

oldflux = flux;
oldk = k;
oldsource = source;
iteration = iteration + 1;

flux = 1/oldk LinearSolve[a, oldsource];
source = b flux;
k = oldk Total[source]/Total[oldsource];

fluxdiff = 0;
fluxdiff = RootMeanSquare[flux - oldflux];


]
POSTED BY: Robert Atwell
3 Replies

Your code initializes with k and fluxdiff being numbers but after one iteration k and fluxdiff are lists. Your while condition specifies fluxdiff >0.0001. But {number} > 0.001 will stop the iterations. For the second iteration, your code crashes because k is also becoming a list.

This should do the trick

out = First@Last@Reap@While[fluxdiff > 0.0001,
      oldflux = flux;
      oldk = k;
      oldsource = source;
      iteration += 1;

      flux = 1/oldk LinearSolve[a, oldsource];
      source = b flux;
      k = First[oldk Total[source]/Total[oldsource]];
      fluxdiff = 0;
      fluxdiff = First[RootMeanSquare[flux - oldflux]];
      Sow[{iteration, flux, source, k, fluxdiff}];
      ];
Grid@out
POSTED BY: Martijn Froeling

Is the IWhile intentional, or a typo?

POSTED BY: Daniel Lichtblau
Posted 2 years ago

It's a typo, the issue exists without the typo.

POSTED BY: Robert Atwell
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract