Skip to content

Conversation

@mdumett
Copy link
Collaborator

@mdumett mdumett commented Mar 17, 2025

have updated the pure periodic boundary condition code (Per) with a new one called addGralBC which actually combines the capabilities of addBC and the pure periodic boundary conditions. The matrix sizes are reduced because of the chopping of boundaries along directions with periodic boundary conditions.

If you recall,

  • addBC imposes explicitly periodic BC (as well as the non-periodic).
  • Pure BC (called Per in the code) handles periodic BC implicitly by removing the boundary when periodic BC are required and it is for the exclusive case where all BC are periodic. The advantage of the Per code is that D = - G' and interpolations are symmetric.
  • The new version, addGralBC combines addBC (for the directions where non-periodic boundary conditions are required) with Per (in the directions where periodic boundary conditions are needed). This new code removes the boundary of the directions where periodic boundary conditions are needed.

Examples of how to use it are added.

So, now we have two ways of imposing boundary conditions: explicitly (using addBC) and implicitly (using addGralBC, which calls Per for the directions where there is a periodic boundary condition).

addGralBC and Per are in my addBCs3 branch. Please, review them to be able to push it as soon as possible.

This ends all boundary treatment.

These are gradient, divergence, and interpolation periodic operators. They satisfy the following properties: grad = - div^T and interp from nodes to centers is the transpose of the interp from centers to nodes
addBC allowed periodic BC imposing the conditions explicitly. Per files solve periodic BC without imposing any conditions. The domain does not include boundaries. Gral combines traditional operators and handling periodic boundary conditions without specifying explicitly them
Copy link
Collaborator

@jbrzensk jbrzensk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mdumett for these additions.

I had a few items with the license info, and location, but I have suggestions for the overall structure.

We do not want to have users interacting with this many functions. It is confusing. I am confused. I think these should be called internally from the addBC functions you made earlier.
Those functions already take parameters for boundary conditions, periodic included correct? If the user requests periodic, then the addBC function should call the appropriate addGralBC1D operator, to generate the periodic version.

Same for the divPer and gradPer. If the user requests periodic everywhere, then the addBC should call divPer or gradPer, and return them to the user as D or G. The user does not need to see the naming used.

I also think, with all of your excellent additions, we need to have subfolders for the operators. We may have to accept there will be $PATH arguments, but there are a lot of files for humans to parse now. I propose a three tiered structure of:

src/MATLAB/  
│── +Boundary_Conditions/     % Boundary Conditions
│   ├── addBC1D.m             % Main function to apply BCs (user-facing)  
│   ├── addBC2D.m             %  
│   ├── BC_Helpers/           % All internal functions the user does not need to see  
│   │   ├── addGralBC1D.m  
│   │   ├── addGralBC2D.m  
│   │   └── others....m  
│── +Interpolators/           % Interpolators
│   ├── intperCenterToNode.m  % Main function to apply BCs (user-facing)  
│   ├── interpNodeToCenter.m  %  
│   ├── Interp_Helpers/       % All internal functions the user does not need to see  
│   │   ├──  interpolCentersToFacesD1DPer.m  
│   │   ├──  interpolCentersToFacesD2DPer.m  
│   │   └── others....m  
│── +Operators/    % Mimetic Operators
│   ├── div.m      % overloaded functions (user-facing)  
│   ├── grad.m     %  figure out what to call based on number of arguments
│   ├── Operator_Helpers/  % All internal functions the user does not need to see  
│   │   ├──  DI1.m  
│   │   ├──  DI2.m  
│   │   ├──  Div2D.m  
│   │   ├──  Div3D.m  
│   │   └── curvGrad.m  

Now, I get that you are not responsible for this, but I think the boundary conditions (which you are now king of 😜 )can start us off. I ask for @jcorbino and @cpaolini @pritkc @JananiPSrinivasan for comments about the structure proposed. I threw this out there without too much thinking.

% dx : Step size along x-axis
% n : Number of cells along y-axis
% dy : Step size along y-axis

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We put the license information at the end of the description.

% ----------------------------------------------------------------------------
% SPDX-License-Identifier: GPL-3.0-or-later
% © 2008-2024 San Diego State University Research Foundation (SDSURF).
% See LICENSE file or https://www.gnu.org/licenses/gpl-3.0.html for details.
% ----------------------------------------------------------------------------


