def test_is_mesh_connectivity_required(self):
     solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
     fake_bool = 0  # compare to output in test/SolverInterface.cpp
     fake_mesh_id = 0
     self.assertEqual(
         fake_bool,
         solver_interface.is_mesh_connectivity_required(fake_mesh_id))
 def test_read_write_block_scalar_data_single_float(self):
     solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
     write_data = 8
     with self.assertRaises(TypeError):
         solver_interface.write_block_scalar_data(1, 1, write_data)
     with self.assertRaises(TypeError):
         solver_interface.read_block_scalar_data(1, 1)
 def test_get_data_id(self):
     solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
     fake_mesh_id = 0  # compare to test/SolverInterface.cpp, fake_mesh_id
     fake_data_name = "FakeData"  # compare to test/SolverInterface.cpp, fake_data_name
     fake_data_id = 15  # compare to test/SolverInterface.cpp, fake_data_ID
     data_id = solver_interface.get_data_id(fake_data_name, fake_mesh_id)
     self.assertTrue(data_id == fake_data_id)
 def test_set_mesh_access_region(self):
     solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
     fake_mesh_id = 0  # compare to test/SolverInterface.cpp, fake_mesh_id
     fake_dimension = 3  # compare to test/SolverInterface.cpp, fake_dimensions
     fake_bounding_box = np.arange(fake_dimension * 2)
     solver_interface.set_mesh_access_region(fake_mesh_id,
                                             fake_bounding_box)
 def test_get_mesh_id(self):
     solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
     # TODO: it would be nice to be able to mock the output of the interface
     # directly in the test, not in test/SolverInterface.hpp
     fake_mesh_id = 0  # compare to test/SolverInterface.hpp, fake_mesh_id
     actual_output = solver_interface.get_mesh_id("testMesh")
     self.assertEqual(fake_mesh_id, actual_output)
 def test_set_mesh_vertex_tuple(self):
     solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
     fake_mesh_id = 0  # compare to test/SolverInterface.cpp, fake_mesh_id
     fake_dimension = 3  # compare to test/SolverInterface.cpp, fake_dimensions
     position = tuple(np.random.rand(fake_dimension))
     vertex_id = solver_interface.set_mesh_vertex(fake_mesh_id, position)
     self.assertTrue(0 == vertex_id)
 def test_read_write_block_vector_data_mixed(self):
     solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
     write_data = [(3, 7, 8), (7, 6, 5)]
     solver_interface.write_block_vector_data(1, np.array([1, 2]),
                                              write_data)
     read_data = solver_interface.read_block_vector_data(
         1, np.array([1, 2]))
     self.assertTrue(np.array_equal(write_data, read_data))
 def test_read_write_block_vector_data(self):
     solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
     write_data = np.array([[3, 7, 8], [7, 6, 5]], dtype=np.double)
     solver_interface.write_block_vector_data(1, np.array([1, 2]),
                                              write_data)
     read_data = solver_interface.read_block_vector_data(
         1, np.array([1, 2]))
     self.assertTrue(np.array_equal(write_data, read_data))
 def test_get_dimensions(self):
     solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
     # TODO: it would be nice to be able to mock the output of the interface
     # directly in the test, not in test/SolverInterface.hpp
     fake_dimension = 3  # compare to test/SolverInterface.hpp, fake_dimensions
     # TODO: it would be nice to be able to mock the output of the interface
     # directly in the test, not in test/SolverInterface.hpp
     self.assertEqual(fake_dimension, solver_interface.get_dimensions())
 def test_set_mesh_vertices_empty_list(self):
     solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
     fake_mesh_id = 0  # compare to test/SolverInterface.cpp, fake_mesh_id
     positions = []
     n_fake_vertices = 0
     expected_output = np.array(range(n_fake_vertices))
     actual_output = solver_interface.set_mesh_vertices(
         fake_mesh_id, positions)
     self.assertTrue(np.array_equal(expected_output, actual_output))
 def test_get_mesh_vertex_ids_from_positions(self):
     solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
     fake_mesh_id = 0  # compare to test/SolverInterface.cpp, fake_mesh_id
     fake_dimension = 3  # compare to test/SolverInterface.cpp, fake_dimensions
     n_fake_vertices = 3  # compare to test/SolverInterface.cpp, n_fake_vertices
     positions = np.random.rand(n_fake_vertices, fake_dimension)
     fake_vertex_ids = range(n_fake_vertices)
     vertex_ids = solver_interface.get_mesh_vertex_ids_from_positions(
         fake_mesh_id, positions)
     self.assertTrue(np.array_equal(fake_vertex_ids, vertex_ids))
 def test_set_mesh_vertices(self):
     solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
     fake_mesh_id = 0  # compare to test/SolverInterface.cpp, fake_mesh_id
     fake_dimension = 3  # compare to test/SolverInterface.cpp, fake_dimensions
     n_fake_vertices = 3  # compare to test/SolverInterface.cpp, n_fake_vertices
     positions = np.random.rand(n_fake_vertices, fake_dimension)
     expected_output = np.array(range(n_fake_vertices))
     actual_output = solver_interface.set_mesh_vertices(
         fake_mesh_id, positions)
     self.assertTrue(np.array_equal(expected_output, actual_output))
