Skip to content

Commit

Permalink
More simplifications and quality of life updates (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgensd authored Aug 9, 2024
1 parent bbe3ec9 commit d28f4e6
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 90 deletions.
101 changes: 51 additions & 50 deletions cpp/ContactConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ dolfinx_mpc::mpc_data<T> compute_block_contributions(
{
const std::int32_t local_slave = local_slaves[i];
std::iota(dofs.begin(), dofs.end(), local_slave_blocks[i] * block_size);
auto local_max = std::find(dofs.begin(), dofs.end(), local_slave);
auto local_max = std::ranges::find(dofs, local_slave);
const auto max_index = std::distance(dofs.begin(), local_max);
for (std::int32_t j = 0; j < block_size; j++)
{
Expand Down Expand Up @@ -723,7 +723,7 @@ mpc_data<T> create_contact_slip_condition(
}
}
}
if (auto not_found = std::find(slave_found.begin(), slave_found.end(), false);
if (auto not_found = std::ranges::find(slave_found, false);
not_found != slave_found.end())
{
std::runtime_error(
Expand Down Expand Up @@ -768,17 +768,20 @@ mpc_data<T> create_contact_slip_condition(
if (!(slave_found[c]) && (slave_max - slave_min > 0))
{
slave_found[c] = true;
std::copy(remote_colliding_masters.begin() + proc_start + slave_min,
remote_colliding_masters.begin() + proc_start + slave_max,
offproc_masters.begin() + offproc_offsets[c]);

std::copy(remote_colliding_coeffs.begin() + proc_start + slave_min,
remote_colliding_coeffs.begin() + proc_start + slave_max,
offproc_coeffs.begin() + offproc_offsets[c]);

std::copy(remote_colliding_owners.begin() + proc_start + slave_min,
remote_colliding_owners.begin() + proc_start + slave_max,
offproc_owners.begin() + offproc_offsets[c]);
std::ranges::copy(
remote_colliding_masters.begin() + proc_start + slave_min,
remote_colliding_masters.begin() + proc_start + slave_max,
offproc_masters.begin() + offproc_offsets[c]);

std::ranges::copy(
remote_colliding_coeffs.begin() + proc_start + slave_min,
remote_colliding_coeffs.begin() + proc_start + slave_max,
offproc_coeffs.begin() + offproc_offsets[c]);

std::ranges::copy(
remote_colliding_owners.begin() + proc_start + slave_min,
remote_colliding_owners.begin() + proc_start + slave_max,
offproc_owners.begin() + offproc_offsets[c]);
}
}
}
Expand Down Expand Up @@ -814,15 +817,15 @@ mpc_data<T> create_contact_slip_condition(
{
const std::int32_t master_min = masters_offsets[i];
const std::int32_t master_max = masters_offsets[i + 1];
std::copy(masters_out.begin() + master_min,
masters_out.begin() + master_max,
local_masters.begin() + local_offsets[i] + loc_pos[i]);
std::copy(coefficients_out.begin() + master_min,
coefficients_out.begin() + master_max,
local_coeffs.begin() + local_offsets[i] + loc_pos[i]);
std::copy(owners_out.begin() + master_min,
owners_out.begin() + master_max,
local_owners.begin() + local_offsets[i] + loc_pos[i]);
std::ranges::copy(masters_out.begin() + master_min,
masters_out.begin() + master_max,
local_masters.begin() + local_offsets[i] + loc_pos[i]);
std::ranges::copy(coefficients_out.begin() + master_min,
coefficients_out.begin() + master_max,
local_coeffs.begin() + local_offsets[i] + loc_pos[i]);
std::ranges::copy(owners_out.begin() + master_min,
owners_out.begin() + master_max,
local_owners.begin() + local_offsets[i] + loc_pos[i]);
loc_pos[i] += master_max - master_min;
}

Expand All @@ -832,18 +835,18 @@ mpc_data<T> create_contact_slip_condition(
const std::int32_t master_min = offproc_offsets[i];
const std::int32_t master_max = offproc_offsets[i + 1];
const std::int32_t slave_index = slave_indices_remote[i];
std::copy(offproc_masters.begin() + master_min,
offproc_masters.begin() + master_max,
local_masters.begin() + local_offsets[slave_index]
+ loc_pos[slave_index]);
std::copy(offproc_coeffs.begin() + master_min,
offproc_coeffs.begin() + master_max,
local_coeffs.begin() + local_offsets[slave_index]
+ loc_pos[slave_index]);
std::copy(offproc_owners.begin() + master_min,
offproc_owners.begin() + master_max,
local_owners.begin() + local_offsets[slave_index]
+ loc_pos[slave_index]);
std::ranges::copy(offproc_masters.begin() + master_min,
offproc_masters.begin() + master_max,
local_masters.begin() + local_offsets[slave_index]
+ loc_pos[slave_index]);
std::ranges::copy(offproc_coeffs.begin() + master_min,
offproc_coeffs.begin() + master_max,
local_coeffs.begin() + local_offsets[slave_index]
+ loc_pos[slave_index]);
std::ranges::copy(offproc_owners.begin() + master_min,
offproc_owners.begin() + master_max,
local_owners.begin() + local_offsets[slave_index]
+ loc_pos[slave_index]);
loc_pos[slave_index] += master_max - master_min;
}
}
Expand Down Expand Up @@ -1479,18 +1482,18 @@ mpc_data<T> create_contact_inelastic_condition(

// Count number of incoming slaves
std::vector<std::int32_t> inc_num_slaves(src_ranks_ghost.size(), 0);
std::ranges::for_each(
ghost_slaves,
[block_size, size_local, &ghost_owners, &inc_num_slaves,
&src_ranks_ghost](std::int32_t slave)
{
const std::int32_t owner
= ghost_owners[slave / block_size - size_local];
const auto it
= std::find(src_ranks_ghost.begin(), src_ranks_ghost.end(), owner);
const auto index = std::distance(src_ranks_ghost.begin(), it);
inc_num_slaves[index]++;
});
std::ranges::for_each(ghost_slaves,
[block_size, size_local, &ghost_owners, &inc_num_slaves,
&src_ranks_ghost](std::int32_t slave)
{
const std::int32_t owner
= ghost_owners[slave / block_size - size_local];
const auto it
= std::ranges::find(src_ranks_ghost, owner);
const auto index
= std::distance(src_ranks_ghost.begin(), it);
inc_num_slaves[index]++;
});
// Count number of outgoing slaves and masters
dolfinx::graph::AdjacencyList<int> shared_indices
= slave_index_map->index_to_dest_ranks();
Expand Down Expand Up @@ -1524,8 +1527,7 @@ mpc_data<T> create_contact_inelastic_condition(
const auto num_masters = (std::int32_t)masters_i.size();
for (auto proc : shared_indices.links(slave / block_size))
{
const auto it = std::find(dest_ranks_ghost.begin(),
dest_ranks_ghost.end(), proc);
const auto it = std::ranges::find(dest_ranks_ghost, proc);
std::int32_t index = std::distance(dest_ranks_ghost.begin(), it);
out_num_masters[index] += num_masters;
out_num_slaves[index]++;
Expand Down Expand Up @@ -1708,8 +1710,7 @@ mpc_data<T> create_contact_inelastic_condition(
std::vector<std::int32_t> local_ghosts
= map_dofs_global_to_local<U>(V, in_ghost_slaves);
slaves.resize(num_loc_slaves + num_ghost_slaves);
std::copy(local_ghosts.cbegin(), local_ghosts.cend(),
slaves.begin() + num_loc_slaves);
std::ranges::copy(local_ghosts, slaves.begin() + num_loc_slaves);
mpc.slaves = slaves;
mpc.masters = masters;
mpc.offsets = offsets;
Expand Down
2 changes: 1 addition & 1 deletion cpp/PeriodicConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ dolfinx_mpc::mpc_data<T> _create_periodic_condition(
if (rank == proc)
continue;
// Find position in neighborhood communicator
auto it = std::find(s_to_m_ranks.begin(), s_to_m_ranks.end(), proc);
auto it = std::ranges::find(s_to_m_ranks, proc);
assert(it != s_to_m_ranks.end());
auto dist = std::distance(s_to_m_ranks.begin(), it);
const std::int32_t insert_location
Expand Down
2 changes: 1 addition & 1 deletion cpp/assemble_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ std::vector<std::int32_t> dolfinx_mpc::compute_local_slave_index(
assert((std::uint32_t)dof < is_slave.size());
if (is_slave[dof])
{
auto it = std::find(slaves.begin(), slaves.end(), dof);
auto it = std::ranges::find(slaves, dof);
const auto slave_index = std::distance(slaves.begin(), it);
local_index[slave_index] = i * bs + j;
}
Expand Down
9 changes: 3 additions & 6 deletions cpp/mpc_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ create_extended_functionspace(const dolfinx::fem::FunctionSpace<U>& V,
// when we have multiple masters from the same block

if ((local_blocks[i] == -1)
and (std::find(additional_ghosts.begin(), additional_ghosts.end(),
global_blocks[i])
and (std::ranges::find(additional_ghosts, global_blocks[i])
== additional_ghosts.end()))
{
additional_ghosts.push_back(global_blocks[i]);
Expand All @@ -202,13 +201,11 @@ create_extended_functionspace(const dolfinx::fem::FunctionSpace<U>& V,

std::vector<std::int64_t> all_ghosts(num_ghosts + additional_ghosts.size());
std::ranges::copy(ghosts, all_ghosts.begin());
std::copy(additional_ghosts.cbegin(), additional_ghosts.cend(),
all_ghosts.begin() + num_ghosts);
std::ranges::copy(additional_ghosts, all_ghosts.begin() + num_ghosts);

std::vector<int> all_owners(all_ghosts.size());
std::ranges::copy(ghost_owners, all_owners.begin());
std::copy(additional_owners.cbegin(), additional_owners.cend(),
all_owners.begin() + num_ghosts);
std::ranges::copy(additional_owners, all_owners.begin() + num_ghosts);

// Create new indexmap with ghosts for master blocks added
new_index_map = std::make_shared<dolfinx::common::IndexMap>(
Expand Down
3 changes: 1 addition & 2 deletions cpp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ std::array<MPI_Comm, 2> dolfinx_mpc::create_neighborhood_comms(
std::vector<std::uint8_t> has_slaves(mpi_size, slave_val);
// Check if entities if master entities are on this processor
std::vector<std::uint8_t> has_masters(mpi_size, 0);
if (std::find(meshtags.values().begin(), meshtags.values().end(),
master_marker)
if (std::ranges::find(meshtags.values(), master_marker)
!= meshtags.values().end())
std::ranges::fill(has_masters, 1);

Expand Down
61 changes: 31 additions & 30 deletions cpp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ create_block_to_facet_map(dolfinx::mesh::Topology& topology,

// Get local index of facet with respect to the cell
auto cell_entities = c_to_e->links(cell[0]);
const auto* it
= std::find(cell_entities.data(),
cell_entities.data() + cell_entities.size(), entities[i]);
assert(it != (cell_entities.data() + cell_entities.size()));
const int local_entity = std::distance(cell_entities.data(), it);
const auto it
= std::find(cell_entities.begin(), cell_entities.end(), entities[i]);
assert(it != cell_entities.end());
const auto local_entity = std::distance(cell_entities.begin(), it);
local_indices[i] = local_entity;
auto cell_blocks = dofmap.cell_dofs(cell[0]);
auto closure_blocks
= dofmap.element_dof_layout().entity_closure_dofs(dim, local_entity);
for (std::size_t j = 0; j < closure_blocks.size(); ++j)
{
const int dof = cell_blocks[closure_blocks[j]];
num_facets_per_dof[dof]++;
}
std::ranges::for_each(closure_blocks,
[&num_facets_per_dof, &cell_blocks](auto block)
{
const int dof = cell_blocks[block];
num_facets_per_dof[dof]++;
});
}

// Compute offsets
Expand All @@ -90,11 +90,13 @@ create_block_to_facet_map(dolfinx::mesh::Topology& topology,
auto cell_blocks = dofmap.cell_dofs(cells[i]);
auto closure_blocks = dofmap.element_dof_layout().entity_closure_dofs(
dim, local_indices[i]);
for (std::size_t j = 0; j < closure_blocks.size(); ++j)
{
const int dof = cell_blocks[closure_blocks[j]];
data[offsets[dof] + num_facets_per_dof[dof]++] = entities[i];
}
std::for_each(closure_blocks.begin(), closure_blocks.end(),
[&num_facets_per_dof, &data, &cell_blocks, &offsets,
entity = entities[i]](auto block)
{
const int dof = cell_blocks[block];
data[offsets[dof] + num_facets_per_dof[dof]++] = entity;
});
}
return dolfinx::graph::AdjacencyList<std::int32_t>(data, offsets);
}
Expand Down Expand Up @@ -749,7 +751,7 @@ dolfinx_mpc::mpc_data<T> distribute_ghost_data(
std::div_t div = std::div(slaves[i], bs);
slave_blocks[i] = div.quot;
slave_rems[i] = div.rem;
auto it = std::find(blocks.begin(), blocks.end(), div.quot);
auto it = std::ranges::find(blocks, div.quot);
assert(it != blocks.end());
auto index = std::distance(blocks.begin(), it);
parent_to_sub.push_back((int)index);
Expand All @@ -773,8 +775,7 @@ dolfinx_mpc::mpc_data<T> distribute_ghost_data(
for (auto proc : shared_indices.links(parent_to_sub[i]))
{
// Find index of process in local MPI communicator
auto it
= std::find(dest_ranks_ghosts.begin(), dest_ranks_ghosts.end(), proc);
auto it = std::ranges::find(dest_ranks_ghosts, proc);
const auto index = std::distance(dest_ranks_ghosts.begin(), it);
out_num_masters[index] += num_masters_per_slave[i];
out_num_slaves[index]++;
Expand Down Expand Up @@ -828,8 +829,7 @@ dolfinx_mpc::mpc_data<T> distribute_ghost_data(
for (auto proc : shared_indices.links(parent_to_sub[i]))
{
// Find index of process in local MPI communicator
auto it
= std::find(dest_ranks_ghosts.begin(), dest_ranks_ghosts.end(), proc);
auto it = std::ranges::find(dest_ranks_ghosts, proc);
const auto index = std::distance(dest_ranks_ghosts.begin(), it);

// Insert slave and num masters per slave
Expand All @@ -839,17 +839,18 @@ dolfinx_mpc::mpc_data<T> distribute_ghost_data(
insert_slaves[index]++;

// Insert global master dofs to send
std::copy(masters.begin() + master_start, masters.begin() + master_end,
masters_out.begin() + disp_out_masters[index]
+ insert_masters[index]);
std::ranges::copy(masters.begin() + master_start,
masters.begin() + master_end,
masters_out.begin() + disp_out_masters[index]
+ insert_masters[index]);
// Insert owners to send
std::copy(owners.begin() + master_start, owners.begin() + master_end,
owners_out.begin() + disp_out_masters[index]
+ insert_masters[index]);
std::ranges::copy(
owners.begin() + master_start, owners.begin() + master_end,
owners_out.begin() + disp_out_masters[index] + insert_masters[index]);
// Insert coeffs to send
std::copy(coeffs.begin() + master_start, coeffs.begin() + master_end,
coeffs_out.begin() + disp_out_masters[index]
+ insert_masters[index]);
std::ranges::copy(
coeffs.begin() + master_start, coeffs.begin() + master_end,
coeffs_out.begin() + disp_out_masters[index] + insert_masters[index]);
insert_masters[index] += num_masters_per_slave[i];
}
}
Expand Down Expand Up @@ -1348,7 +1349,7 @@ std::pair<std::vector<U>, std::array<std::size_t, 2>> tabulate_dof_coordinates(

// Get cell dofmap
auto cell_dofs = dofmap->cell_dofs(cells[c]);
auto it = std::find(cell_dofs.begin(), cell_dofs.end(), dofs[c]);
auto it = std::ranges::find(cell_dofs, dofs[c]);
auto loc = std::distance(cell_dofs.begin(), it);

// Copy dof coordinates into vector
Expand Down

0 comments on commit d28f4e6

Please sign in to comment.