function D = div2DPer(k, m, dx, n, dy)
% Returns a two-dimensional mimetic divergence operator
% when the boundary condition is periodic
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add t a qualifier for clarity:

when all of the boundaries are periodic

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review. I will add more comments explaining the specifics of each file. I agree with the fact of adding more options for boundary conditions could be confusing for the user and that most of the files should be hidden.
The only difference with your reasoning is that addGralBC is the version that is most general and has a better implementation than the others. So, addGralBC should be kept as the only public code for handling BC and all the others (including robinBC should be concealed). This also will require modifying all current examples to use addGralBC). If there is an agreement on this, I volunteer to do all these changes in my next version of the code (as soon as possible). Please, let me know.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of naming, one could rename addBC as addNonPerBC (and call it when BC along certain direction is non-periodic, as addGralBC does) and rename addGralBC as addBC.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your addGralBC1D should be named addBC1D. It does all that addBC1D does and more. Is there an issue if your current "addGralBC" boundary condition files replace the existing "addBC" files?

I see the point of the other files, and there needs to be reorganization. The examples are needed as test cases for the periodic operators.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the addGralBC calls both addBC and the pure periodic case. Just renaming addGralBC as addBC will overwrite a large part of the code. I can reorganize following the introduction of directories approach you mentioned, and editing the examples to just use addGralBC (which will be named addBC).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addGradlBC1D only calls addGralBC1Dlhs and addGralBC1Drhs. It seems like it is the same code, just with an initial check to see if it is periodic in addGralBC1Dlhs and addGralBC1Drhs.

If you think of it as a change of addBC1D, you are only changing a few lines, not rewriting tons of code.

@mdumett
Copy link
Collaborator Author

mdumett commented Mar 20, 2025

You are right. addGralBC is independent of addBC. It is just manner of renaming these operators. The dependence is on the gradGral, divGral and lapGral operators. They depend on the original grad, div and lap as well as on the pure periodic operators. The same is true for the interpolators. I can do all modifications and review the examples since addBC treats explicitly periodic BC and addGralBC only implicitly. If you agree, I can proceed doing all changes.

@jbrzensk
Copy link
Collaborator

For clarity:
You are modifying the addBC1D, operators to take into account periodicity.
These will call their own operators, returning the modified left and right hand sides.
The same process as before.

