Dear community friends,
I am using COMSOL with the Python interface to solve the convection-diffusion equation. While comparing several COMSOL models with different parameters, I encountered an issue when setting initial conditions.
I first created an n×n matrix and saved it, then imported it into the COMSOL model as the initial condition. However, I noticed small discrepancies between the initial values in the COMSOL model and the values in the n×n matrix.
Is there a way to ensure that the initial values in the COMSOL model match the imported n×n matrix exactly?
Here is my code:
def setup_initial_condition(self, java_model, physics, initial_condition_file, debug_flag):
"""
Set up initial conditions
Parameters:
-----------
java_model : object
COMSOL Java model object
physics : object
Physics field object
initial_condition_file : str
Path to initial condition file
debug_flag : bool
Whether to enable debug mode
"""
try:
if initial_condition_file:
# Create and set up interpolation function
function_group = java_model.func()
function_group.create('int1', 'Interpolation')
int_func = function_group.get('int1')
int_func.set('source', 'file')
int_func.set('filename', initial_condition_file)
int_func.set('interp', 'neighbor')
# Debug output
if debug_flag:
init = physics.feature('init1')
print("\nProperties of initial condition feature:")
for prop in init.properties():
print(f"- {prop}")
# Set initial value
physics.feature('init1').set('initialValue', 'int1(x,y)')
print("Initial condition setup completed")
except Exception as e:
print(f"Error setting initial condition: {str(e)}")
raise
I tried changing the interpolation method : using int_func.set('interp', 'neighbor'), but it didn't help. There are still small differences between the model and the initial matrix.