Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

Manual Backward Euler Time Stepping in COMSOL

Please login with a confirmed email address before reporting spam

I would like to write a Matlab script to solve the heat equation. (The actual application involves a much, much more complicated equation, but the heat equation can serve as a simple example.) I would like to have have full control of the time stepping. Specifically I would like to discretize the problem in time, a priori, as

u^{k+1} - u^k - dt*\Delta u^{k+1} = 0 ,

solve this as a stationary weak form problem, then copy u^{k+1} --> u^k, and solve again, without modifying the mesh. I have no problem doing this once; I can define dt and u^k and solve for u^{k+1}. My question is this: how do I copy copy u^{k+1} --> u^k and solve again? I have been unable to fine the script command in 3.5 for doing this.

Best,

Steven M. Wise
Mathematics Department
University of Tennessee

3 Replies Last Post Jun 5, 2012, 1:43 p.m. EDT

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago May 5, 2010, 5:06 p.m. EDT
Hi Steven,
funny coincidence that you should ask this now, because it is possible to do real quick (without scripting) with the new Time discrete solver operation in COMSOL 4.0. You should have the 4.0 package on your desk within days from now. Use the Time discrete solver feature to find the solution to time-dependent problems which have already been discretized in time using for example the prev operator or the bdf operator. You can read more in the COMSOL 4.0 reference guide, starting on page 166.

Here is how you set up your scheme:

The time discrete solver enables a new operator, prev. Using your notation:
* u^{k+1} is denoted u,
* u^{k} is denoted prev(u,1),
* u^{k-1} is denoted prev(u,2) etc.

Note that dt is available as the internal timestep name.