All boundary condition operators will be in a subfolder called Boundary_Conditions. The only operator that a user will see is addBC1D or addBC2D etc. The others will be in a subfolder called "BC_Helpers" ( see #99 (review))

You keep the examples because they also act as tests for the Gral functions. (Also, why 'Gral'? )

You are keeping the divPer, gradPer, and lapPer as is until we come up with a better name and way of calling the functions. They are needed, and there is no way around it. I would like them renamed better, maybe "div1DPeriodic". Same for the interpolators. They will go the same way as the boundary conditions, where a few will be used to call the more complicated cases. interpolFacesToCentersG1DPeriodic.m

Examples will be combined in a future PR. There are just too many to parse now.

Interpolators will be combined in a future PR. Smae reason.

I think to really make the boundary condition functions universal, will be for addB1D to detect what is passed, either the grad, div, or lap, based on the size, and to modify the operator appropriately.

@mdumett
Copy link
Collaborator Author

mdumett commented Mar 21, 2025

Agree with all but except with "... to really make the boundary condition functions universal, will be for addB1D to detect what is passed, either the grad, div, or lap, based on the size, and to modify the operator appropriately." All BC functionalities (addGralBC, addBC, and pure periodic case) consider general second-order PDE operators (i.e., the PDE can be composed of Laplacians, gradients, divergences, curls, etc., provided the whole PDE operator is of second-order) and hence the BC functions are not associated to a Laplacian or a gradient or a divergence per se.

@jbrzensk
Copy link
Collaborator

Wait, these addBCs only work with Laplacian minetic operators?

@mdumett
Copy link
Collaborator Author

mdumett commented Mar 21, 2025

No. addGralBC and addBC work for any PDE of second-order. In general the PDE can have several terms, each of them having any operator provided the order of the PDE is two.

@jbrzensk
Copy link
Collaborator

jbrzensk commented Mar 21, 2025 via email

@mdumett
Copy link
Collaborator Author

mdumett commented Mar 21, 2025

That is true. It is expected that the PDE is written in terms of cell centers both input and output (hence the square matrix).

@jbrzensk
Copy link
Collaborator

Are the periodic boundary conditions required? Or is it required by this pull request? They may be better suited to a separate pull request. To save sanity in testing everything.

@mdumett
Copy link
Collaborator Author

mdumett commented Mar 25, 2025

The periodic boundary conditions are required. I suggests to add the general boundary conditions (addGralBC) renamed as addBC. There will be only nine BC files. In addition, the additional versions of the gradient, divergence and curl have to be added in this PR (PR1). In the next PR (PR2), we can deprecate robinBC and all others BC operators but addBC, and rewrite all examples to use exclusively addBC. Later, in another PR (PR3), we can organize implement the new directory organization. Another sequence of work could be PR1, PR3, PR2. Let me know what you think.

@jbrzensk
Copy link
Collaborator

My mistake!
I meant to ask "Are the periodic Interpolators necessary?". I think you already answered this.

I agree with the PR order.

  1. Push just boundary conditions, addBC, (which is addBCGral renamed), and necessary operators for those.
  • "Per" changed to "Periodic" in function names
  • "Gral" is changed to explain the difference between "divGral" and "div", use noun or verb.
  1. Push examples using addBC, deprecating addRobin.
  2. New directory structure (editing examples if needed to take new location), which requires redoing the examples again for the new structure
  3. Adding periodic interpolators

@aboada
Copy link
Collaborator

aboada commented Mar 26, 2025

Thanks @mdumett for these additions.

I had a few items with the license info, and location, but I have suggestions for the overall structure.

We do not want to have users interacting with this many functions. It is confusing. I am confused. I think these should be called internally from the addBC functions you made earlier. Those functions already take parameters for boundary conditions, periodic included correct? If the user requests periodic, then the addBC function should call the appropriate addGralBC1D operator, to generate the periodic version.

Same for the divPer and gradPer. If the user requests periodic everywhere, then the addBC should call divPer or gradPer, and return them to the user as D or G. The user does not need to see the naming used.

I also think, with all of your excellent additions, we need to have subfolders for the operators. We may have to accept there will be $PATH arguments, but there are a lot of files for humans to parse now. I propose a three tiered structure of:

src/MATLAB/  
│── +Boundary_Conditions/     % Boundary Conditions
│   ├── addBC1D.m             % Main function to apply BCs (user-facing)  
│   ├── addBC2D.m             %  
│   ├── BC_Helpers/           % All internal functions the user does not need to see  
│   │   ├── addGralBC1D.m  
│   │   ├── addGralBC2D.m  
│   │   └── others....m  
│── +Interpolators/           % Interpolators
│   ├── intperCenterToNode.m  % Main function to apply BCs (user-facing)  
│   ├── interpNodeToCenter.m  %  
│   ├── Interp_Helpers/       % All internal functions the user does not need to see  
│   │   ├──  interpolCentersToFacesD1DPer.m  
│   │   ├──  interpolCentersToFacesD2DPer.m  
│   │   └── others....m  
│── +Operators/    % Mimetic Operators
│   ├── div.m      % overloaded functions (user-facing)  
│   ├── grad.m     %  figure out what to call based on number of arguments
│   ├── Operator_Helpers/  % All internal functions the user does not need to see  
│   │   ├──  DI1.m  
│   │   ├──  DI2.m  
│   │   ├──  Div2D.m  
│   │   ├──  Div3D.m  
│   │   └── curvGrad.m  

Now, I get that you are not responsible for this, but I think the boundary conditions (which you are now king of 😜 )can start us off. I ask for @jcorbino and @cpaolini @pritkc @JananiPSrinivasan for comments about the structure proposed. I threw this out there without too much thinking.

Some quick suggestions:
If in MATLAB you can derive the context of BC_Helpers from the parent directory (BC_Helpers), you may call it Utils and have:

│── +Boundary_Conditions/     % Boundary Conditions
...
│   ├── Utils/           % All internal functions the user does not need to see  
...

The same applies to Operators and Operators_Helpers. I believe in https://en.wikipedia.org/wiki/Don%27t_repeat_yourself.
Also, I do not think we should have abbreviations like addGralBC1D since it decreases readability.

@mdumett
Copy link
Collaborator Author

mdumett commented Apr 1, 2025

@jbrzensk & @aboada : Mimetic differences use staggered grid. Periodic interpolators are needed together with periodic gradient and divergence. According to your item list,
a) I propose to do a first PR with 1 & 5, for which I need to make some name changes and I will put the license in the right place.
b) In a second PR, a will do 3, and deprecate robinBC.
c) In a third PR, I will focus on the organization of the code following what was suggested.
Please, let me know if you agree with this plan, so I can start and finish it as soon as possible.