Esempio n. 13
0
    def __init__(self,
                 adapter_config_filename='precice-adapter-config.json',
                 interpolation_strategy=GeneralInterpolationExpression):

        self._config = Config(adapter_config_filename)

        self._solver_name = self._config.get_solver_name()

        self._interface = precice.Interface(self._solver_name, 0, 1)
        self._interface.configure(self._config.get_config_file_name())
        self._dimensions = self._interface.get_dimensions()

        self._coupling_subdomain = None  # initialized later
        self._mesh_fenics = None  # initialized later
        self._coupling_bc_expression = None  # initialized later
        self._fenics_dimensions = None  # initialized later

        # coupling mesh related quantities
        self._coupling_mesh_vertices = None  # initialized later
        self._mesh_name = self._config.get_coupling_mesh_name()
        self._mesh_id = self._interface.get_mesh_id(self._mesh_name)
        self._vertex_ids = None  # initialized later
        self._n_vertices = None  # initialized later

        # write data related quantities (write data is written by this solver to preCICE)
        self._write_data_name = self._config.get_write_data_name()
        self._write_data_id = self._interface.get_data_id(
            self._write_data_name, self._mesh_id)
        self._write_data = None  # a numpy 1D array with the values like it is used by precice (The 2D-format of values is (d0x, d0y, d1x, d1y, ..., dnx, dny) The 3D-format of values is (d0x, d0y, d0z, d1x, d1y, d1z, ..., dnx, dny, dnz))
        self._write_function_type = None  # stores whether write function is scalar or vector valued

        # read data related quantities (read data is read by this solver from preCICE)
        self._read_data_name = self._config.get_read_data_name()
        self._read_data_id = self._interface.get_data_id(
            self._read_data_name, self._mesh_id)
        self._read_data = None  # a numpy 1D array with the values like it is used by precice (The 2D-format of values is (d0x, d0y, d1x, d1y, ..., dnx, dny) The 3D-format of values is (d0x, d0y, d0z, d1x, d1y, d1z, ..., dnx, dny, dnz))
        self._read_function_type = None  # stores whether read function is scalar or vector valued

        # numerics
        self._precice_tau = None
        self._my_expression = interpolation_strategy

        # checkpointing
        self._u_cp = None  # checkpoint for temperature inside domain
        self._t_cp = None  # time of the checkpoint
        self._n_cp = None  # timestep of the checkpoint

        # function space
        self._function_space = None
        self._dss = None  # measure for boundary integral

        # Nodes with Dirichlet and Force-boundary
        self._Dirichlet_Boundary = None  # stores a dirichlet boundary (if provided)
        self._has_force_boundary = None  # stores whether force_boundary exists
    def __init__(self, mbdHelper, configFileName="../precice-config.xml"):
        self.mbd = mbdHelper
        self.interface = precice.Interface("Structure_Solver", configFileName,
                                           0, 1)  # proc no, nprocs
        self.dim = self.interface.get_dimensions()
        nodes = self.mbd.getNodes()

        # if self.dim == 2:
        # self.nodesid = np.where(nodes[:,2] < 1e-6)
        # nodes = nodes[self.nodesid]

        self.nnodes = len(nodes)

        nmeshID = self.interface.get_mesh_id("Structure_Nodes")
        self.nodeVertexIDs = self.nnodes * [0.0]
        #self.interface.set_mesh_vertices(nmeshID, self.nnodes, np.ravel(nodes[:,:self.dim]), self.nodeVertexIDs)
        #self.interface.set_mesh_vertices(nmeshID, np.ravel(nodes[:,:self.dim]))
        self.interface.set_mesh_vertices(nmeshID,
                                         np.array(nodes[:, :self.dim]))
        #self.displacements = np.array(self.dim*self.nnodes*[0.0])
        self.displacements = np.zeros((self.nnodes, self.dim))

        # ccs = self.mbd.getCellCenters()
        # self.ncells = len(ccs)
        # cmeshID = self.interface.get_mesh_id("Structure_CellCenters")
        # self.cellVertexIDs = self.ncells*[0.0]
        # self.interface.set_mesh_vertices(cmeshID, self.ncells, np.ravel(ccs[:,:self.dim]), self.cellVertexIDs)
        self.force = np.array(self.dim * self.nnodes * [0.0])

        self.displacementsID = self.interface.get_data_id(
            "DisplacementDelta", nmeshID)
        self.forceID = self.interface.get_data_id("Force", nmeshID)

        self.dt = self.interface.initialize()
        self.mbd.controlDict["timeStep"] = self.dt
        self.mbd.initializeMBDyn()
        #import ipdb; ipdb.set_trace()

        if (self.interface.is_action_required(
                precice.action_write_initial_data())):
            print("Writing initial data")
            self.interface.write_block_vector_data(self.displacementsID,
                                                   self.nodeVertexIDs,
                                                   self.displacements)
            self.interface.mark_action_fulfilled(
                precice.action_write_initial_data())

        self.interface.initialize_data()
        if (self.interface.is_read_data_available()):
            print("Reading initial data")
            self.force = self.interface.read_block_vector_data(
                self.forceID, self.nodeVertexIDs)
        print(4)
 def test_get_mesh_vertex_ids_from_positions_mixed(self):
     solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
     fake_mesh_id = 0  # compare to test/SolverInterface.cpp, fake_mesh_id
     fake_dimension = 3  # compare to test/SolverInterface.cpp, fake_dimensions
     n_fake_vertices = 3  # compare to test/SolverInterface.cpp, n_fake_vertices
     positions = np.random.rand(n_fake_vertices, fake_dimension)
     positions = list(
         tuple(positions[i, j] for j in range(positions.shape[1]))
         for i in range(positions.shape[0]))
     fake_vertex_ids = range(n_fake_vertices)
     vertex_ids = solver_interface.get_mesh_vertex_ids_from_positions(
         fake_mesh_id, positions)
     self.assertTrue(np.array_equal(fake_vertex_ids, vertex_ids))
 def test_get_mesh_vertices(self):
     solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
     fake_mesh_id = 0  # compare to test/SolverInterface.cpp, fake_mesh_id
     n_fake_vertices = 3  # compare to test/SolverInterface.cpp, n_fake_vertices
     fake_dimension = 3  # compare to test/SolverInterface.cpp, fake_dimensions
     fake_vertices = np.zeros((n_fake_vertices, fake_dimension))
     for i in range(n_fake_vertices):
         fake_vertices[i, 0] = i
         fake_vertices[i, 1] = i + n_fake_vertices
         fake_vertices[i, 2] = i + 2 * n_fake_vertices
     vertices = solver_interface.get_mesh_vertices(fake_mesh_id,
                                                   range(n_fake_vertices))
     self.assertTrue(np.array_equal(fake_vertices, vertices))
    def test_read_write_vector_data_non_contiguous(self):
        """
        Tests behaviour of solver interface, if a non contiguous array is passed to the interface.

        Note: Check whether np.ndarray is contiguous via np.ndarray.flags.
        """
        solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
        dummy_array = np.random.rand(3, 3)
        write_data = dummy_array[:, 1]
        assert (write_data.flags["C_CONTIGUOUS"] is False)
        solver_interface.write_vector_data(1, 1, write_data)
        read_data = solver_interface.read_vector_data(1, 1)
        self.assertTrue(np.array_equal(write_data, read_data))
 def test_get_mesh_vertices_and_ids(self):
     solver_interface = precice.Interface("test", "dummy.xml", 0, 1)
     fake_mesh_id = 0  # compare to test/SolverInterface.cpp, fake_mesh_id
     n_fake_vertices = 3  # compare to test/SolverInterface.cpp, n_fake_vertices
     fake_dimension = 3  # compare to test/SolverInterface.cpp, fake_dimensions
     vertex_ids = np.arange(n_fake_vertices)
     coordinates = np.zeros((n_fake_vertices, fake_dimension))
     for i in range(n_fake_vertices):
         coordinates[i, 0] = i * fake_dimension
         coordinates[i, 1] = i * fake_dimension + 1
         coordinates[i, 2] = i * fake_dimension + 2
     fake_ids, fake_coordinates = solver_interface.get_mesh_vertices_and_ids(
         fake_mesh_id)
     self.assertTrue(np.array_equal(fake_ids, vertex_ids))
     self.assertTrue(np.array_equal(fake_coordinates, coordinates))
