CHLNDDEV / OceanMesh2D

A two-dimensional triangular mesh generator with pre- and post-processing utilities written in pure MATLAB (no toolboxes required) designed specifically to build models that solve shallow-water equations or wave equations in a coastal environment (ADCIRC, FVCOM, WaveWatch3, SWAN, SCHISM, Telemac, etc.).
https://github.com/sponsors/krober10nd
GNU General Public License v3.0
182 stars 65 forks source link

Renumbering boundary nodes to start from 1 #262

Closed cliu3 closed 2 years ago

cliu3 commented 2 years ago

Is it possible to renumber the open boundary nodes to start from 1, either through built-in functionalities or post-processing?

krober10nd commented 2 years ago

@cliu3 you want the global number of the open boundary nodes to start at 1? If I understood that right, then I would suggest to write a short code that maps your current global node numbering to your desired new numbering and then loops through the connectivity table and updates the connectivity.

Something like this (beware: I have not tested this at all).

new_node_numbers = [(1:length(p))'; zeros(length(p),1)]; 
% assume I have one open ocean boundary 
new_node_number=0;
for i = 1 : length(m.op.nbdv(:,1)) % for the nodes on the open boundary
    new_node_numbers(m.op.nbdv(i,1),2) = new_node_number 
    new_node_number = new_node_number + 1;  
end
% now for all other nodes 
for i = 1 : length(m.p)
    if new_node_numbers(i,2) == 0
       new_node_numbers(i,2) = new_node_number
       new_node_number = new_node_number + 1; 
    end
end
% now update connectivity 
new_t =m.t
for i = 1 : length(m.t) 
   for j = 1: 3
        new_t(i,j) = new_node_numbers(new_t(i,j),2);
   end
end
cliu3 commented 2 years ago

@krober10nd This is very helpful. Thank you!

krober10nd commented 2 years ago

No problem. I'm relooking at what I wrote above and will need to make sure you start the variable new_node_number at 1 (not 0).

krober10nd commented 2 years ago

Can I close this?

cliu3 commented 2 years ago

Yes you may close this. I'd like to share my tweak of the code that ended up working for me:

function m = renumber_ob(m)
    new_node_numbers = [(1:length(m.b))', zeros(length(m.b),1)]; 
    % assume I have only one open ocean boundary 
    new_node_number=1;
    for i = 1 : length(m.op.nbdv(:,1)) % for the nodes on the open boundary
        new_node_numbers(m.op.nbdv(i,1),2) = new_node_number ;
        new_node_number = new_node_number + 1;  
    end
    % now for all other nodes 
    for i = 1 : length(m.p)
        if new_node_numbers(i,2) == 0
           new_node_numbers(i,2) = new_node_number;
           new_node_number = new_node_number + 1; 
        end
    end
    % now update connectivity 
    new_t =m.t;
    for i = 1 : length(m.t) 
       for j = 1: 3
            new_t(i,j) = new_node_numbers(new_t(i,j),2);
       end
    end
    % replace bcs
    m.op.nbdv = new_node_numbers(m.op.nbdv,2);
    for i=1:size(m.bd.nbvv,2)
        col = m.bd.nbvv(:,i);
        col(col>0) = new_node_numbers(col(col>0),2);
        m.bd.nbvv(:,i) = col;
    end
    new_node_numbers = sortrows(new_node_numbers,2);
    m.p = m.p(new_node_numbers(:,1),:);
    m.t = new_t;
    m.b = m.b(new_node_numbers(:,1));
krober10nd commented 2 years ago

Thank you very much @cliu3