## How can I convert a numpy array into something mpacts does understand?¶

mpacts arrays are generally initialized from basic python lists. The most convenient way to convert a numpy array into a list is usually the map() function:

```import numpy as np
import mpacts.core.array as ar
mgr = ar.ArrayManager("testmgr") #This could also be a particle container!
a = ar.create_array("Scalar", "a", mgr)
nparray1D = np.random.random( 5 )
list1D    = map( float, nparray1D )
a.set_array( list1D )

v = ar.create_array("Vector", "v", mgr)
nparray3D = np.random.random( (5,3) )
list3D    = map( tuple, nparray3D )
v.set_array( list3D )
```

## How can I do vector operations on data coming from python?¶

The answer is: using array-expressions (unless you can use numpy directly)! In the following example, we assume you have the arrays a and v from above:

```from mpacts.core.expression import Expr, dot, ExpressionCommand
myexpression = Expr( a ).i() == dot( Expr( v ).i(), Expr( v ).i() )
cmd = ExpressionCommand( "test", expression=myexpression() )
cmd.execute()
print a #let's look at the squared norm of v:
```

## How can I do my own calculations in python during a running simulation?¶

In order to do work during a “run_n() or “run_until()” there are two options. The first and recommended way is to create a PythonCommand and add this command to the simulation. This can be done like this:

```#definition of the command
import mpacts.core.command as cmds

class MyStoreForcesCommand( cmds.PythonCommand ):
def __init__(self, simulation, pc, onParticle, gate=cmds.ExecuteAlways ):
cmds.PythonCommand.__init__(self, "StoreForceCommand", simulation("loop_cmds/post_contact_cmds"))
self.set( gate = gate)
self.mgr = pc
self.Forces  =[]

def execute(self):
self.Forces.append( self.mgr["F"][onParticle] )

#usage of the command on ParticleContainer object "Spheres" for particle 10
mycmd = MyStoreForcesCommand( mysim, Spheres, 10 )
#now we can run, and the forces on particle 10 will be stored
mysim.run_until(1)
print mycmd.Forces #view the output after storing
```

Another “quick-and-dirty” way exists, which is only recommended for testing due to its low efficiency. This method is basically split up one “run_n” or “run_until” into smaller pieces:

```import numpy as np
Forces = []
endTime = 1
times = np.linspace(0, 1, 100) #between 0 and 1 in 100 steps
for t in times:
mysim.run_until(t)
Forces.append( Spheres["F"] )
```

Note

This will make CTRL-C no longer useful, since CTRL-C just ends a single “run_n()” or “run_until()”, so one would have to press CTRL-C a hundred times to actually stop this simulation. However the simulation can also be killed (in linux) by pressing CTRL-\

## How can I conditionally execute a command? How to execute a command only every now and then?¶

To conditionally execute a command in a command list, you can use “CommandGate”s. Those are set as a property of a command (“gate”), and they are checked by all CommandLists to decide whether or not to execute a command. A number of standard “gates” are provided in the Command package. Example:

```import mpacts.core.command as cmds
import mpacts.core.simulation as sim
mysim = sim.Simulation("mysim",timestep=0.1)
#... make Spheres particle container
#... for command definition, see above:
mycmd = MyStoreForcesCommand( mysim, Spheres, 10, gate=cmds.ExecuteEveryNtimes( N=10 ) )
mysim.run_n(1000)# will now only execute MyStoreForcesCommand 100 times...
```