@aboada
Copy link
Collaborator

aboada commented Apr 3, 2025

@jbrzensk & @aboada : Mimetic differences use staggered grid. Periodic interpolators are needed together with periodic gradient and divergence. According to your item list, a) I propose to do a first PR with 1 & 5, for which I need to make some name changes and I will put the license in the right place. b) In a second PR, a will do 3, and deprecate robinBC. c) In a third PR, I will focus on the organization of the code following what was suggested. Please, let me know if you agree with this plan, so I can start and finish it as soon as possible.

Sounds good to me. Thanks!

@mdumett
Copy link
Collaborator Author

mdumett commented Apr 7, 2025

@jbrzensk and @aboada: With respect to the name of the new function that adds boundary conditions to the PDE and rhs operators (addBC), I realized that all the boundary condition operators that are considered for MOLE are just for scalar fields. Vector fields have different Dirichlet and Neumann BCs (and possibly Robin as well as others). For example, if u is a vector field, n the unit exterior normal, and t the unit tangent, u.n = 0 is Dirichlet for vector fields and is not considered currently in MOLE. In addition, [u]_t = 0 (no relative motion between a viscous fluid and a rigid wall), [S n]_t, + b [u]_t = 0, with S = mu (div_x u + div_x^T u), the viscous stress tensor, as well as others are frequent BCs for vector fields and MOLE does not contain any of those. I suggest using the name addScalarFieldBC instead of addBC for the new functionality. What do you think?

@jbrzensk
Copy link
Collaborator

jbrzensk commented Apr 7, 2025

So the boundary conditions you have made up are for scalars only?

If so, you should call your boundary condition functions "addScalarBC", which will take the place of "addBC", with an eye on the future where you will make a "addVectorBC" function.

Gral is changed by Scalar, Per by Periodic and added License Identifier. However, there are divGral, gradGral and lapGral operator that still need to be resolved
@mdumett
Copy link
Collaborator Author

mdumett commented Apr 10, 2025

@jbrzensk @aboada: As you can see in the addBCs3 branch, I have changed the name addGralBc by addScalarBC, the name Per by Periodic and added the License-Identifier in all source files. Also modified the examples related to these changes. However, there remain files divGral, gradGral and lapGral. The reason is that the Gral versions call the Periodic operators or the old ones (div, grad, lap) according to the BC type in each coordinate. To handle this the Gral versions have a couple of extra arguments associated to BCs.

We need to decide how to incorporate this into MOLE carefully since it may create a legacy issue. It might make incompatible users old codes. I propose to rename the old versions as divNonPeriodic, gradNonPeriodic, lapNonPeriodic and the Gral versions as div, grad, lap. If the last two arguments of the Gral versions are empty, it will call the NonPeriodic versions (i.e, the old ones). If they are non-empty it will choose between Periodic or NonPeriodic operators according to the BCs in each coordinate. I believe this is a simple way to solve this issue. Please, let me know what you think.

@jbrzensk
Copy link
Collaborator

This seems needlesssly overly complicated. What does "Gral" mean? I'm going to offer suggestions to simplify this large code change.

Would it be easier to modify the existing div, grad, etc, to have the option of taking two extra arguments? Then call divGralXX to decide how to do periodic? That would allow existing code to still work, and allow people to use the familiar div arguments. This allows us to 'quietly' modify div or grad, and not disturb existing code.

OR, just call your divGralXX divPeriodicXX. The user knows the boundary condition they want. If there is some periodicity, call the periodic one ( which is called divPeriodic, no dimension ); otherwise, call the regular code.

grad3DPeriodic is a repeat of the command gradGral3D(k,m,dx,n,dy,o,dz,[0,0],[0,0],[0,0]), I don't think we need the individual grad3DPeriodic, div3DPeriodic, etc, if they are just wrappers to the equivalent grad and div calls. (Alsogradperis not defined in the periodic operators) (-8 files)

