scrimp.examples

We provide some examples coming from our publications.

The equations are explained here.

Wave

Conservative 2D anisotropic membrane with mixed boundary ports.

2D mixed-boundary anisotropic membrane, energy variables q=grad w, p=rho*w_t, coenergy e_q=T q and e_p=p/rho. This preserves the original model’s four boundary ports and its actual GetFEM GWFL Hamiltonian 0.5*q.T.q, where T is the full tensor parameter.

Physical outward-normal traction: u_N=n.e_q at bottom/right/top; observation y_N=e_p. Left side prescribes u_D=e_p (velocity) and observes y_D=n.e_q (traction). The pH energy balance is

H(t)-H(0) = integral (u_N*y_N + u_D*y_D) dt.

The negative flow-side algebraic rows impose Y_N=e_p and U_D=e_p: this is the same physical sign convention as qualified wave_1d.py. The physical sign of the measured work is audited independently against the original GetFEM Hamiltonian; no success is inferred solely from these formulae.

This tutorial starts from EXACTLY zero, compatible with zero initial Dirichlet velocity and initial normal stress, and uses a left-boundary velocity that is zero at t=0 but nontrivial soon after. The original model, before promotion, passed real MUMPS MPI1/2 10-step TSCN physical and shared-ParaView tests at cf42089d; other LU backends require independent native acceptance.

Scientific context: Haine, Matignon, Serhani (2023), anisotropic heterogeneous wave PFEM, DOI 10.4208/ijnam2023-1005; and Brugnoli, Haine, Matignon (2022) on mixed boundary control.

examples.wave.main()
examples.wave.measure_wave_2d(system)

Measure ORIGINAL GetFEM energy and all four signed port powers.

examples.wave.wave_eq(*, mesh_size=0.2, final_time=0.08, time_step=0.008, factor_solver=None)

Build and integrate the conservative mixed-boundary 2D pH wave.

The time/mesh defaults are intentionally for native installation acceptance, not for a convergence order or published benchmark.

  • file: examples/wave_coenergy.py

  • authors: Giuseppe Ferraro, Ghislain Haine

  • date: 22 nov. 2022

  • brief: wave equations in co-energy formulation, two sub-domains

examples.wave_coenergy.main()
examples.wave_coenergy.measure_coenergy(system)

Audit actual CN midpoint FE work, damping and boundary feedback.

Here the signed algebraic boundary power U*Y is a LOSS: because the coenergy Grad-Grad coupling imposes Y=-p (weakly), the original FE balance is dH/dt=-integral(U*Y)-D_r. The passive law is U=+kY. This is not the mixed-boundary 2A/2B sign convention.

The substituted resistive port has no independent flow unknown and Port.get_power() correctly cannot report its dissipative power. We assemble its true mixed FE dissipation in two mathematically equivalent ways: D_r=integral e_r**2/nu = -integral p*e_r. The pointwise nu*p**2 is ONLY a diagnostic because e_r is DG1 and nu*p is not generally in the same FE space.

examples.wave_coenergy.wave_coenergy_eq(*, mesh_size=0.2, final_time=0.08, time_step=0.008, factor_solver='mumps')

Solve the anisotropic 2D wave in genuine co-energy variables.

q=e_q (stress), p=e_p (velocity); Tinv=T**(-1), rho as in 2A/2B. Damping acts only on the inner disk, feedback only on the outer rim. The short fixed CN scenario is for native acceptance, NOT convergence.

Heat

  • file: examples/heat.py

  • authors: Giuseppe Ferraro, Ghislain Haine

  • date: 22 nov. 2022

  • brief: 2D heat equation with Lyapunov Hamiltonian

examples.heat.heat_eq(final_time=5.0, time_step=0.01, mesh_size=0.1, *, time_stepper='cn', initializer=None, factor_solver=None, showcase=False, showcase_output=None, check=False, init_profile=None)

2-D mixed-boundary heat pH-DAE, with the original weak formulation.

The v1.3 demonstration selects fixed-step Crank–Nicolson with strict consistency projection of algebraic unknowns (no pseudo-time alteration of the prescribed differential initial state). The stricter per-example initialization was necessary for full MPI agreement of every coefficient; it does not change SCRIMP’s global initializer defaults.

To reproduce the separately qualified historical BDF2 case, call heat_eq(time_stepper=”bdf”, initializer=”pseudo”, showcase=False). These options preserve its original bricks, initial data and three Dirichlet + one Neumann/Robin feedback control.

When showcase=True, the example ITSELF exports the actual GetFEM temperature time series and renders scientific PNG/SVG/GIF figures. check=True enforces the genuine CN stepwise power balance.

examples.heat.main()
examples.heat.measure_heat_cn(system)

Independently check each native CN step via actual FE/port power.

Control-port power is U*Y on the true mixed FE ports. Replacing this with pointwise T*(J_Q.Normal) is physically WRONG for this DG/Dirichlet-multiplier discretization.

