cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X

Difficulties extracting information from an array using nested for loops

belsherj
1-Newbie

Difficulties extracting information from an array using nested for loops

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

17 REPLIES 17
MikeArmstrong
5-Regular Member
(To:belsherj)

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

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.

Hello!

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

MikeArmstrong
5-Regular Member
(To:VladimirN)

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

That was out of my worksheet. I removed the "if and continue" statements as they where not doing anything. That is only a cleaned up version of the original post.

Mike

MikeArmstrong
5-Regular Member
(To:belsherj)

It becoming more clear.

A few things to clear up first.

I just need to get my head around this, sorry.

  • You look up aqueous_species for zero values.
  • Then you look in starting_balance for zero's, then I get a little lost.

How about you write the process long hand in your sheet. I'm 100% this can be done, but I can't get my head around it.

When you say your answers should be 7, 12 and 13, what does that mean? Rows? Colunms? Count?

Mike

Thanks for the responses. The answers 7, 12 and 13 refer to the positions of compounds in the aqueous_species array. For instance, position 7 refers to NO2, 12 refers to NaNO2 and 13 refers to NaNO3. I then use this information to extract physical property data from other arrays. For example, I will then go to the array that contains the reduced chemical potential data and the value in row 7 is the reduced chemical potential for NO2 (which is also row 7 in the aqueous_species array.)

The worksheet has (or will have) the ability to calculate equilibrium for a system that contains 31 different chemical compounds and 11 different "elemental" constituents (I know that C2O4 and e- are not elements). However, if I am trying to calculate equilibrium for a small subset of compounds, I end up with lots of zeros in my matrices (these zeros are due to the compounds that do not exist in the mixture) and MathCad cannot solve the linear system of equations.

How I am trying to work around this is to reduce the size of the matrix by eliminating compounds that cannot exist in the mixture due to the starting conditions. This is where the difficulties begin. The starting-balance array tells me the total amount of each of the "elemental" constituents in the system. In this example I have 111.017 moles of H and 0 moles of S. Based on this information I know that any compound that contains S cannot exist in the mixture. What I want to do is go down the S column in the final-element-balance matrix and any compound that contains S should be eliminated from further consideration. In this example "aqueous_active_set" row 8 has a value of 11. This corresponds to SO4, however this should not be included because "starting-balance" and "final-element-balance" tell me that SO4 will not exist in this solid-liquid system. I used S in this example but I want to do it for every independent component in the sytem except for e-.

MikeArmstrong
5-Regular Member
(To:belsherj)

I'm really sorry this isn't clicking and I've spent a little time on it now and to be perfectly honest I think others might be confused.

If you don't mind I would like to go to the beginning. I have shortened your matrix names for ease.

First we check vector as for a zero value, which is the second element. Correct?

Then we check...........

Now can you write the next two steps as I have above.

Sorry for my ignorance, I have had a long day.

Mike

Message was edited by: Mike Armstrong

No big deal. I can get it to work without using the inner loop, i just need ten separate steps. I thought adding the inner would make things a little more elegant, but clearly there is something I am not understanding about the syntax. The steps

For each row in as:

1. Check as for a zero value

2. If that row in as has a zero value go over to the matrix feb and check that same row for nonzero values. For any column in that row that has a nonzero value check the corresponding column in array sb. If that column has a zero value then "exit" the loop and check the next row in as.

3. If the row in as has a zero value and any columns in the corresponding row in matrix feb that has a nonzero value also has a nonzero value in the corresponding column in array sb than add the position of that row into the output array.

Hopefully that is clearer. If not, I can get it to work without the inner loop, it is just a little messier. I've included two examples below for illustration. Feel free to ignore them if necessary.

Check row 1 (assuming the array start at position zero)

1. Row 1 in array as has a zero value. So I go over to matrix feb

2. I check all values in row 1 of matrix feb. I see that column 0 has a value of 1.

3. I now check column zero of array sb and see that it has zero value.

4. Therefore exit the loop and move onto row 2

Check row 7

1. Row 7 in array as has a zero value. So I go over to matrix feb

2. I check all value in row 7 of matrix feb. I see that columns 4 and 6 have nonzero value (we ignore column 10)'

3. I now check columns 4 and 6 in array sb. I see that that contains nonzero values.

4. Therefore I add 7 to the output array.

MikeArmstrong
5-Regular Member
(To:belsherj)

Got it

Just give me a little time to get the program working.

Check row 1 (assuming the array start at position zero)

1. Row 1 in array as has a zero value. So I go over to matrix feb

2. I check all values in row 1 of matrix feb. I see that column 0 has a value of 1.

3. I now check column zero of array sb and see that it has zero value.

4. Therefore exit the loop and move onto row 2

So in the case you check the first value in row one adove zero, which in this is case and break the loop becuase the corresponding value in the same colunm in sb is zero.

Mike

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

Thanks Mike, I took the weekend off to do some house remodeling - your dedication puts me to shame. Anyway, it looks good. It seemed like a program that should be easy, but I guess not.

MikeArmstrong
5-Regular Member
(To:belsherj)

Thanks Mike, I took the weekend off to do some house remodeling - your dedication puts me to shame. Anyway, it looks good. It seemed like a program that should be easy, but I guess not.

No problem at all - don't tell the wife though

Mike

Jeremy Belsher wrote:

A.... For any column in that row that has a nonzero value check the corresponding column in array sb. If that column has a zero value then "exit" the loop and check the next row in as...

...Check row 1 (assuming the array start at position zero)

1. Row 1 in array as has a zero value. So I go over to matrix feb

2. I check all values in row 1 of matrix feb. I see that column 0 has a value of 1.

3. I now check column zero of array sb and see that it has zero value.

4. Therefore exit the loop and move onto row 2...

Step 4 is not consistent with sentence A here. Why do you exit the loop before checking other columns (in fact for row 1 there is another column with a non-zero value and that column does appear in array sb!)?

Alan

I exit the loop because as soon as a column (for a given row) has a nonzero value and the corresponding column in array sb as a zero value, my criteria has been violated and I am no longer interesting in that particular row.

In practical terms, if you are at all curious, what it means is that compound i contains element j, which does not exist in solution therefore compound i does not exist in solution.

Jeremy Belsher wrote:

I exit the loop because as soon as a column (for a given row) has a nonzero value and the corresponding column in array sb as a zero value, my criteria has been violated and I am no longer interesting in that particular row.

Got it! To prove it, here's an alternative to Mike's solution!

Alan

Thanks Alan, that also looks good. I didn't know about the sign() function, which will be useful in other programs. Just for discussion's sake (and to help me learn), what was wrong with my initial formulation? It seems like it should have worked and I don't understand where my logic went wrong.

Jeremy Belsher wrote:

Thanks Alan, that also looks good. I didn't know about the sign() function, which will be useful in other programs.

Actually, there is a small inadequacy in my previous file (i.e. an error!) - also involving the sign function - corrected in the attached.

Just for discussion's sake (and to help me learn), what was wrong with my initial formulation? It seems like it should have worked and I don't understand where my logic went wrong.

I'll have to go back and look at your original again. Can't remember now what was in it!

Alan

Top Tags