Esempio n. 19
0
    def __init__(self, adapter_config_filename='precice-adapter-config.json'):

        self._config = Config(adapter_config_filename)

        self._solver_name = self._config.get_solver_name()

        self._interface = precice.Interface(self._solver_name, 0, 1)
        self._interface.configure(
            self._config.get_config_file_name())  # raises error
        self._dimensions = self._interface.get_dimensions()

        self._coupling_subdomain = None  # initialized later
        self._mesh_fenics = None  # initialized later
        self._coupling_bc_expression = None  # initialized later
        self._fenics_dimensions = None  # initialized later
        self._vector_function = None  # initialized later

        ## coupling mesh related quantities
        self._coupling_mesh_vertices = None  # initialized later
        self._mesh_name = self._config.get_coupling_mesh_name()
        self._mesh_id = self._interface.get_mesh_id(self._mesh_name)
        self._vertex_ids = None  # initialized later
        self._n_vertices = None  # initialized later

        ## write data related quantities (write data is written by this solver to preCICE)
        self._write_data_name = self._config.get_write_data_name()
        self._write_data_id = self._interface.get_data_id(
            self._write_data_name, self._mesh_id)
        self._write_data = None  #_write_data is a vector with the values like it is used by precice (The 2D-format of values is (d0x, d0y, d1x, d1y, ..., dnx, dny) The 3D-format of values is (d0x, d0y, d0z, d1x, d1y, d1z, ..., dnx, dny, dnz))

        ## read data related quantities (read data is read by this solver from preCICE)
        self._read_data_name = self._config.get_read_data_name()
        self._read_data_id = self._interface.get_data_id(
            self._read_data_name, self._mesh_id)
        self._read_data = None  #a numpy 1D array with the values like it is used by precice (The 2D-format of values is (d0x, d0y, d1x, d1y, ..., dnx, dny) The 3D-format of values is (d0x, d0y, d0z, d1x, d1y, d1z, ..., dnx, dny, dnz))

        ## numerics
        self._precice_tau = None

        ## checkpointing
        self._u_cp = None  # checkpoint for temperature inside domain
        self._t_cp = None  # time of the checkpoint
        self._n_cp = None  # timestep of the checkpoint

        #function space
        self._function_space = None
        self.dss = None  #intgration domain for evaluation an integral over the domain
Esempio n. 20
0
 def __init__(self,
              preciceConfigFile,
              participantName,
              config,
              MESH,
              MODEL,
              MAT,
              isNonLinear=False):
     self.interfaces = []
     self.numInterfaces = len(config)
     self.MESH = MESH
     self.MODEL = MODEL
     self.MAT = MAT
     self.LOADS = []
     self.isNonLinear = isNonLinear
     self.participantName = participantName
     self.preciceDt = -1
     self.precice = precice.Interface(participantName, preciceConfigFile, 0,
                                      1)
     self.configure(config)
Esempio n. 21
0
    def __init__(self, adapter_config_filename='precice-adapter-config.json'):

        self._config = Config(adapter_config_filename)

        self._solver_name = self._config.get_solver_name()

        self._interface = precice.Interface(self._solver_name, 0, 1)
        self._interface.configure(self._config.get_config_file_name())
        self._dimensions = self._interface.get_dimensions()

        self._coupling_subdomain = None # initialized later
        self._mesh_fenics = None # initialized later
        self._coupling_bc_expression = None # initialized later

        ## coupling mesh related quantities
        self._coupling_mesh_vertices = None # initialized later
        self._mesh_name = self._config.get_coupling_mesh_name()
        self._mesh_id = self._interface.get_mesh_id(self._mesh_name)
        self._vertex_ids = None # initialized later
        self._n_vertices = None # initialized later

        ## write data related quantities (write data is written by this solver to preCICE)
        self._write_data_name = self._config.get_write_data_name()
        self._write_data_id = self._interface.get_data_id(self._write_data_name, self._mesh_id)
        self._write_data = None

        ## read data related quantities (read data is read by this solver from preCICE)
        self._read_data_name = self._config.get_read_data_name()
        self._read_data_id = self._interface.get_data_id(self._read_data_name, self._mesh_id)
        self._read_data = None

        ## numerics
        self._precice_tau = None

        ## checkpointing
        self._u_cp = None  # checkpoint for temperature inside domain
        self._t_cp = None  # time of the checkpoint
        self._n_cp = None  # timestep of the checkpoint