addScalarBCXX are the same as the existing addBCXX. I think you just want to rename the existing files, or keep them and ignore the addScalarBC( -9 files ).

Here is an example of the first thing I was talking about.

function D = div(k, m, dx, dc ,nc)
% Returns a m+2 by m+1 one-dimensional mimetic divergence operator
%
% Parameters:
%                 k : Order of accuracy
%                 m : Number of cells
%                dx : Step size
%  ( optional )  dc : (a0) vector for left/right BC 
%  ( optional )  nc : (b0) vector for left/right coefficient
 
    % Assertions:
    assert(k >= 2, 'k >= 2');
    assert(mod(k, 2) == 0, 'k % 2 = 0');
    assert(m >= 2*k+1, ['m >= ' num2str(2*k+1) ' for k = ' num2str(k)]);
    assert( nargin == 3 || nargin == 5 , 'Need 3 or 5 arguments');

 if ( nargin == 5 )
        D = divGral1D(k,m,dx,dc,nc);
   
 else
    D = sparse(m+2, m+1);

    switch k
        case 2
            for i = 2:m+1
               D(i, i-1:i) = [-1 1];
            end

        case 4
            A = [-11/12 17/24 3/8 -5/24 1/24];
            D(2, 1:5) = A;
            D(m+1, m-3:end) = -fliplr(A);
            for i = 3:m
               D(i, i-2:i+1) = [1/24 -9/8 9/8 -1/24];
            end

        case 6
            A = [-1627/1920  211/640  59/48  -235/192 91/128 -443/1920 31/960; ...
                    31/960  -687/640 129/128   19/192 -3/32    21/640  -3/640];
            D(2:3, 1:7) = A;
            D(m:m+1, m-5:end) = -rot90(A,2);
            for i = 4:m-1
                D(i, i-3:i+2) = [-3/640 25/384 -75/64 75/64 -25/384 3/640];
            end

        case 8
            A = [-1423/1792     -491/7168   7753/3072 -18509/5120  3535/1024 -2279/1024  953/1024 -1637/7168  2689/107520; ...
                  2689/107520 -36527/35840  4259/5120   6497/15360 -475/1024  1541/5120 -639/5120  1087/35840  -59/17920; ...
                   -59/17920    1175/21504 -1165/1024   1135/1024    25/3072  -251/5120   25/1024   -45/7168     5/7168];
            D(2:4, 1:9) = A;
            D(m-1:m+1, m-7:end) = -rot90(A,2);
            for i = 5:m-2
                D(i, i-4:i+3) = [5/7168 -49/5120 245/3072 -1225/1024 1225/1024 -245/3072 49/5120 -5/7168];
            end
    end
    D = (1/dx).*D;
end
end

@mdumett
Copy link
Collaborator Author

mdumett commented Apr 11, 2025

@jbrzensk @aboada: There is a lack of understanding of the proposed changes. There are two different parts in them. The operators and the boundary conditions.

Operators: I refer to grad as an example but what is mentioned applies to div too
For m cells, periodic operators (e.g., gradPeriodic) are square m x m matrices. Non-periodic ones (like the old grad) are (m+1) x (m+2) non-square matrices. Therefore grad3DPeriodic is not simply a repeat of gradGral3D with the right arguments. Periodic operators are smaller.

The new grad matrix representation depends on whether the BC along each axis is periodic or not.

The changes proposed to add a vargin argument for grad. Whether this argument its empty or not, in each direction, will be used to call the periodic or the old ones. The user will not have to change anything in their codes, since it is a vargin argument. I believe, this is in essence what you propose.

Boundary conditions:
The reason of this difference is the way periodic boundary conditions are treated. Periodic BC does it in an implicit way, without modifying any matrices. The previous way of imposing periodic BCs was in an explicit way, modifying rectangular matrices.

The implicit code will be used for the Plasma Fusion project at least.

I hope this helps to understand the new code. Please, let me know if it is clear now.

Thank you for pointing out some places were I forgot to substitute Per by Periodic.

I would like to continue modifying the examples that use addBC to use addScalarBC instead and then reorganize the directories.

@jbrzensk
Copy link
Collaborator

