how to correctly impose periodic BC with zero mean constraint

44 views
Skip to first unread message

matteo...@nottingham.ac.uk

unread,
Nov 27, 2023, 1:45:17 PM11/27/23
to chebfun-users
Hi everyone,

I could not find an example where periodic BC are set together with a zero mean or other constraints. any help?
thanks
Matteo

domain = [0,1];
L = chebop(domain);
mu = @(x) 1./(1+(cos(2*pi*x)));
L.op = @(x,u) diff ( mu(x).*diff(u) );
L.bc = 'periodic';
L \ 1;  % ill-posed

Ian Ajzenszmidt

unread,
Nov 27, 2023, 2:21:27 PM11/27/23
to matteo...@nottingham.ac.uk, chebfun-users
Imposing periodic boundary conditions (PBCs) with a zero mean constraint can be achieved by employing a two-step approach:

    Apply PBCs: Enforce PBCs on the original data to ensure the continuity of the field across the periodic boundaries. This involves replicating the data across the periodic domain and setting the values at corresponding boundary points to be equal.

    Zero-mean correction: After applying PBCs, the mean of the data may not be zero due to the replication process. To correct for this, subtract the mean of the replicated data from each data point. This ensures that the overall mean of the data remains zero even after applying PBCs.

Here's a more detailed explanation of the process:

Step 1: Applying PBCs

    Identify the periodic directions: Determine the directions in which the data is periodic. For instance, in a 2D simulation, the data might be periodic in both the x and y directions.

    Replicate data: Create copies of the original data by replicating it across the periodic directions. For example, if the data is periodic in the x direction, create copies of the data by shifting it to the left and right by the periodic length.

    Set boundary values: Set the values at corresponding boundary points in the replicated data to be equal. This ensures that the data is continuous across the periodic boundaries.

Step 2: Zero-mean correction

    Calculate the mean: Compute the mean of the replicated data. This mean value represents the average shift introduced by the replication process.

    Subtract mean: Subtract the calculated mean from each data point in the replicated data. This corrects for the mean shift and ensures that the overall mean of the data remains zero.

By following these two steps, you can effectively impose PBCs with a zero mean constraint on your data. This approach is particularly useful in computational simulations where maintaining a zero mean is crucial for numerical stability and consistency.



--
You received this message because you are subscribed to the Google Groups "chebfun-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chebfun-user...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/chebfun-users/57aa4843-de02-49ea-be0c-71aef582bbdbn%40googlegroups.com.

Ian Ajzenszmidt

unread,
Nov 27, 2023, 2:33:34 PM11/27/23
to matteo...@nottingham.ac.uk, chebfun-users
Periodic BC with Zero Mean
ChatGPT 4
User
 how to correctly impose periodic BC with zero mean constraint
2 / 2
ChatGPT

Imposing periodic boundary conditions (BCs) with a zero mean constraint in a computational model, such as a simulation in computational fluid dynamics or finite element analysis, involves several steps:

    Define Periodic Boundary Conditions:
        Identify the boundaries where the periodic condition is to be applied.
        Ensure that these boundaries are geometrically and functionally identical.
        Apply a condition that enforces the variable (e.g., velocity, displacement) to be the same at corresponding points on these boundaries.

    Apply Zero Mean Constraint:
        This constraint requires the average value of a variable over the domain or across specific boundaries to be zero.
        Mathematically, this can be represented as the integral of the variable over the domain or boundary being equal to zero.
        In computational models, this often translates to an additional equation or constraint in the system.

    Implementation in Computational Models:
        In software like ANSYS, OpenFOAM, or MATLAB, these conditions are implemented through specific boundary condition settings or through custom scripting.
        For periodic BCs, you often specify 'periodic' or 'cyclic' in the boundary condition settings, and identify the pairs of boundaries that are periodic to each other.
        For the zero mean constraint, you might have to add an additional equation or modify the solver to include this constraint.

    Validation and Testing:
        After setting up the conditions, validate the model by running test cases to ensure the periodicity and the zero mean constraint are correctly implemented.
        Check the results for consistency and physical accuracy.

    Adjustments and Troubleshooting:
        If the model doesn't behave as expected, you may need to refine the mesh, adjust the tolerance levels, or re-check the boundary condition settings.

It's important to refer to the specific documentation of the software you're using, as the exact implementation details can vary. Additionally, consulting with experts in the field or relevant community forums can provide valuable insights and practical tips.

matteo...@nottingham.ac.uk

unread,
Nov 28, 2023, 5:19:31 AM11/28/23
to chebfun-users
of course the RHS was wrong here (it has to be zero mean). Replace it with x instead.
still the problem is ill-posed, defined up to a constant. 
while it's easy to add a constraint for Dirichlet and Neumann BC (just adding one more element to the bc structure, not sure how to do it for periodic BC

Nick Hale

unread,
Nov 28, 2023, 7:47:26 AM11/28/23
to chebfun-users
Hi Matteo

As far as I recall, it's not possible to append additional constraints to 'periodic' BCs in Chebop.

One way you might get around this is to enforced the periodicity in a more manual way, as below.
(This will use a Chebyshev basis rather than a trigonometric one.) In this manner you can append
an additional constraint, although (a) you will be blasted with a bunch of warnings about not 
having the correct number of boundary conditions and (b) it still doesn't seem to work... 

domain = [0,1];
L = chebop(domain);
mu = @(x) 1./(1.1+(cos(2*pi*x)));
L.op = @(x,u) diff ( mu(x).*diff(u) );
L.bc = @(x,u) [u(0) - u(1) ; deriv(u,0) - deriv(u,1) ; sum(u)];
x = chebfun('x', [0 1]);
L\x