Esempio n. 22
0
    def __init__(self, adapter_config_filename='precice-adapter-config.json'):

        self._config = Config(adapter_config_filename)

        self._solver_name = self._config.get_solver_name()

        self.comm = mpi_comm_world()
        self.rank = MPI.rank(self.comm)
        self.size = MPI.size(self.comm)

        self._interface = precice.Interface(self._solver_name, self.rank,
                                            self.size)
        self._interface.configure(self._config.get_config_file_name())
        self._dimensions = self._interface.get_dimensions()

        self._coupling_subdomain = None  # initialized later
        self._V_fenics = None  # initialized later

        ## coupling mesh related quantities
        self._coupling_mesh_vertices = None  # initialized later
        self._mesh_name = self._config.get_coupling_mesh_name()
        self._mesh_id = self._interface.get_mesh_id(self._mesh_name)
        self._vertex_ids = None  # initialized later

        ## write data related quantities (write data is written by this solver to preCICE)
        self._write_data_name = self._config.get_write_data_name()
        self._write_data_id = self._interface.get_data_id(
            self._write_data_name, self._mesh_id)
        self._write_data = None

        ## read data related quantities (read data is read by this solver from preCICE)
        self._read_data_name = self._config.get_read_data_name()
        self._read_data_id = self._interface.get_data_id(
            self._read_data_name, self._mesh_id)
        self._read_data = None

        ## numerics
        self._precice_tau = None
    args = parser.parse_args()
except SystemExit:
    print("")
    print("Usage: python ./solverdummy precice-config participant-name mesh-name")    
    quit()

configuration_file_name = args.configurationFileName
participant_name = args.participantName
mesh_name = args.meshName

n = 1

solver_process_index = 0
solver_process_size = 1

interface = precice.Interface(participant_name, configuration_file_name, MPI.COMM_WORLD.Get_rank(), MPI.COMM_WORLD.Get_size())
    
mesh_id = interface.get_mesh_id(mesh_name)

dimensions = interface.get_dimensions()
vertices = np.ones((n, dimensions)) * MPI.COMM_WORLD.Get_rank()

vertex_ids = interface.set_mesh_vertices(mesh_id, vertices)

dt = interface.initialize()
    
while interface.is_coupling_ongoing():
   
    if interface.is_action_required(precice.action_write_iteration_checkpoint()):
        print("DUMMY: Writing iteration checkpoint")
        interface.mark_action_fulfilled(precice.action_write_iteration_checkpoint())
Esempio n. 24
0
    quit()
writeVideoToFile = True if args.write_video else False

print("Starting Fluid Solver...")

configFileName = args.configurationFileName

N = config.n_elem
dx = config.L / N  # element length

print("N: " + str(N))

solverName = "FLUID"

print("Configure preCICE...")
interface = precice.Interface(solverName, configFileName, 0, 1)
print("preCICE configured...")

dimensions = interface.get_dimensions()

velocity = config.velocity_in(0) * np.ones(N + 1)
velocity_n = config.velocity_in(0) * np.ones(N + 1)
pressure = config.p0 * np.ones(N + 1)
pressure_n = config.p0 * np.ones(N + 1)
crossSectionLength = config.a0 * np.ones(N + 1)
crossSectionLength_n = config.a0 * np.ones(N + 1)

if plotting_mode == config.PlottingModes.VIDEO:
    fig, ax = plt.subplots(1)
    if writeVideoToFile:
        FFMpegWriter = manimation.writers['imagemagick']
Esempio n. 25
0
output_mode = config.OutputModes.VTK if args.write_vtk else config.OutputModes.OFF
writeVideoToFile = True if args.write_video else False

print("Starting Fluid Solver...")

configFileName = args.configurationFileName

N = config.n_elem
dx = config.L / N  # element length

print("N: " + str(N))

solverName = "FLUID"

print("Configure preCICE...")
interface = precice.Interface(solverName, 0, 1)
interface.configure(configFileName)
print("preCICE configured...")

dimensions = interface.get_dimensions()

velocity = config.velocity_in(0) * np.ones(N+1)
velocity_n = config.velocity_in(0) * np.ones(N+1)
pressure = config.p0 * np.ones(N+1)
pressure_n = config.p0 * np.ones(N+1)
crossSectionLength = config.a0 * np.ones(N+1)
crossSectionLength_n = config.a0 * np.ones(N+1)


if plotting_mode == config.PlottingModes.VIDEO:
    fig, ax = plt.subplots(1)
Esempio n. 26
0
import numpy as np
import time
import precice

# preCICE setup
participant_name = "Dummy"
config_file_name = "precice-config.xml"
solver_process_index = 0
solver_process_size = 1
interface = precice.Interface(participant_name, config_file_name,
                              solver_process_index, solver_process_size)

mesh_name = "Dummy-Mesh"
mesh_id = interface.get_mesh_id(mesh_name)

lmbda_id = interface.get_data_id("Lmbda", mesh_id)
mu_id = interface.get_data_id("Mu", mesh_id)
gc_id = interface.get_data_id("Gc", mesh_id)

# define coupling mesh
L = 1e-3  # domain size in m
N = 100  # number of cells in each direction
axis = np.linspace(-L / 2, L / 2, N + 1)  # coordinates along one axis
positions = np.transpose(
    [np.tile(axis, len(axis)),
     np.repeat(axis, len(axis))])  # mesh coordinates
vertex_ids = interface.set_mesh_vertices(mesh_id, positions)

precice_dt = interface.initialize()  # pseudo timestep size handled by preCICE