"Operators: I refer to grad as an example but what is mentioned applies to div too
For m cells, periodic operators (e.g., gradPeriodic) are square m x m matrices. Non-periodic ones (like the old grad) are (m+1) x (m+2) non-square matrices. Therefore grad3DPeriodic is not simply a repeat of gradGral3D with the right arguments. Periodic operators are smaller."

What is the difference between these two commands?

G = grad3DPeriodic(4,10,1,11,1,12,1);
G = gradGral3D(4,10,1,11,1,12,1,[0;0;0;0;0;0],[0;0;0;0;0;0]);

Matlab says they are the same:

>> Gp = grad3DPeriodic(4,10,1,11,1,12,1);
>> G = gradGral3D(4,10,1,11,1,12,1,[0;0;0;0;0;0],[0;0;0;0;0;0]);
>> Gp = full(Gp);
>> G = full(G);
>> diff = sum( abs( Gp(:) - G(:) ) );
>> diff

diff =

     0

This is where my comment came from. These appear to be the same. If they are, we do not need another function to generate the 2D and 3D versions of a periodic operator. The 1D version IS needed, but the others are superfluous.

"Boundary conditions:
The reason of this difference is the way periodic boundary conditions are treated. Periodic BC does it in an implicit way, without modifying any matrices. The previous way of imposing periodic BCs was in an explicit way, modifying rectangular matrices."

The "AddScalarBC" seems to be the same as "addBC", except with the additional property that "addScalarBC" can "separate out periodic versus non-periodic boundary conditions". This implies this is exactly the same as the existing addBC functions, with an added internal feature. If so, why not just add the feature to the existing addBC functions? You do not need 9 NEW functions.

@mdumett
Copy link
Collaborator Author

mdumett commented Apr 11, 2025

Operators:
You are right. The 1D IS needed, but the others are superfluous. I will remove the 2D and 3D versions.

Boundary conditions:
I will remove the addBC versions and keep the addScalarBC versions since the latter are better. However, I will remove the addBC in the next PR when updating the examples that were introduced for addBC to be examples for addScalarBC. Is that fine? Or should I remove them now and also update the addBC examples to use addScalarBC. Please, let me know to proceed.

@aboada
Copy link
Collaborator

aboada commented Apr 12, 2025

This seems needlesssly overly complicated. What does "Gral" mean? I'm going to offer suggestions to simplify this large code change.

Would it be easier to modify the existing div, grad, etc, to have the option of taking two extra arguments? Then call divGralXX to decide how to do periodic? That would allow existing code to still work, and allow people to use the familiar div arguments. This allows us to 'quietly' modify div or grad, and not disturb existing code.

OR, just call your divGralXX divPeriodicXX. The user knows the boundary condition they want. If there is some periodicity, call the periodic one ( which is called divPeriodic, no dimension ); otherwise, call the regular code.

grad3DPeriodic is a repeat of the command gradGral3D(k,m,dx,n,dy,o,dz,[0,0],[0,0],[0,0]), I don't think we need the individual grad3DPeriodic, div3DPeriodic, etc, if they are just wrappers to the equivalent grad and div calls. (Alsogradperis not defined in the periodic operators) (-8 files)

addScalarBCXX are the same as the existing addBCXX. I think you just want to rename the existing files, or keep them and ignore the addScalarBC( -9 files ).

Here is an example of the first thing I was talking about.

function D = div(k, m, dx, dc ,nc)
% Returns a m+2 by m+1 one-dimensional mimetic divergence operator
%
% Parameters:
%                 k : Order of accuracy
%                 m : Number of cells
%                dx : Step size
%  ( optional )  dc : (a0) vector for left/right BC 
%  ( optional )  nc : (b0) vector for left/right coefficient
 
    % Assertions:
    assert(k >= 2, 'k >= 2');
    assert(mod(k, 2) == 0, 'k % 2 = 0');
    assert(m >= 2*k+1, ['m >= ' num2str(2*k+1) ' for k = ' num2str(k)]);
    assert( nargin == 3 || nargin == 5 , 'Need 3 or 5 arguments');

 if ( nargin == 5 )
        D = divGral1D(k,m,dx,dc,nc);
   
 else
    D = sparse(m+2, m+1);

    switch k
        case 2
            for i = 2:m+1
               D(i, i-1:i) = [-1 1];
            end

        case 4
            A = [-11/12 17/24 3/8 -5/24 1/24];
            D(2, 1:5) = A;
            D(m+1, m-3:end) = -fliplr(A);
            for i = 3:m
               D(i, i-2:i+1) = [1/24 -9/8 9/8 -1/24];
            end

        case 6
            A = [-1627/1920  211/640  59/48  -235/192 91/128 -443/1920 31/960; ...
                    31/960  -687/640 129/128   19/192 -3/32    21/640  -3/640];
            D(2:3, 1:7) = A;
            D(m:m+1, m-5:end) = -rot90(A,2);
            for i = 4:m-1
                D(i, i-3:i+2) = [-3/640 25/384 -75/64 75/64 -25/384 3/640];
            end

        case 8
            A = [-1423/1792     -491/7168   7753/3072 -18509/5120  3535/1024 -2279/1024  953/1024 -1637/7168  2689/107520; ...
                  2689/107520 -36527/35840  4259/5120   6497/15360 -475/1024  1541/5120 -639/5120  1087/35840  -59/17920; ...
                   -59/17920    1175/21504 -1165/1024   1135/1024    25/3072  -251/5120   25/1024   -45/7168     5/7168];
            D(2:4, 1:9) = A;
            D(m-1:m+1, m-7:end) = -rot90(A,2);
            for i = 5:m-2
                D(i, i-4:i+3) = [5/7168 -49/5120 245/3072 -1225/1024 1225/1024 -245/3072 49/5120 -5/7168];
            end
    end
    D = (1/dx).*D;
end
end

I would extract the else statement in a separate function; the cognitive complexity is too high. Can we make these functions private (like divGral1D)? Does it make sense? It's been a while since I have coded in Matlab but I think we can benefit from reducing the scope (while still having modularity).

mdumett added 2 commits April 12, 2025 05:22
Typos in grad2DPeriodic and grad3DPeriodic fixed bfore removing those files. Just a sanity check. In addition, removing repeated rows in addBC2D and addScalar2D to speed up these codes. addBC2D will be removed. Again, having a better code before removing it just in case we need to get back to it in the future.
div, grad, and lap extended for non-periodic and periodic bc keeping legacy code. All examples in the MOLE were tested successfully
@mdumett
Copy link
Collaborator Author

mdumett commented Apr 14, 2025

This code has extended div, grad, and lap to include periodic BCs.
All MOLE examples have been tested successfully without legacy problems.

With respect to the conflicts:

  1. The addBC conflicts above refer to files that have been removed and are no longer required. Instead the addScalarBC files have been added.
  2. The conflicts referring the div, grad, and lap files are because the old files have been renamed as divNonPeriodic, etc, and the new div, grad, and lap contain the versions that include periodic BC.

This code can safely be merged with MOLE.

The only thing remaining is the organization of the different directories.

Copy link
Collaborator

@jbrzensk jbrzensk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noted the comments should show that the last arguments are optional, and that there should be error checking for the number of arguments.

I checked the code out for Octave compatibility, as well, and that the examples still work.

The word optional for arguments are not necessary have been included. Explicit constraints to the number of arguments of div, grad, and lap were added.
@mdumett
Copy link
Collaborator Author

mdumett commented Apr 15, 2025

Optional arguments have been made explicit. Constraints to the number of arguments of div, grad, and lap are included.

Fixing error message typos.
@mdumett
Copy link
Collaborator Author

mdumett commented Apr 16, 2025

Thank you for finding the typos. I have fixed them.

@jbrzensk
Copy link
Collaborator

@mdumett Now you need to fix the conflicts before this branch can be merged.
This is probably the easiest way:
Resolve Conflicts

@mdumett
Copy link
Collaborator Author

mdumett commented Apr 16, 2025

Even though examples do not use any longer addBC, maybe it is worth to have that working code in the library. It imposes periodic boundary conditions explicitly and not implicitly as the new code. It might be useful for developers in the future, even though users will not have access to it. What you think? I will still need to resolve the operator conflicts.

@jbrzensk
Copy link
Collaborator

I do not think it is necessary. If someone really wants to see how it could be done, they could go back through the github history and see how it was done.

@mdumett mdumett closed this pull request by merging all changes into master in dfa5a28 Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants