Skip to main content
1-Visitor
July 7, 2011
Solved

Difficulties extracting information from an array using nested for loops

  • July 7, 2011
  • 17 replies
  • 7046 views

I am using nested for loop to tell me the position of array elements that meet three different criteria. The inner loop is set for j = 0 - 9, but it appears that it is only evaluating for j = 9 and is ignoring j = 0 - 8. How am I messing up the syntax? The relevant details of the problem are on the attached worksheet.

Best answer by MikeArmstrong

Have a look at the attached, I think it works.

My god, I struggled with that one - Spend the rest of the weekend with the wife now

Mike

17 replies

1-Visitor
July 7, 2011

I don't fully understand what your trying to do. have a look at the attached sheet. I have started to clean your program up.

Can you elaborate a little please?

I cant see which column of final_element_balance your looking at.

Mike

belsherj1-VisitorAuthor
1-Visitor
July 8, 2011

Okay, I was concerned that I was being too confusing. Sorry about that. What I am doing is calculating phase equilibrium using Gibbs Energy minimization. I solve the resulting constrained minimization problem using Langrage multipliers. At the end of an iteration I need to check whether any of the compounds need to "swapped' out of the active set. This is where my problem arises.

The array "aqueous_species" contains the molar amounts of compound i that is currently in solution. I've relabeled the array so that it is clear what compound corresponds to what array position

The array "starting-balance" contains an elemental balance based on a starting inventory. I've relabled the array so that it is clear which element corresponds to what array position.

The matrix "final element balance" is a bookkeeping array that tells me how many of each element is connected with each compound. i.e. final_elemental_balance(1,4) tells me that there are 4 H associated with Al(OH)4-. I've relabeled this matrix to make it clear which element is connected with what column and what compound is connected with what row.

When I "swap" compounds out of the active set two (or three?) conditions need to be met:

1. The compound must have a current value of 0 in the "aqueous_species" array (this is by definition what the active set is)

2. For any element that has a value of zero in the "starting-balance" array, that element column must also have a value of zero in the "final element balance" array for the compound under consideration. For example, in this case, I don't need to consider wether Al(OH)4 needs to be swapped out of the active set, since I know that it does not exist in solution because the "starting-balance" array tells me that there is no aluminum in solution, hence no Al(OH)4 in solution.

I tried to do this with the "aqueous_active_set" program, but the inner loop j = 0 to 9 only seems to be evaluating whether the final column of "final element balance" (which corresponds to oxalate, C2O4) is nonzero, but it should also be evaluating wether columns j=0 to 8 contain nonzero values.

I hope that more clear, but I fear that it is not.

24-Ruby IV
July 8, 2011

Hello!

Why do we need this line in the loop of the program. Maybe you forgot the condition "if"?Programm.jpg