step = 0
Esempio n. 27
0
def main(inflow: 'inflow velocity' = 10,
         viscosity: 'kinematic viscosity' = 1.0,
         density: 'density' = 1.0,
         theta=0.5,
         timestepsize=0.01):

    # mesh and geometry definition
    grid_x_1 = numpy.linspace(-3, -1, 7)
    grid_x_1 = grid_x_1[:-1]
    grid_x_2 = numpy.linspace(-1, -0.3, 8)
    grid_x_2 = grid_x_2[:-1]
    grid_x_3 = numpy.linspace(-0.3, 0.3, 13)
    grid_x_3 = grid_x_3[:-1]
    grid_x_4 = numpy.linspace(0.3, 1, 8)
    grid_x_4 = grid_x_4[:-1]
    grid_x_5 = numpy.linspace(1, 3, 7)
    grid_x = numpy.concatenate(
        (grid_x_1, grid_x_2, grid_x_3, grid_x_4, grid_x_5), axis=None)
    grid_y_1 = numpy.linspace(0, 1.5, 16)
    grid_y_1 = grid_y_1[:-1]
    grid_y_2 = numpy.linspace(1.5, 2, 4)
    grid_y_2 = grid_y_2[:-1]
    grid_y_3 = numpy.linspace(2, 4, 7)
    grid_y = numpy.concatenate((grid_y_1, grid_y_2, grid_y_3), axis=None)
    grid = [grid_x, grid_y]

    topo, geom = mesh.rectilinear(grid)
    domain = topo.withboundary(inflow='left', wall='top,bottom', outflow='right') - \
        topo[18:20, :10].withboundary(flap='left,right,top')

    # Nutils namespace
    ns = function.Namespace()

    # time approximations
    # TR interpolation
    ns._functions['t'] = lambda f: theta * f + (1 - theta) * subs0(f)
    ns._functions_nargs['t'] = 1
    # 1st order FD
    ns._functions['δt'] = lambda f: (f - subs0(f)) / dt
    ns._functions_nargs['δt'] = 1
    # 2nd order FD
    ns._functions['tt'] = lambda f: (1.5 * f - 2 * subs0(f) + 0.5 * subs00(f)
                                     ) / dt
    ns._functions_nargs['tt'] = 1
    # extrapolation for pressure
    ns._functions['tp'] = lambda f: (1.5 * f - 0.5 * subs0(f))
    ns._functions_nargs['tp'] = 1

    ns.nu = viscosity
    ns.rho = density
    ns.uin = inflow
    ns.x0 = geom  # reference geometry
    ns.dbasis = domain.basis('std', degree=1).vector(2)
    ns.d_i = 'dbasis_ni ?meshdofs_n'
    ns.umesh_i = 'dbasis_ni (1.5 ?meshdofs_n - 2 ?oldmeshdofs_n + 0.5 ?oldoldmeshdofs_n ) / ?dt'
    ns.x_i = 'x0_i + d_i'  # moving geometry
    ns.ubasis, ns.pbasis = function.chain([
        domain.basis('std', degree=2).vector(2),
        domain.basis('std', degree=1),
    ])
    ns.F_i = 'ubasis_ni ?F_n'  # stress field
    ns.urel_i = 'ubasis_ni ?lhs_n'  # relative velocity
    ns.u_i = 'umesh_i + urel_i'  # total velocity
    ns.p = 'pbasis_n ?lhs_n'  # pressure

    # initialization of dofs
    meshdofs = numpy.zeros(len(ns.dbasis))
    oldmeshdofs = meshdofs
    oldoldmeshdofs = meshdofs
    oldoldoldmeshdofs = meshdofs
    lhs0 = numpy.zeros(len(ns.ubasis))

    # for visualization
    bezier = domain.sample('bezier', 2)

    # preCICE setup
    configFileName = "../precice-config.xml"
    participantName = "Fluid"
    solverProcessIndex = 0
    solverProcessSize = 1
    interface = precice.Interface(participantName, configFileName,
                                  solverProcessIndex, solverProcessSize)

    # define coupling meshes
    meshName = "Fluid-Mesh"
    meshID = interface.get_mesh_id(meshName)

    couplinginterface = domain.boundary['flap']
    couplingsample = couplinginterface.sample(
        'gauss', degree=2)  # mesh located at Gauss points
    dataIndices = interface.set_mesh_vertices(meshID,
                                              couplingsample.eval(ns.x0))

    # coupling data
    writeData = "Force"
    readData = "Displacement"
    writedataID = interface.get_data_id(writeData, meshID)
    readdataID = interface.get_data_id(readData, meshID)

    # initialize preCICE
    precice_dt = interface.initialize()
    dt = min(precice_dt, timestepsize)

    # boundary conditions for fluid equations
    sqr = domain.boundary['wall,flap'].integral('urel_k urel_k d:x0' @ ns,
                                                degree=4)
    cons = solver.optimize('lhs', sqr, droptol=1e-15)
    sqr = domain.boundary['inflow'].integral(
        '((urel_0 - uin)^2 + urel_1^2) d:x0' @ ns, degree=4)
    cons = solver.optimize('lhs', sqr, droptol=1e-15, constrain=cons)

    # weak form fluid equations
    res = domain.integral('t(ubasis_ni,j (u_i,j + u_j,i) rho nu d:x)' @ ns,
                          degree=4)
    res += domain.integral('(-ubasis_ni,j p δ_ij + pbasis_n u_k,k) d:x' @ ns,
                           degree=4)
    res += domain.integral('rho ubasis_ni δt(u_i d:x)' @ ns, degree=4)
    res += domain.integral('rho ubasis_ni t(u_i,j urel_j d:x)' @ ns, degree=4)

    # weak form for force computation
    resF = domain.integral('(ubasis_ni,j (u_i,j + u_j,i) rho nu d:x)' @ ns,
                           degree=4)
    resF += domain.integral('tp(-ubasis_ni,j p δ_ij d:x)' @ ns, degree=4)
    resF += domain.integral('pbasis_n u_k,k d:x' @ ns, degree=4)
    resF += domain.integral('rho ubasis_ni tt(u_i d:x)' @ ns, degree=4)
    resF += domain.integral('rho ubasis_ni (u_i,j urel_j d:x)' @ ns, degree=4)
    resF += couplinginterface.sample('gauss',
                                     4).integral('ubasis_ni F_i d:x' @ ns)
    consF = numpy.isnan(
        solver.optimize('F',
                        couplinginterface.sample('gauss',
                                                 4).integral('F_i F_i' @ ns),
                        droptol=1e-10))

    # boundary conditions mesh displacements
    sqr = domain.boundary['inflow,outflow,wall'].integral('d_i d_i' @ ns,
                                                          degree=2)
    meshcons0 = solver.optimize('meshdofs', sqr, droptol=1e-15)

    # weak form mesh displacements
    meshsqr = domain.integral('d_i,x0_j d_i,x0_j d:x0' @ ns, degree=2)

    # better initial guess: start from Stokes solution, comment out for comparison with other solvers
    #res_stokes = domain.integral('(ubasis_ni,j ((u_i,j + u_j,i) rho nu - p δ_ij) + pbasis_n u_k,k) d:x' @ ns, degree=4)
    #lhs0 = solver.solve_linear('lhs', res_stokes, constrain=cons, arguments=dict(meshdofs=meshdofs, oldmeshdofs=oldmeshdofs, oldoldmeshdofs=oldoldmeshdofs, oldoldoldmeshdofs=oldoldoldmeshdofs, dt=dt))
    lhs00 = lhs0

    timestep = 0
    t = 0

    while interface.is_coupling_ongoing():

        # read displacements from interface
        if interface.is_read_data_available():
            readdata = interface.read_block_vector_data(
                readdataID, dataIndices)
            coupledata = couplingsample.asfunction(readdata)
            sqr = couplingsample.integral(((ns.d - coupledata)**2).sum(0))
            meshcons = solver.optimize('meshdofs',
                                       sqr,
                                       droptol=1e-15,
                                       constrain=meshcons0)
            meshdofs = solver.optimize('meshdofs', meshsqr, constrain=meshcons)

        # save checkpoint
        if interface.is_action_required(
                precice.action_write_iteration_checkpoint()):
            lhs_checkpoint = lhs0
            lhs00_checkpoint = lhs00
            t_checkpoint = t
            timestep_checkpoint = timestep
            oldmeshdofs_checkpoint = oldmeshdofs
            oldoldmeshdofs_checkpoint = oldoldmeshdofs
            oldoldoldmeshdofs_checkpoint = oldoldoldmeshdofs
            interface.mark_action_fulfilled(
                precice.action_write_iteration_checkpoint())

        # solve fluid equations
        lhs1 = solver.newton(
            'lhs',
            res,
            lhs0=lhs0,
            constrain=cons,
            arguments=dict(
                lhs0=lhs0,
                dt=dt,
                meshdofs=meshdofs,
                oldmeshdofs=oldmeshdofs,
                oldoldmeshdofs=oldoldmeshdofs,
                oldoldoldmeshdofs=oldoldoldmeshdofs)).solve(tol=1e-6)

        # write forces to interface
        if interface.is_write_data_required(dt):
            F = solver.solve_linear('F',
                                    resF,
                                    constrain=consF,
                                    arguments=dict(
                                        lhs00=lhs00,
                                        lhs0=lhs0,
                                        lhs=lhs1,
                                        dt=dt,
                                        meshdofs=meshdofs,
                                        oldmeshdofs=oldmeshdofs,
                                        oldoldmeshdofs=oldoldmeshdofs,
                                        oldoldoldmeshdofs=oldoldoldmeshdofs))
            # writedata = couplingsample.eval(ns.F, F=F) # for stresses
            writedata = couplingsample.eval('F_i d:x' @ ns, F=F, meshdofs=meshdofs) * \
                numpy.concatenate([p.weights for p in couplingsample.points])[:, numpy.newaxis]
            interface.write_block_vector_data(writedataID, dataIndices,
                                              writedata)

        # do the coupling
        precice_dt = interface.advance(dt)
        dt = min(precice_dt, timestepsize)

        # advance variables
        timestep += 1
        t += dt
        lhs00 = lhs0
        lhs0 = lhs1
        oldoldoldmeshdofs = oldoldmeshdofs
        oldoldmeshdofs = oldmeshdofs
        oldmeshdofs = meshdofs

        # read checkpoint if required
        if interface.is_action_required(
                precice.action_read_iteration_checkpoint()):
            lhs0 = lhs_checkpoint
            lhs00 = lhs00_checkpoint
            t = t_checkpoint
            timestep = timestep_checkpoint
            oldmeshdofs = oldmeshdofs_checkpoint
            oldoldmeshdofs = oldoldmeshdofs_checkpoint
            oldoldoldmeshdofs = oldoldoldmeshdofs_checkpoint
            interface.mark_action_fulfilled(
                precice.action_read_iteration_checkpoint())

        if interface.is_time_window_complete():
            x, u, p = bezier.eval(['x_i', 'u_i', 'p'] @ ns,
                                  lhs=lhs1,
                                  meshdofs=meshdofs,
                                  oldmeshdofs=oldmeshdofs,
                                  oldoldmeshdofs=oldoldmeshdofs,
                                  oldoldoldmeshdofs=oldoldoldmeshdofs,
                                  dt=dt)
            with treelog.add(treelog.DataLog()):
                export.vtk('Fluid_' + str(timestep), bezier.tri, x, u=u, p=p)

    interface.finalize()