Perhaps it is useful anyway. Good luck.

Nick

PS. Another major problem with your example is that mu(x) has a pole at x = 0.5, causing
NaNs to appear in the discretisation. I adjusted mu slightly in my example about to avoid this.

Ian Ajzenszmidt

unread,
Nov 28, 2023, 8:17:08 AM11/28/23
to Nick Hale, chebfun-users
I could not find an example where periodic BC are set together with a zero mean or other constraints. any help? in Matlab with chebfun or octave programs.

Implementing periodic boundary conditions (PBC) with a zero mean constraint in MATLAB using Chebfun or in Octave can be a bit intricate but definitely achievable. Chebfun is particularly well-suited for this task due to its ability to handle functions and differential equations effectively.

Here's a general outline of how you can approach this in MATLAB with Chebfun or in Octave:

Using MATLAB with Chebfun

  1. Define Your Domain:

    • Start by defining the domain of your problem where you want to apply PBC.
  2. Create a Chebfun:

    • Use Chebfun to create a function over the domain. Chebfun allows you to work with functions very efficiently in MATLAB.
  3. Apply Periodic Boundary Conditions:

    • In Chebfun, you can specify periodic boundary conditions when you create a Chebyshev function. For instance, you can use chebfun('f', [a, b], 'trig') for a periodic function f on the domain [a, b].
  4. Enforce Zero Mean Constraint:

    • After defining your function with periodic boundary conditions, you need to adjust it to ensure a zero mean. This can be done by subtracting the mean from the function.
    • You can calculate the mean using Chebfun's mean function and then modify your Chebfun accordingly.
  5. Verification:

    • Finally, verify that your function satisfies both the periodic boundary conditions and the zero mean constraint. This can be done by plotting the function or checking the values at the boundaries and the mean.

Using Octave

  1. Define the Domain and Function:

    • Define the domain and create a discrete representation of your function. Octave doesn’t have Chebfun, so you’ll be working with arrays.
  2. Apply Periodic Boundary Conditions:

    • Implement PBC manually by ensuring that the values at the boundaries of your array are equivalent.
  3. Enforce Zero Mean Constraint:

    • Calculate the mean of the array and adjust the values to ensure the mean is zero. This may involve iteratively modifying the values while keeping the PBC intact.
  4. Verification:

    • As with MATLAB, verify that your implementation is correct.

Example in MATLAB with Chebfun:

matlab
% Define the domain dom = [0, 2*pi]; % Create a periodic chebfun f = chebfun(@(x) sin(x), dom, 'trig'); % Adjust for zero mean f = f - mean(f); % Verification plot(f); assert(abs(mean(f)) < 1e-6, 'Mean is not zero');

Notes

  • This is a basic outline. The exact implementation can vary based on your specific problem.
  • Chebfun in MATLAB is more advanced compared to Octave for this task, especially with its 'trig' flag for periodic functions.
  • Ensure that the modifications for zero mean don't violate the periodic boundary conditions.

Remember, the implementation details might vary depending on the complexity of your function and the specific requirements of your problem.

User
I could not find an example where periodic BC are set together with a zero mean or other constraints. any help in matlab / octave with chebfun.

Creating an example in MATLAB using Chebfun where you set periodic boundary conditions (BCs) along with a zero mean constraint can be quite specific and nuanced, depending on the type of function or problem you are dealing with. However, I can guide you through a general approach to achieve this:

Steps to Implement Periodic BCs with a Zero Mean Constraint using Chebfun in MATLAB:

  1. Install and Initialize Chebfun:

    • Ensure that Chebfun is installed in your MATLAB environment.
    • Initialize Chebfun by adding it to your MATLAB path.
  2. Define the Domain:

    • Specify the domain over which you want to apply the periodic BCs. For example, this could be [0, 2*pi] for a function with periodicity over one cycle of sine or cosine.
  3. Create a Chebfun Object with Periodic Boundary Conditions:

    • In Chebfun, you can create a function with periodic BCs using the 'trig' flag. For example:
      matlab
    • mean_f = mean(f); f = f - mean_f;
    • This step modifies f so that its average value over the domain is zero.
  1. Validate the Conditions:

    • Ensure that your function satisfies both the periodic BCs and the zero mean constraint. You can plot f and also check the mean to ensure it's close to zero.

Example Code:

Here's a simple example where we create a periodic function with a zero mean:

matlab
% Define the domain domain = [0, 2*pi]; % Create a periodic function (e.g., sin(x)) f = chebfun(@(x) sin(x), domain, 'trig'); % Adjust the function to have zero mean f = f - mean(f); % Validate the function plot(f); title('Periodic Function with Zero Mean'); disp(['Mean of the function: ', num2str(mean(f))]);

In this example, sin(x) is used as a base function. This function is periodic over the domain [0, 2*pi], and by subtracting its mean, we enforce the zero mean constraint.

Things to Consider:

  • The choice of the base function (someFunction(x) in the example) will depend on your specific application or problem.
  • Always validate that the final function adheres to both the periodic BCs and the zero mean constraint, as these modifications can sometimes have unexpected effects.

This example should give you a starting point to work with periodic BCs and zero mean constraints in MATLAB using Chebfun. The approach would be similar in Octave, though the specific functions and syntax might vary slightly.


    • f = chebfun(@(x) someFunction(x), domain, 'trig');
    • This command creates a Chebfun object f which is inherently periodic over the specified domain.
  • Apply the Zero Mean Constraint:

    • To enforce a zero mean, you can adjust the function by subtracting its mean:
      matlab



--
You received this message because you are subscribed to the Google Groups "chebfun-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chebfun-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages