### How can I calculate the center of mass of the following circle and velocity?

252
views
1
6 months ago by

I am aware that this is a silly question, but I wasn't able to figure out how to do it.

where $\mathbf{u}$ is the velocity vector and $\mathbf{x}$  is position coordinates.

\mathbf{X}_c= (x_c,y_c)= \frac{\int_{\Omega_2} \mathbf{x}\ dx}{\int_{\Omega_2} 1\ dx}

\mathbf{U}_c = \frac{\int_{\Omega_2} \mathbf{u}\ dx}{\int_{\Omega_2} 1\ dx}

Community: FEniCS Project

7
6 months ago by
Using the answer to this question, you could could get the centroid in python by doing the following:
from dolfin import *
import numpy as np

mesh = UnitSquareMesh(40, 40)
V = VectorFunctionSpace(mesh, "CG", 1)
R = VectorFunctionSpace(mesh, "R", 0)

position = Function(V)
position.assign(Expression(["x[0]", "x[1]"], element=V.ufl_element()))
c = TestFunction(R)

volume = assemble(Constant(1.0)*dx(domain=mesh))
centroid = assemble(dot(c, position)*dx)
centroid /= volume

# Some random vector field
u = Function(V)
u.vector()[:] = np.random.rand(u.vector().size())

u_c = assemble(dot(c, u)*dx)
u_c /= volume​

The result will be a vector object depending on your linear algebra backend (PETSc, Eigen).

Is it possible extract the components of the above vectors? If so, how can I do it? thanks so much!
written 6 months ago by Cassia
1

Yes, but it depends on what kind of object you'd like to use.

u_c_np = u_c.array() # numpy array
print("u_c = ", u_c_np)

or

u_c0 = u_c[0][0]
u_c1 = u_c[1][0]

The first index (0 and 1) above returns a numpy array with shape (1,), while the second index (0 in both cases) retrieves the float object.

written 6 months ago by Miguel Rodriguez