|
| 1 | +package edu.kit.provideq.toolbox.materialsimulation.solvers; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Optional; |
| 5 | + |
| 6 | +import org.springframework.stereotype.Component; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.context.ApplicationContext; |
| 10 | + |
| 11 | +import edu.kit.provideq.toolbox.Solution; |
| 12 | +import edu.kit.provideq.toolbox.materialsimulation.ansatzes.AnsatzConfiguration; |
| 13 | +import edu.kit.provideq.toolbox.materialsimulation.MaterialSimulationConfiguration; |
| 14 | +import edu.kit.provideq.toolbox.meta.SolvingProperties; |
| 15 | +import edu.kit.provideq.toolbox.meta.SubRoutineDefinition; |
| 16 | +import edu.kit.provideq.toolbox.meta.SubRoutineResolver; |
| 17 | +import edu.kit.provideq.toolbox.meta.setting.SolverSetting; |
| 18 | +import edu.kit.provideq.toolbox.meta.setting.basic.IntegerSetting; |
| 19 | +import edu.kit.provideq.toolbox.process.ProcessRunner; |
| 20 | +import edu.kit.provideq.toolbox.process.PythonProcessRunner; |
| 21 | +import reactor.core.publisher.Mono; |
| 22 | +import reactor.core.scheduler.Schedulers; |
| 23 | + |
| 24 | + |
| 25 | +/** |
| 26 | + * {@link MaterialSimulationConfiguration#MATERIAL_SIMULATION} "solver" using a PySCF implementation. |
| 27 | + * It creates an electronic structure problem using a PySCF driver for material simulation. |
| 28 | + * It allows to define the charge and spin of the molecule. |
| 29 | + */ |
| 30 | +@Component |
| 31 | +public class PyscfDriver extends MaterialSimulationSolver { |
| 32 | + private static final SubRoutineDefinition<String, String> ANSATZ_SUBROUTINE = |
| 33 | + new SubRoutineDefinition<>(AnsatzConfiguration.MATERIAL_SIMULATION_ANSATZ, "Which Ansatz should be used?"); |
| 34 | + private final String scriptPath; |
| 35 | + private final String venv; |
| 36 | + private final ApplicationContext context; |
| 37 | + |
| 38 | + private static final String SETTING_CHARGE = "Charge"; |
| 39 | + private static final int DEFAULT_CHARGE = 0; |
| 40 | + |
| 41 | + private static final String SETTING_SPIN = "Spin"; |
| 42 | + private static final int DEFAULT_SPIN = 0; |
| 43 | + |
| 44 | + @Autowired |
| 45 | + public PyscfDriver( |
| 46 | + @Value("${path.custom.materialsimulation-driver-pyscf}") String scriptPath, |
| 47 | + @Value("${venv.custom.materialsimulation-driver-pyscf}") String venv, |
| 48 | + ApplicationContext context) { |
| 49 | + this.scriptPath = scriptPath; |
| 50 | + this.venv = venv; |
| 51 | + this.context = context; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String getName() { |
| 56 | + return "PySCF Driver"; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public String getDescription() { |
| 61 | + return "Creates an Electronic Structure Problem using the Pyscf Driver " |
| 62 | + + "for Material Simulation. It allows to define the charge and spin of the molecule."; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public List<SolverSetting> getSolverSettings() { |
| 67 | + return List.of( |
| 68 | + new IntegerSetting( |
| 69 | + SETTING_CHARGE, |
| 70 | + "The charge of the molecule. The parameter to define " |
| 71 | + + "the total number of electrons in the system.", |
| 72 | + DEFAULT_CHARGE, |
| 73 | + 0, Integer.MAX_VALUE |
| 74 | + ), |
| 75 | + new IntegerSetting( |
| 76 | + SETTING_SPIN, |
| 77 | + "The spin of the molecule. Equals the number of unpaired electrons 2S," |
| 78 | + + " i.e. the difference between the number of alpha and beta electrons.", |
| 79 | + DEFAULT_SPIN, |
| 80 | + 0, Integer.MAX_VALUE |
| 81 | + ) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public Mono<Solution<String>> solve( |
| 87 | + String input, |
| 88 | + SubRoutineResolver subRoutineResolver, |
| 89 | + SolvingProperties properties |
| 90 | + ) { |
| 91 | + var solution = new Solution<>(this); |
| 92 | + |
| 93 | + int charge = properties.<IntegerSetting>getSetting(SETTING_CHARGE) |
| 94 | + .map(IntegerSetting::getValue) |
| 95 | + .orElse(DEFAULT_CHARGE); |
| 96 | + |
| 97 | + int spin = properties.<IntegerSetting>getSetting(SETTING_SPIN) |
| 98 | + .map(IntegerSetting::getValue) |
| 99 | + .orElse(DEFAULT_SPIN); |
| 100 | + |
| 101 | + if (charge < 0) { |
| 102 | + throw new IllegalArgumentException("Charge must be non-negative"); |
| 103 | + } |
| 104 | + if (spin < 0) { |
| 105 | + throw new IllegalArgumentException("Spin must be non-negative"); |
| 106 | + } |
| 107 | + |
| 108 | + var processResult = context |
| 109 | + .getBean(PythonProcessRunner.class, scriptPath, venv) |
| 110 | + .withArguments( |
| 111 | + ProcessRunner.INPUT_FILE_PATH, |
| 112 | + ProcessRunner.OUTPUT_FILE_PATH, |
| 113 | + "--charge", String.valueOf(charge), |
| 114 | + "--spin", String.valueOf(spin) |
| 115 | + ) |
| 116 | + .writeInputFile(input) |
| 117 | + .readOutputFile() |
| 118 | + .run(getProblemType(), solution.getId()); |
| 119 | + |
| 120 | + Optional<String> output = processResult.output(); |
| 121 | + if (!processResult.success() || output.isEmpty()) { |
| 122 | + solution.setDebugData(processResult.errorOutput().orElse("Unknown error occurred.")); |
| 123 | + solution.abort(); |
| 124 | + return Mono.just(solution); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + return subRoutineResolver.runSubRoutine(ANSATZ_SUBROUTINE, output.get()) |
| 130 | + .publishOn(Schedulers.boundedElastic()) // avoids blocking from Files.writeString() in try/catch |
| 131 | + .map(subRoutineSolution -> { |
| 132 | + if (subRoutineSolution.getSolutionData() == null |
| 133 | + || subRoutineSolution.getSolutionData().isEmpty()) { |
| 134 | + solution.setDebugData("Subroutine did not return a valid solution."); |
| 135 | + solution.abort(); |
| 136 | + return solution; |
| 137 | + } |
| 138 | + |
| 139 | + solution.setSolutionData(subRoutineSolution.getSolutionData()); |
| 140 | + solution.complete(); |
| 141 | + |
| 142 | + return solution; |
| 143 | + } |
| 144 | + ); |
| 145 | + } |
| 146 | +} |
| 147 | + |
0 commit comments