examples.heat.render_heat_visuals(folder, observed, pvd_file, max_gif_frames=36)

Render genuine GetFEM DG1 temperature snapshots and budgets.

All frames originate from actual saved FE snapshots and native GetFEM VTU files. The colormap range is identical across frames, and no fictitious spatial or temporal interpolation is used.

Heat-Wave coupling

  • file: sandbox/heat_hw.py

  • authors: Giuseppe Ferraro, Ghislain Haine

  • date: 15 dec. 2022

  • brief: a 2D coupled heat-wave system

examples.heat_wave.heat_wave_eq(heat_region=1, wave_region=2, *, factor_solver=None, final_time=2.0, time_step=0.001, save_step=0.05, initial_wave_boundary='clamp', initial_wave_profile='gaussian', long_showcase=False, physics_only=False, output_dir=None)

A physically oriented, energy-dissipative coupled heat-wave example.

Reference: Haine, Matignon & Monteghetti, Mathematical Reports (2022), eqs. (2.1)-(2.2), (3.15), (4.3)-(4.5). This example is NOT the paper’s numerical experiment: it keeps the original SCRIMP heat div-div and wave grad-grad weak forms, choosing compatible mixed FEM spaces.

The original built-in Concentric geometry has disk=1, annulus=2, interface=10, outer boundary=20. CN and the 0-input external Dirichlet condition are used to expose the heat dissipation and closed interconnection in an interpretable power balance.

Interface normals always mean each subdomain’s outward normal. The conservative two-sided conforming interface compiler is enabled by default in the v1.3.0 release-preparation source. Physical labels, FEM declarations and weak forms are unchanged. Ambiguous or unsupported interfaces fail explicitly; use SCRIMP_AUTO_INTERFACE=0 only to recover legacy manual assembly if your model was already oriented by hand.

Parameters:
  • heat_region – Domain label of the heat equation (1 or 2).

  • wave_region – Domain label of the wave equation (the other one).

  • factor_solver – Optional PETSc LU solver (default: SCRIMP MUMPS).

  • final_time – Physical end time.

  • time_step – Fixed Crank–Nicolson time step.

  • save_step – Accepted trajectory sampling interval.

  • initial_wave_profile – “gaussian” retains the historical heat/wave condition. “gallery_packet” is a smooth high-angular-wavenumber wave excitation concentrated near the outer radius, with zero initial heat and stress. It is not a certified cavity eigenmode and is available only for a wave annulus.

  • long_showcase – Produce an opt-in native GetFEM annular-wave PNG/GIF showcase after successful physical integration and VTK export.

  • physics_only – Run the same full native PDE and initialize the original Hamiltonian terms but skip plots and ParaView files. This allows genuine parallel physics audits on all MPI ranks without concurrent writers; defaults to False and changes no equations, FEM or physical initialization.

  • initial_wave_boundary – Compatible wave velocity initialization when the exterior boundary belongs to the wave region. “clamp” (default) sets exactly the exterior CG1 degrees of freedom to zero, leaving interior L2-projected coefficients unchanged. “constrained_l2” computes the closest zero-trace field in the physical GetFEM L2 norm (MPI1 only). “unconstrained” reproduces the historical, generally DAE-incompatible Gaussian for diagnostics. Neither valid method relaxes algebraic DAE constraints.

  • output_dir – Optional explicit user output directory.

examples.heat_wave.render_heat_wave_energy_ports(system, *, heat_region, wave_region)

Render true GetFEM heat-wave energy transfer from the canonical run.

The two oriented interface sides and the heat-flux dissipation are sampled from the actual solved GetFEM state/mesh. Step-associated CN power and its discrete identity are claimed ONLY if every real accepted PETSc step has been saved. Otherwise powers are honest instantaneous nodal samples; their integrated work is approximate. The renderer is user-facing and must be called collectively in MPI.

examples.heat_wave.render_heat_wave_long_showcase(times, H, *, profile, wave_region, output_root=None)

Create genuine log-log and annular-wave visuals from THIS run.

Only wave_region=2 can provide the annular gallery. VTK nodal velocity plots are qualitative: they are not a proof of a whispering-gallery eigenmode nor a numerical energy conservation gate. No time interpolation, manufactured data or new physics.

Shallow water

  • file: examples/shallow_water.py

  • authors: Ghislain Haine

  • date: 22 nov. 2022

  • brief: inviscid shallow water equations

examples.shallow_water.native_triangular_field(filename, field_name)

Return original GetFEM nodes and plotting triangles, native h/p values.

examples.shallow_water.render_native_shallow_water(system, folder, *, rho=1000.0, max_gif_frames=36)

Render true 2-D h and p/rho from exported VTK, plus physical budgets.

Call on the actual canonical DPHS instance AFTER successful nonlinear_affine/BDF2 simulation, H registration, and native export_to_pv(‘h’)/export_to_pv(‘p’). Only MPI1 is supported for this visual protocol; MPI2 physical parity is a separate audit.

