 # Discussion Forum

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.

## How can I specify forces for each particle at each timepoint?

I have a 2D system of particles immersed in a fluid in a rectangular domain. The goal is to simulate the "run and tumble" trajectory of bacterial swimming, which involves 1 second of swimming in one direction, followed by random reorientation during 0.15 seconds, then repeat.

I have applied Brownian and drag forces, which works fine. I also need to apply a different force to each particle that changes with time, which corresponds to each bacterium's propulsive thrust during swimming.

It seems that the easiest way to do this is to create a CSV which specifies the x and y force vectors at each timepoint for each particle, and then simply apply these during the simulation along with the rest of the forces that can be calculated within the model (e.g. Brownian, drag) in order to simulate the resulting motion of each particle.

I have generated a CSV using code externally with columns: time, Force_x Force_y. I cannot figure out how to use an external file to specify the forces, however. Also, if there is a better way of simulating self-propelling particles (e.g., bacteria, janus particles etc.), I would be open to suggestions. Any help would be greatly appreciated.

1 Reply Last Post Sep 22, 2021, 10:46 a.m. EDT

Posted: 2 years ago

I crawled so you could run. In the end I solved this myself. I wrote a program externally in MatLab which created a text file with three columns. The first column contained the timesteps while the second and third columns contained the x and y components of the force vector. Then I imported this text file into two interpolation functions (Component>Definitions), one for the Force x-component and one for the y-component. The functions were named "Force_x" and "Force_y". Both of these functions accepts "s" as an Argument and outputs "N" as the function. Finally, I added a generic Force to one of the physics modules, chose Specify force>directly, and then simply specified the forces as Force_x(t) and Force_y(t) which I had set up in the last step.

If needed, different forces can be applied to each particle in the simulation. To do this, define the generic force as above and use the particle index (unique to each particle) to seed a random function which picks a random row (i.e. time) within the text file from which to start. For example, for the x component this might look like:

F=Force_x(t+random offset)

where the random offset is a function that you specify to choose a random row of the text file.

Be careful to have a long enough text file that there isn't any overlap between the sections that will be "sampled" by your generic force for different particles. In other words, you want (t+random offset) for particle i to be different for (t+random offset) for any other particle j, keeping in mind that the text file will be sampled from rows (t_1 + random offset) to (t_2 + random offset), where t_1 and t_2 are the first and final timesteps of your entire simulation.

If you have difficulty, contact me and I can help replicate this solution for you.

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.