Skip to content

Commit 14bfe4e

Browse files
committed
Merge branch '554-parse-vtk-xml-in-c' into 'development'
Optional C++ XML parser Closes #554 See merge request damask/DAMASK!1155
2 parents eee9924 + 9d31154 commit 14bfe4e

File tree

15 files changed

+1106
-120
lines changed

15 files changed

+1106
-120
lines changed

src/CLI.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,32 @@
1111

1212
#ifdef BOOST
1313

14-
#include "CLI.h"
14+
#include <cstdlib>
15+
#include <cstring>
16+
#include <filesystem>
17+
#include <stdexcept>
18+
#include <utility>
19+
#include <petscversion.h>
1520
#include <ISO_Fortran_binding.h>
1621
#include <pwd.h>
1722
#include <unistd.h>
1823
#include <array>
24+
1925
#include <boost/asio/ip/host_name.hpp>
2026
#include <boost/lexical_cast.hpp>
27+
#include <boost/version.hpp>
28+
#if BOOST_VERSION >= 108800
29+
#include <boost/uuid/basic_random_generator.hpp>
30+
#endif
31+
#include <boost/system/detail/error_code.hpp>
2132
#include <boost/program_options/options_description.hpp>
2233
#include <boost/program_options/parsers.hpp>
2334
#include <boost/program_options/value_semantic.hpp>
2435
#include <boost/program_options/variables_map.hpp>
25-
#include <boost/system/detail/error_code.hpp>
26-
#include <boost/version.hpp>
27-
#if BOOST_VERSION >= 108600
28-
#include <boost/uuid/basic_random_generator.hpp>
29-
#endif
3036
#include <boost/uuid/random_generator.hpp>
3137
#include <boost/uuid/uuid_io.hpp>
32-
#include <cstdlib>
33-
#include <cstring>
34-
#include <filesystem>
35-
#include <stdexcept>
36-
#include <utility>
37-
#include <petscversion.h>
38+
39+
#include "CLI.h"
3840

3941
namespace fs = std::filesystem;
4042