examples.shallow_water.render_original_shallow_water_spatial_VTU(folder, true_times, *, rho=1000.0, max_gif_frames=36, figures_directory=None, gif_directory=None)

Render ONLY real GetFEM h/p FE fields from complete original VTU.

Used both by the normal canonical example and the standalone recovery of real original VTK files after a successful simulation. The recovery does not invent unavailable FE Hamiltonian quadratures.

examples.shallow_water.shallow_water_eq(*, factor_solver=None, final_time=0.5, time_step=0.0001, save_step=0.01, showcase=False, showcase_output=None)

A structure-preserving discretization of the inviscid shallow-water equation

Formulation Grad-Grad, homogeneous boundary condition, on a tank.

The executable entry point (python examples/shallow_water.py or python -m examples.shallow_water) enables showcase=True by DEFAULT: it solves the genuine inviscid equations and saves original CG2 h, DG1 p, 3-D height and velocity PNG/GIF, Hamiltonian and mass plots. It uses the separately qualified nonlinear_affine DAE initializer, leaving the PDE, h/p initial data, four zero controls, mesh, FEM, BDF2 and lagged gyro unchanged. Default CLI horizon: t_f=0.12. For historical compatibility, calling shallow_water_eq(showcase=False) or passing –no-showcase on the CLI retains the old pseudo-time initialization and single Hamiltonian / h PVD outputs (t_f=0.5). Visual images do not certify exact BDF2 energy conservation.

examples.shallow_water.true_pvd_files(root, name, true_times)

Demand exactly one genuine native VTK snapshot for each saved FE t.

examples.shallow_water.vtk_array(node, *, native_field_size=None)

Read native GetFEM ASCII/inline-binary VTK, validating true byte counts.

The original GetFEM 5.5 field exporter writes an IEEE Float32 count header for PointData (e.g. h: 547 points -> float32(547), p: 744 3-component VTK points from 1488 native 2D scalars -> float32(1488)). The same exporter uses a conventional UInt32 BYTE-count header for Points and Cells. Both encodings are independently verified on the native source-14181a94 archive. Do not silently parse all field headers as UInt32 byte counts.

Finite-dimensional models

Mass–spring–damper: a finite-dimensional port-Hamiltonian example.

State x=(q,p), with displacement q and momentum p=m*q_dot.

H(q,p) = k*q**2/2 + p**2/(2*m) e_q = dH/dq = k*q, e_p = dH/dp = p/m q_dot = e_p p_dot = -e_q - c*e_p + f(t)

Power balance: H_dot = -c*e_p**2 + f*e_p.

Each lumped port has a single DOF. The interval used here is only a normalization domain for GetFEM’s weak-form assembly; it is NOT a spatial discretization of the oscillator.

examples.mass_spring_damper.mass_spring_damper(final_time=8.0, time_step=0.01, plot=False, *, factor_solver=None, output_dir=None)

Simulate the forced, damped oscillator using lumped dynamic ports.

Returns

scrimp.DPHS

Solved system with Hamiltonian terms and time histories.

Prescribe exact constitutive co-energies at t=0 to preserve the physical initial states (q0,p0), without pseudo-time evolution.

examples.mass_spring_damper.render_mass_spring_visuals(system, folder, *, mass, stiffness, damping)

Original lumped-DOF time series, physical power and GIF.

Only the saved PETSc/GetFEM solution drives every figure and the mechanical schematic. This rendering is part of the canonical user example, not a research or acceptance helper. It never invents an FEM space for a zero-dimensional oscillator.

BDF2 energy-power identity is evaluated at k>=2 only; the PETSc internal startup half-step is not an ordinary BDF1 interval.

Nonlinear damped pendulum, modeled as a lumped port-Hamiltonian system.

States are the angle theta (radians) and angular momentum p = I*theta_dot. For a point mass m at distance L from the pivot, the governing relationships are:

I = m*L**2
H = p**2/(2*I) + m*g*L*(1-cos(theta))
e_theta = m*g*L*sin(theta),  e_p = p/I
theta_dot = e_p
p_dot = -e_theta - damping*e_p + tau(t)

This example keeps the full nonlinear sine restoring torque. It does not use the small-angle approximation sin(theta) = theta.

examples.nonlinear_pendulum.nonlinear_pendulum(final_time=12.0, time_step=0.01, initial_angle=1.2, torque_amplitude=0.0, plot=False, *, factor_solver=None, output_dir=None)

Integrate a nonlinear (possibly forced) pendulum with damping.

Initialize algebraic co-energies from the exact nonlinear sine constitutive law, preserving prescribed angle and momentum.

examples.nonlinear_pendulum.render_pendulum_visuals(system, folder, *, inertia, gravitational_torque, damping, length, torque_amplitude)

Render original accepted lumped GetFEM states in the example.

No extra ODE integration, spatial FE simulation, interpolated states, or fictitious exact CN Hamiltonian conservation identity. Figures are saved so that direct command-line runs work headlessly. The motion GIF is explicitly a mechanical schematic.