On the picture general_form_input.png below you'll see how to enter the following equation:
(u^{k+1} - u^k)/dt - div(grad(u^{k+1}) = 0
By setting f to -(u-prev(u,1))/timestep in the PDE General form interface.

In order to use the time discrete solver, you need to define a Time Dependent study. Once the solver sequence is generated:
* remove the Time feature in the Solver Sequence,
* add a Time discrete feature instead (you will find it by right-clicking on Solver Sequence 1 > Other > Time Discrete),
* then tune solver setting as you which. I recommend to use "Steps taken by solver" in Output > Times to store. It will enable postprocessing of expressions using prev operator.

The attached time_discrete_demo.mph is created with COMSOL Multiphysics 4.0 + Hotfix 1.

Niklas Rom, COMSOL

I would like to write a Matlab script to solve the heat equation. (The actual application involves a much, much more complicated equation, but the heat equation can serve as a simple example.) I would like to have have full control of the time stepping. Specifically I would like to discretize the problem in time, a priori, as

u^{k+1} - u^k - dt*\Delta u^{k+1} = 0 ,

solve this as a stationary weak form problem, then copy u^{k+1} --> u^k, and solve again, without modifying the mesh. I have no problem doing this once; I can define dt and u^k and solve for u^{k+1}. My question is this: how do I copy copy u^{k+1} --> u^k and solve again? I have been unable to fine the script command in 3.5 for doing this.

Best,

Steven M. Wise
Mathematics Department
University of Tennessee


Hi Steven, funny coincidence that you should ask this now, because it is possible to do real quick (without scripting) with the new [i]Time discrete solver[/i] operation in COMSOL 4.0. You should have the 4.0 package on your desk within days from now. Use the Time discrete solver feature to find the solution to time-dependent problems which have already been discretized in time using for example the [b]prev[/b] operator or the [b]bdf[/b] operator. You can read more in the COMSOL 4.0 reference guide, starting on page 166. Here is how you set up your scheme: The time discrete solver enables a new operator, [b]prev[/b]. Using your notation: * u^{k+1} is denoted u, * u^{k} is denoted prev(u,1), * u^{k-1} is denoted prev(u,2) etc. Note that dt is available as the internal [b]timestep[/b] name. On the picture [b]general_form_input.png[/b] below you'll see how to enter the following equation: (u^{k+1} - u^k)/dt - div(grad(u^{k+1}) = 0 By setting [b]f[/b] to [b]-(u-prev(u,1))/timestep[/b] in the PDE General form interface. In order to use the time discrete solver, you need to define a Time Dependent study. Once the solver sequence is generated: * remove the Time feature in the Solver Sequence, * add a Time discrete feature instead (you will find it by right-clicking on [b]Solver Sequence 1 > Other > Time Discrete[/b]), * then tune solver setting as you which. I recommend to use "Steps taken by solver" in Output > Times to store. It will enable postprocessing of expressions using prev operator. The attached [b]time_discrete_demo.mph[/b] is created with COMSOL Multiphysics 4.0 + Hotfix 1. Niklas Rom, COMSOL [QUOTE] I would like to write a Matlab script to solve the heat equation. (The actual application involves a much, much more complicated equation, but the heat equation can serve as a simple example.) I would like to have have full control of the time stepping. Specifically I would like to discretize the problem in time, a priori, as u^{k+1} - u^k - dt*\Delta u^{k+1} = 0 , solve this as a stationary weak form problem, then copy u^{k+1} --> u^k, and solve again, without modifying the mesh. I have no problem doing this once; I can define dt and u^k and solve for u^{k+1}. My question is this: how do I copy copy u^{k+1} --> u^k and solve again? I have been unable to fine the script command in 3.5 for doing this. Best, Steven M. Wise Mathematics Department University of Tennessee [/QUOTE]


Please login with a confirmed email address before reporting spam

Posted: 1 decade ago May 5, 2010, 5:26 p.m. EDT
Niklas,

Thank you so much for the help. In fact, I had been trying the Time Discrete Solver function in COMSOL 4.0 within the Time-Dependent framework, but I couldn't find any documentation on the ``prev" operator. (I did get the bdf operator to work and I assumed that the prev operator had a similar syntax.) Even now when I looked back at the guide from page 166 I didn't see its usage clearly defined. I reverted to 3.5 after I didn't have any luck in 4.0.

But in any case, I think I can get by with your quite clear explanation. Thanks again.

Steven Wise


Hi Steven,
funny coincidence that you should ask this now, because it is possible to do real quick (without scripting) with the new Time discrete solver operation in COMSOL 4.0. You should have the 4.0 package on your desk within days from now. Use the Time discrete solver feature to find the solution to time-dependent problems which have already been discretized in time using for example the prev operator or the bdf operator. You can read more in the COMSOL 4.0 reference guide, starting on page 166.

Here is how you set up your scheme:

The time discrete solver enables a new operator, prev. Using your notation:
* u^{k+1} is denoted u,
* u^{k} is denoted prev(u,1),
* u^{k-1} is denoted prev(u,2) etc.

Note that dt is available as the internal timestep name.

On the picture general_form_input.png below you'll see how to enter the following equation:
(u^{k+1} - u^k)/dt - div(grad(u^{k+1}) = 0
By setting f to -(u-prev(u,1))/timestep in the PDE General form interface.

In order to use the time discrete solver, you need to define a Time Dependent study. Once the solver sequence is generated:
* remove the Time feature in the Solver Sequence,
* add a Time discrete feature instead (you will find it by right-clicking on Solver Sequence 1 > Other > Time Discrete),
* then tune solver setting as you which. I recommend to use "Steps taken by solver" in Output > Times to store. It will enable postprocessing of expressions using prev operator.

The attached time_discrete_demo.mph is created with COMSOL Multiphysics 4.0 + Hotfix 1.

Niklas Rom, COMSOL

I would like to write a Matlab script to solve the heat equation. (The actual application involves a much, much more complicated equation, but the heat equation can serve as a simple example.) I would like to have have full control of the time stepping. Specifically I would like to discretize the problem in time, a priori, as

u^{k+1} - u^k - dt*\Delta u^{k+1} = 0 ,

solve this as a stationary weak form problem, then copy u^{k+1} --> u^k, and solve again, without modifying the mesh. I have no problem doing this once; I can define dt and u^k and solve for u^{k+1}. My question is this: how do I copy copy u^{k+1} --> u^k and solve again? I have been unable to fine the script command in 3.5 for doing this.

Best,

Steven M. Wise
Mathematics Department
University of Tennessee



Niklas, Thank you so much for the help. In fact, I had been trying the Time Discrete Solver function in COMSOL 4.0 within the Time-Dependent framework, but I couldn't find any documentation on the ``prev" operator. (I did get the bdf operator to work and I assumed that the prev operator had a similar syntax.) Even now when I looked back at the guide from page 166 I didn't see its usage clearly defined. I reverted to 3.5 after I didn't have any luck in 4.0. But in any case, I think I can get by with your quite clear explanation. Thanks again. Steven Wise Hi Steven, funny coincidence that you should ask this now, because it is possible to do real quick (without scripting) with the new [i]Time discrete solver[/i] operation in COMSOL 4.0. You should have the 4.0 package on your desk within days from now. Use the Time discrete solver feature to find the solution to time-dependent problems which have already been discretized in time using for example the [b]prev[/b] operator or the [b]bdf[/b] operator. You can read more in the COMSOL 4.0 reference guide, starting on page 166. Here is how you set up your scheme: The time discrete solver enables a new operator, [b]prev[/b]. Using your notation: * u^{k+1} is denoted u, * u^{k} is denoted prev(u,1), * u^{k-1} is denoted prev(u,2) etc. Note that dt is available as the internal [b]timestep[/b] name. On the picture [b]general_form_input.png[/b] below you'll see how to enter the following equation: (u^{k+1} - u^k)/dt - div(grad(u^{k+1}) = 0 By setting [b]f[/b] to [b]-(u-prev(u,1))/timestep[/b] in the PDE General form interface. In order to use the time discrete solver, you need to define a Time Dependent study. Once the solver sequence is generated: * remove the Time feature in the Solver Sequence, * add a Time discrete feature instead (you will find it by right-clicking on [b]Solver Sequence 1 > Other > Time Discrete[/b]), * then tune solver setting as you which. I recommend to use "Steps taken by solver" in Output > Times to store. It will enable postprocessing of expressions using prev operator. The attached [b]time_discrete_demo.mph[/b] is created with COMSOL Multiphysics 4.0 + Hotfix 1. Niklas Rom, COMSOL [QUOTE] I would like to write a Matlab script to solve the heat equation. (The actual application involves a much, much more complicated equation, but the heat equation can serve as a simple example.) I would like to have have full control of the time stepping. Specifically I would like to discretize the problem in time, a priori, as u^{k+1} - u^k - dt*\Delta u^{k+1} = 0 , solve this as a stationary weak form problem, then copy u^{k+1} --> u^k, and solve again, without modifying the mesh. I have no problem doing this once; I can define dt and u^k and solve for u^{k+1}. My question is this: how do I copy copy u^{k+1} --> u^k and solve again? I have been unable to fine the script command in 3.5 for doing this. Best, Steven M. Wise Mathematics Department University of Tennessee [/QUOTE] [/QUOTE]

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Jun 5, 2012, 1:43 p.m. EDT
hello, old topic, would you have the same demo for comsol 4.2? Each time I try the time discrete solver the computation stops at the first step, I don't really understand why.
hello, old topic, would you have the same demo for comsol 4.2? Each time I try the time discrete solver the computation stops at the first step, I don't really understand why.

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.