2

I have a class called class_geometry which hold the geometric quantities of each cells in a grid. On of these quantities is the x location of each of the four corners of the cell. I then make initialize an array of these class objects (not sure if my terminology is correct). My problem is that I do not know how to initialize an array in my class def. Here is my class:

classdef class_geometry
    properties
        dx1;
        dx2;
        dx3;
        dx4;
    end
end

And I would like to have just dx that holds four different values:

classdef class_geometry
    properties
        dx;
    end
end

So in the end I could do something like that:

for i = 1:IL+1
    for j = 1:JL+1
        cell(i,j).dx(1) = x_grid(i+1,j)   - x_grid(i,j);
        cell(i,j).dx(2) = x_grid(i+1,j+1) - x_grid(i+1,j);
        cell(i,j).dx(3) = x_grid(i,j+1)   - x_grid(i+1,j+1);
        cell(i,j).dx(4) = x_grid(i,j)     - x_grid(i,j+1);
    end
end

Thanks for the help.

2
  • So you want to avoid the cycle, right? Commented Nov 12, 2012 at 10:00
  • ya pretty much. I'll also have other properties like dy, dS, dn who all have 4 values assign to them. It'll make the code easier to read and implement Commented Nov 12, 2012 at 10:06

1 Answer 1

1

If the problem is to initialize an array inside the class, you have to define a constructor. The constructor is called when you initialize your object and aims at allocating memory for object members.

Here is how you can initialize and empty array for dx

   classdef class_geometry
       methods

         function obj = class_geometry()   //Constructor function
             obj.dx = zeros(1,4);   //because you had 4 entries
             ...

         end

        end
        .....
   end

Moreover, if you want to initialize the values dx contains in the constructor, you have to pass it the index/indices of the current grid cell

       function obj = class_geometry(i,j)   //Constructor function, 2nd version
                                            //i,j indices of the cell
             obj.dx = zeros(1,4);   //because you had 4 entries
              dx(1) = x_grid(i+1,j)   - x_grid(i,j);
              dx(2) = x_grid(i+1,j+1) - x_grid(i+1,j);
              dx(3) = x_grid(i,j+1)   - x_grid(i+1,j+1);
              dx(4) = x_grid(i,j)     - x_grid(i,j+1);

       end

Now, you can initialize your cells as

      cell(i,j) = class_geometry(i,j); //forall i,j.

Instead, if the problem is the initialization of array of objects, you may want to consider this reference.


Finally, my humble guess is that the vectorization you are trying is sub-optimal.

Because you have your grid in the matrix x_grid, a vectorization like (for the first case)

  dx_1 = diff(x_grid,1,1); //computes x_grid(i+1,j) - x_grid(i,j); 
                           //forall i,j producing the difference matrix

would be more efficient. It would save you a lot of external cycles (wrt the class).

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your input. This helps for this case but I also need in another part of my code to have a property array for my primitive variables U. If you could just tell me how to set up a array in a class that would be great! So in this case instead of cell(i,j).dx_1 = diff(x_grid,1,1) I would have cell(i,j).dx(1) = diff(x_grid,1,1)
@user1236187, well, the correct version of your statement would be ` cell(:,:).dx(1) = diff(x_grid,1,1)` but frankly I doubt matlab does support it. Let me have a look.
@user1236187, please consider the answer now
That's perfect! It's exactly what I was looking for thanks! Just a last question, you were talking about efficiency earlier, is this considered to be inefficient in terms of running time? I am doing a CFD code on the euler equations, I would like my code so be a minimum efficient.
Nice!! :) -- may you consider to accept the answer? ;). Well, AFAIK matlab is not very efficient in OOP. Moreover, you should try to vectorize as much as you can, meaning you should try to logically wrap things in arrays so that you can express your operations in terms of vector operations (matlab has fast core compiled functions for that).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.