Esempio n. 28
0
def main():

    print("Running utils")

    # define the Nutils mesh
    grid = [
        np.linspace(a, b,
                    round((b - a) / size) + 1)
        for (a, b, size) in [(0, 1, 0.05), (-.25, 0, 0.05)]
    ]
    domain, geom = nutils.mesh.rectilinear(grid)

    # Nutils namespace
    ns = nutils.function.Namespace()
    ns.x = geom

    ns.basis = domain.basis('std', degree=1)  # linear finite elements
    ns.u = 'basis_n ?lhs_n'  # solution
    ns.dudt = 'basis_n (?lhs_n - ?lhs0_n) / ?dt'  # time derivative
    ns.flux = 'basis_n ?fluxdofs_n'  # heat flux
    ns.k = 100  # thermal diffusivity
    ns.uwall = 310  # wall temperature

    # define the weak form
    res = domain.integral('(basis_n dudt + k basis_n,i u_,i) d:x' @ ns,
                          degree=2)

    # define Dirichlet boundary condition
    sqr = domain.boundary['bottom'].integral('(u - uwall)^2 d:x' @ ns,
                                             degree=2)
    cons = nutils.solver.optimize('lhs', sqr, droptol=1e-15)

    # preCICE setup
    interface = precice.Interface("Solid", "../precice-config.xml", 0, 1)

    # define coupling mesh
    mesh_name = "Solid-Mesh"
    mesh_id = interface.get_mesh_id(mesh_name)
    coupling_boundary = domain.boundary['top']
    coupling_sample = coupling_boundary.sample(
        'gauss', degree=2)  # mesh vertices at Gauss points
    vertices = coupling_sample.eval(ns.x)
    vertex_ids = interface.set_mesh_vertices(mesh_id, vertices)

    # coupling data
    flux_id = interface.get_data_id("Heat-Flux", mesh_id)
    temperature_id = interface.get_data_id("Temperature", mesh_id)

    # helper functions to project heat flux to coupling boundary
    projection_matrix = coupling_boundary.integrate(
        ns.eval_nm('basis_n basis_m d:x'), degree=2)
    projection_cons = np.zeros(res.shape)
    projection_cons[projection_matrix.rowsupp(1e-15)] = np.nan

    def fluxdofs(v):
        return projection_matrix.solve(v, constrain=projection_cons)

    precice_dt = interface.initialize()

    cons0 = cons  # to not lose the Dirichlet BC at the bottom
    lhs0 = np.zeros(res.shape)  # solution from previous timestep
    timestep = 0
    dt = 0.01

    # set u = uwall as initial condition and visualize
    sqr = domain.integral('(u - uwall)^2' @ ns, degree=2)
    lhs0 = nutils.solver.optimize('lhs', sqr)
    bezier = domain.sample('bezier', 2)
    x, u = bezier.eval(['x_i', 'u'] @ ns, lhs=lhs0)
    with treelog.add(treelog.DataLog()):
        nutils.export.vtk('Solid_0', bezier.tri, x, T=u)

    while interface.is_coupling_ongoing():

        # read temperature from interface
        if interface.is_read_data_available():
            temperature_values = interface.read_block_scalar_data(
                temperature_id, vertex_ids)
            temperature_function = coupling_sample.asfunction(
                temperature_values)

            sqr = coupling_sample.integral((ns.u - temperature_function)**2)
            cons = nutils.solver.optimize('lhs',
                                          sqr,
                                          droptol=1e-15,
                                          constrain=cons0)

        # save checkpoint
        if interface.is_action_required(
                precice.action_write_iteration_checkpoint()):
            lhs_checkpoint = lhs0
            timestep_checkpoint = timestep
            interface.mark_action_fulfilled(
                precice.action_write_iteration_checkpoint())

        # potentially adjust non-matching timestep sizes
        dt = min(dt, precice_dt)

        # solve nutils timestep
        lhs = nutils.solver.solve_linear('lhs',
                                         res,
                                         constrain=cons,
                                         arguments=dict(lhs0=lhs0, dt=dt))

        # write heat fluxes to interface
        if interface.is_write_data_required(dt):
            flux_function = res.eval(lhs0=lhs0, lhs=lhs, dt=dt)
            flux_values = coupling_sample.eval(
                '-flux' @ ns, fluxdofs=fluxdofs(flux_function))
            interface.write_block_scalar_data(flux_id, vertex_ids, flux_values)

        # do the coupling
        precice_dt = interface.advance(dt)

        # advance variables
        timestep += 1
        lhs0 = lhs

        # read checkpoint if required
        if interface.is_action_required(
                precice.action_read_iteration_checkpoint()):
            lhs0 = lhs_checkpoint
            timestep = timestep_checkpoint
            interface.mark_action_fulfilled(
                precice.action_read_iteration_checkpoint())
        else:  # go to next timestep
            if timestep % 20 == 0:  # visualize
                bezier = domain.sample('bezier', 2)
                x, u = bezier.eval(['x_i', 'u'] @ ns, lhs=lhs)
                with treelog.add(treelog.DataLog()):
                    nutils.export.vtk('Solid_' + str(timestep),
                                      bezier.tri,
                                      x,
                                      T=u)

    interface.finalize()
Esempio n. 29
0
def main(side='Dirichlet'):

    print("Running nutils")

    # domain size
    y_bottom, y_top = 0, 1
    x_left, x_right = 0, 2
    x_coupling = 1  # x coordinate of coupling interface

    n = 10  # number of mesh vertices per dimension

    if side == 'Dirichlet':
        x_grid = np.linspace(x_left, x_coupling, n)
    elif side == 'Neumann':
        x_grid = np.linspace(x_coupling, x_right, n)
    else:
        raise Exception('invalid side {!r}'.format(side))

    y_grid = np.linspace(y_bottom, y_top, n)

    # define the Nutils mesh
    domain, geom = nutils.mesh.rectilinear([x_grid, y_grid])

    # Nutils namespace
    ns = nutils.function.Namespace()
    ns.x = geom

    degree = 1  # linear finite elements
    ns.basis = domain.basis('std', degree=degree)
    ns.alpha = 3  # parameter of problem
    ns.beta = 1.3  # parameter of problem
    ns.u = 'basis_n ?lhs_n'  # solution
    ns.dudt = 'basis_n (?lhs_n - ?lhs0_n) / ?dt'  # time derivative
    ns.flux = 'basis_n ?fluxdofs_n'  # heat flux
    ns.f = 'beta - 2 - 2 alpha'  # rhs
    ns.uexact = '1 + x_0 x_0 + alpha x_1 x_1 + beta ?t'  # analytical solution

    # define the weak form
    res0 = domain.integral(
        '(basis_n dudt - basis_n f + basis_n,i u_,i) d:x' @ ns,
        degree=degree * 2)

    # set boundary conditions at non-coupling boundaries
    # top and bottom boundary are non-coupling for both sides
    sqr0 = domain.boundary['top'].integral(
        '(u - 1 - x_0 x_0 - alpha - beta ?t)^2 d:x' @ ns, degree=degree * 2)
    sqr0 += domain.boundary['bottom'].integral(
        '(u - 1 - x_0 x_0 - beta ?t)^2 d:x' @ ns, degree=degree * 2)
    if side == 'Dirichlet':  # left boundary is non-coupling
        sqr0 += domain.boundary['left'].integral(
            '(u - 1 - alpha x_1 x_1 - beta ?t)^2 d:x' @ ns, degree=degree * 2)
    elif side == 'Neumann':  # right boundary is non-coupling
        sqr0 += domain.boundary['right'].integral(
            '(u - 1 - x_0 x_0 - alpha x_1 x_1 - beta ?t)^2 d:x' @ ns,
            degree=degree * 2)

    # preCICE setup
    interface = precice.Interface(side, "../precice-config.xml", 0, 1)

    # define coupling mesh
    mesh_name = side + "-Mesh"
    mesh_id = interface.get_mesh_id(mesh_name)
    coupling_boundary = domain.boundary['right' if side ==
                                        'Dirichlet' else 'left']
    coupling_sample = coupling_boundary.sample('gauss', degree=degree * 2)
    vertices = coupling_sample.eval(ns.x)
    vertex_ids = interface.set_mesh_vertices(mesh_id, vertices)

    # coupling data
    write_data = "Temperature" if side == "Neumann" else "Heat-Flux"
    read_data = "Heat-Flux" if side == "Neumann" else "Temperature"
    write_data_id = interface.get_data_id(write_data, mesh_id)
    read_data_id = interface.get_data_id(read_data, mesh_id)

    # helper functions to project heat flux to coupling boundary
    projection_matrix = coupling_boundary.integrate(
        ns.eval_nm('basis_n basis_m d:x'), degree=degree * 2)
    projection_cons = np.zeros(res0.shape)
    projection_cons[projection_matrix.rowsupp(1e-15)] = np.nan

    def fluxdofs(v):
        return projection_matrix.solve(v, constrain=projection_cons)

    # helper data structure to apply heat flux correctly
    dx_function = 'd:x' @ ns

    precice_dt = interface.initialize()

    # write initial data
    if interface.is_action_required(precice.action_write_initial_data()):
        write_data = np.zeros(len(vertex_ids))
        interface.write_block_scalar_data(write_data_id, vertex_ids,
                                          write_data)
        interface.mark_action_fulfilled(precice.action_write_initial_data())

    interface.initialize_data()

    t = 0

    # initial condition
    sqr = domain.integral('(u - uexact)^2' @ ns, degree=degree * 2)
    lhs0 = nutils.solver.optimize('lhs',
                                  sqr,
                                  droptol=1e-15,
                                  arguments=dict(t=t))
    bezier = domain.sample('bezier', degree * 2)
    x, u, uexact = bezier.eval(['x_i', 'u', 'uexact'] @ ns, lhs=lhs0, t=t)
    with treelog.add(treelog.DataLog()):
        nutils.export.vtk(side + '-0',
                          bezier.tri,
                          x,
                          Temperature=u,
                          reference=uexact)

    t += precice_dt
    timestep = 0
    dt = 0.1

    while interface.is_coupling_ongoing():

        # update (time-dependent) boundary condition
        cons0 = nutils.solver.optimize('lhs',
                                       sqr0,
                                       droptol=1e-15,
                                       arguments=dict(t=t))

        # read data from interface
        if interface.is_read_data_available():
            read_data = interface.read_block_scalar_data(
                read_data_id, vertex_ids)
            read_function = coupling_sample.asfunction(read_data)

            if side == 'Dirichlet':
                sqr = coupling_sample.integral((ns.u - read_function)**2)
                cons = nutils.solver.optimize('lhs',
                                              sqr,
                                              droptol=1e-15,
                                              constrain=cons0,
                                              arguments=dict(t=t))
                res = res0
            else:
                cons = cons0
                res = res0 + coupling_sample.integral(
                    ns.basis * read_function * dx_function)

        # save checkpoint
        if interface.is_action_required(
                precice.action_write_iteration_checkpoint()):
            lhs_checkpoint = lhs0
            t_checkpoint = t
            timestep_checkpoint = timestep
            interface.mark_action_fulfilled(
                precice.action_write_iteration_checkpoint())

        # potentially adjust non-matching timestep sizes
        dt = min(dt, precice_dt)

        # solve nutils timestep
        lhs = nutils.solver.solve_linear('lhs',
                                         res,
                                         constrain=cons,
                                         arguments=dict(lhs0=lhs0, dt=dt, t=t))

        # write data to interface
        if interface.is_write_data_required(dt):
            if side == 'Dirichlet':
                flux_function = res.eval(lhs0=lhs0, lhs=lhs, dt=dt, t=t)
                write_data = coupling_sample.eval(
                    'flux' @ ns, fluxdofs=fluxdofs(flux_function))
            else:
                write_data = coupling_sample.eval('u' @ ns, lhs=lhs)

            interface.write_block_scalar_data(write_data_id, vertex_ids,
                                              write_data)

        # do the coupling
        precice_dt = interface.advance(dt)

        # advance variables
        t += dt
        timestep += 1
        lhs0 = lhs

        # read checkpoint if required
        if interface.is_action_required(
                precice.action_read_iteration_checkpoint()):
            lhs0 = lhs_checkpoint
            t = t_checkpoint
            timestep = timestep_checkpoint
            interface.mark_action_fulfilled(
                precice.action_read_iteration_checkpoint())
        else:  # go to next timestep
            bezier = domain.sample('bezier', degree * 2)
            x, u, uexact = bezier.eval(['x_i', 'u', 'uexact'] @ ns,
                                       lhs=lhs,
                                       t=t)

            with treelog.add(treelog.DataLog()):
                nutils.export.vtk(side + "-" + str(timestep),
                                  bezier.tri,
                                  x,
                                  Temperature=u,
                                  reference=uexact)

    interface.finalize()
Esempio n. 30
0
                    type=str,
                    default="precice-config.xml")

try:
    args = parser.parse_args()
except SystemExit:
    print("")
    print(
        "Did you forget adding the precice configuration file as an argument?")
    print("Try '$ python SolidSolver.py precice-config.xml'")
    quit()

print("N: " + str(N))

print("Configure preCICE...")
interface = precice.Interface("Solid", args.configurationFileName, 0, 1)
print("preCICE configured...")

dimensions = interface.get_dimensions()

pressure = p0 * np.ones(N + 1)
crossSectionLength = a0 * np.ones(N + 1)

meshID = interface.get_mesh_id("Solid-Nodes-Mesh")
crossSectionLengthID = interface.get_data_id("CrossSectionLength", meshID)
pressureID = interface.get_data_id("Pressure", meshID)

vertexIDs = np.zeros(N + 1)
grid = np.zeros([N + 1, dimensions])

grid[:, 0] = np.linspace(0, L, N + 1)  # x component