@@ -143,7 +145,7 @@ CLI::CLI(std::span<const char*> args, int* worldrank) {
143145

144146
boost::system::error_code ec;
145147
std::string hostname = boost::asio::ip::host_name(ec);
146-
if (ec) std::runtime_error("Boost hostname collection error: " + ec.message());
148+
if (ec) std::runtime_error("boost hostname collection error: " + ec.message());
147149

148150
cout << " Host name: " << hostname << std::endl;
149151
cout << " User name: " << get_username() << std::endl << std::endl;

src/C_interfacing.f90

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
! SPDX-License-Identifier: AGPL-3.0-or-later
2+
!--------------------------------------------------------------------------------------------------
3+
!> @author Daniel Otto de Mentock, Max‑Planck‑Institut für Nachhaltige Materialien GmbH
4+
!> @author Martin Diehl, KU Leuven
5+
!> @brief Helpers to interface between C and Fortran strings
6+
!--------------------------------------------------------------------------------------------------
7+
module C_interfacing
8+
use, intrinsic :: ISO_C_binding
9+
10+
use prec
11+
12+
implicit none(type,external)
13+
private
14+
15+
public :: &
16+
c_f_string
17+
#if (defined(__INTEL_COMPILER) && __INTEL_COMPILER_BUILD_DATE < 20240000) \
18+
|| (defined(__GFORTRAN__) && __GNUC__ < 15) \
19+
|| defined(__flang__)
20+
public :: &
21+
f_c_string
22+
#endif
23+
24+
interface c_f_string
25+
module procedure c_f_string_scalar
26+
module procedure c_f_string_array
27+
end interface
28+
29+
contains
30+
31+
!--------------------------------------------------------------------------------------------------
32+
!> @brief Convert C string to Fortran string.
33+
!> @details: C string is a fixed-size fortran array referencing a NULL terminated c string.
34+
! Due to the NULL-termination, C string has one more element than the Fortran string.
35+
!--------------------------------------------------------------------------------------------------
36+
pure function c_f_string_scalar(c_string) result(f_string)
37+
38+
character(kind=C_CHAR,len=*), intent(in) :: c_string
39+
character(len=:), allocatable :: f_string
40+
41+
integer(pI64) :: i
42+
43+
44+
allocate(character(len=len(c_string,kind=pI64))::f_string)
45+
do i=1_pI64,len(f_string,pI64)
46+
if (c_string(i:i) /= C_NULL_CHAR) then
47+
f_string(i:i)=c_string(i:i)
48+
else
49+
f_string = f_string(:i-1_pI64)
50+
exit
51+
end if
52+
end do
53+
54+
end function c_f_string_scalar
55+
56+
!--------------------------------------------------------------------------------------------------
57+
!> @brief Convert C string pointer to Fortran string.
58+
!> @details: C string is an assumed-size fortran array referencing a NULL terminated c string.
59+
!--------------------------------------------------------------------------------------------------
60+
pure function c_f_string_array(c_string) result(f_string)
61+
62+
character(kind=C_CHAR), intent(in), dimension(*) :: c_string
63+
character(len=:), allocatable :: f_string
64+
65+
integer :: n, i
66+
67+
68+
n = 0
69+
do
70+
if (c_string(n+1) == C_NULL_CHAR) exit
71+
n = n + 1
72+
end do
73+
74+
allocate(character(len=n) :: f_string)
75+
do i = 1, n
76+
f_string(i:i) = c_string(i)
77+
end do
78+
79+
end function c_f_string_array
80+
81+
82+
#if (defined(__INTEL_COMPILER) && __INTEL_COMPILER_BUILD_DATE < 20240000) \
83+
|| (defined(__GFORTRAN__) && __GNUC__ < 15) \
84+
|| defined(__flang__)
85+
!--------------------------------------------------------------------------------------------------
86+
!> @brief Fortran 2023 "f_c_string" (without optional argument).
87+
!> @details: C string is NULL terminated and, hence, longer by one than the Fortran string.
88+
!--------------------------------------------------------------------------------------------------
89+
pure function f_c_string(f_string) result(c_string)
90+
91+
character(len=*), intent(in) :: f_string
92+
character(kind=C_CHAR,len=len_trim(f_string,pI64)+1_pI64) :: c_string
93+
94+
95+
c_string = trim(f_string)//C_NULL_CHAR
96+
97+
end function f_c_string
98+
#endif
99+
100+
101+
end module C_interfacing

src/IO.f90

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ module IO
1616
use prec
1717
use constants
1818
use misc
19-
#ifndef MARC_SOURCE
20-
use OS
21-
#endif
19+
use C_interfacing
2220

2321
implicit none(type,external)
2422
private
@@ -601,6 +599,20 @@ subroutine IO_warning(warning_ID, &
601599
end subroutine IO_warning
602600

603601

602+
!--------------------------------------------------------------------------------------------------
603+
!> @brief C interface to IO_error_new taking a null-terminated C string.
604+
!--------------------------------------------------------------------------------------------------
605+
subroutine F_IO_error(error_ID, msg) bind(C, name='F_IO_error')
606+
607+
integer(C_INT), intent(in), value :: error_ID
608+
character(len=*,kind=C_CHAR), intent(in) :: msg
609+
610+
611+
call IO_error_new(int(error_ID,pI16), trim(c_f_string(msg)))
612+
613+
end subroutine F_IO_error
614+
615+
604616
!--------------------------------------------------------------------------------------------------
605617
!> @brief Test whether a file descriptor refers to a terminal.
606618
!> @detail A terminal is neither a file nor a redirected STDOUT/STDERR/STDIN.

src/IO.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <string_view>
5+
6+
extern "C" void F_IO_error(int error_ID, const char* msg);
7+
8+
class IO {
9+
public:
10+
void (*fn)(int error_ID, const char* msg) = &F_IO_error;
11+
void error(int error_ID, std::string_view msg) const {
12+
const std::string tmp(msg);
13+
fn(error_ID, tmp.c_str());
14+
}
15+
};

src/Marc/DAMASK_Marc.f90

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@
2222
#endif
2323

2424
#include "../prec.f90"
25+
#include "../C_interfacing.f90"
2526
#include "../constants.f90"
2627
#include "../parallelization.f90"
2728
#include "../misc.f90"
2829
#include "../IO.f90"
29-
#ifdef FYAML
30-
#include "../OS.f90"
31-
#endif
3230
#include "../types.f90"
3331
#include "../YAML.f90"
3432
#include "../HDF5_utilities.f90"

src/OS.f90

Lines changed: 3 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module OS
88
use, intrinsic :: ISO_fortran_env
99

1010
use prec
11+
use C_interfacing
1112

1213
implicit none(type,external)
1314
private
@@ -18,16 +19,10 @@ module OS
1819
OS_setCWD, &
1920
OS_getCWD, &
2021
OS_getHostName, &
21-
OS_getUserName, &
22+
OS_getUserName
2223
#ifdef OLD_STYLE_C_TO_FORTRAN_STRING
23-
free_C, &
24+
public :: free_C
2425
#endif
25-
#if (defined(__INTEL_COMPILER) && __INTEL_COMPILER_BUILD_DATE < 20240000) \
26-
|| (defined(__GFORTRAN__) && __GNUC__ < 15) \
27-
|| defined(__flang__)
28-
f_c_string, &
29-
#endif
30-
c_f_string
3126

3227
interface
3328

@@ -100,11 +95,6 @@ end subroutine free_C
10095

10196
end interface
10297

103-
interface c_f_string
104-
module procedure c_f_string_scalar
105-
module procedure c_f_string_array
106-
end interface
107-
10898
contains
10999

110100

@@ -214,76 +204,6 @@ function OS_getUserName()
214204
end function OS_getUserName
215205

216206

217-
!--------------------------------------------------------------------------------------------------
218-
!> @brief Convert C string to Fortran string.
219-
!> @details: C string is a fixed-size fortran array referencing a NULL terminated c string.
220-
! Due to the NULL-termination, C string has one more element than the Fortran string.
221-
!--------------------------------------------------------------------------------------------------
222-
pure function c_f_string_scalar(c_string) result(f_string)
223-
224-
character(kind=C_CHAR,len=*), intent(in) :: c_string
225-
character(len=:), allocatable :: f_string
226-
227-
integer(pI64) :: i
228-
229-
230-
allocate(character(len=len(c_string,kind=pI64))::f_string)
231-
do i=1_pI64,len(f_string,pI64)
232-
if (c_string(i:i) /= C_NULL_CHAR) then
233-
f_string(i:i)=c_string(i:i)
234-
else
235-
f_string = f_string(:i-1_pI64)
236-
exit
237-
end if
238-
end do
239-
240-
end function c_f_string_scalar
241-
242-
!--------------------------------------------------------------------------------------------------
243-
!> @brief Convert C string pointer to Fortran string.
244-
!> @details: C string is an assumed-size fortran array referencing a NULL terminated c string.
245-
!--------------------------------------------------------------------------------------------------
246-
pure function c_f_string_array(c_string) result(f_string)
247-
248-
character(kind=C_CHAR), intent(in), dimension(*) :: c_string
249-
character(len=:), allocatable :: f_string
250-
251-
integer :: n, i
252-
253-
254-
n = 0
255-
do
256-
if (c_string(n+1) == C_NULL_CHAR) exit
257-
n = n + 1
258-
end do
259-
260-
allocate(character(len=n) :: f_string)
261-
do i = 1, n
262-
f_string(i:i) = c_string(i)
263-
end do
264-
265-
end function c_f_string_array
266-
267-
268-
#if (defined(__INTEL_COMPILER) && __INTEL_COMPILER_BUILD_DATE < 20240000) \
269-
|| (defined(__GFORTRAN__) && __GNUC__ < 15) \
270-
|| defined(__flang__)
271-
!--------------------------------------------------------------------------------------------------
272-
!> @brief Fortran 2023 "f_c_string" (without optional argument).
273-
!> @details: C string is NULL terminated and, hence, longer by one than the Fortran string.
274-
!--------------------------------------------------------------------------------------------------
275-
pure function f_c_string(f_string) result(c_string)
276-
277-
character(len=*), intent(in) :: f_string
278-
character(kind=C_CHAR,len=len_trim(f_string,pI64)+1_pI64) :: c_string
279-
280-
281-
c_string = trim(f_string)//C_NULL_CHAR
282-
283-
end function f_c_string
284-
#endif
285-
286-
287207
!--------------------------------------------------------------------------------------------------
288208
!> @brief Check correctness of some OS functions.
289209
!--------------------------------------------------------------------------------------------------
@@ -310,4 +230,3 @@ end subroutine OS_selfTest
310230

311231

312232
end module OS
313-

src/YAML.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module YAML
1111
use IO
1212
use types
1313
#ifdef FYAML
14-
use OS
14+
use C_interfacing
1515
#endif
1616

1717
implicit none(type,external)

src/grid/DAMASK_grid.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ program DAMASK_grid
2222
use IO
2323
use config
2424
use math
25+
use discretization_grid
2526
use materialpoint
2627
use material
2728
use spectral_utilities
@@ -244,6 +245,7 @@ program DAMASK_grid
244245
end do
245246

246247
call grid_mechanical_init(num_grid%get_dict('mechanical',defaultVal=emptyDict))
248+
call discretization_grid_finalize()
247249
call config_numerics_deallocate()
248250

249251
!--------------------------------------------------------------------------------------------------
@@ -428,7 +430,6 @@ program DAMASK_grid
428430

429431
end do loadCaseLooping
430432

431-
432433
!--------------------------------------------------------------------------------------------------
433434
! report summary of whole calculation
434435
print'(/,1x,a)', '###########################################################################'

0 commit comments

Comments
 (0)