| | |
| | | package com.se.simu.service; |
| | | |
| | | import cn.hutool.json.JSONUtil; |
| | | import com.se.simu.config.PropertiesConfig; |
| | | import com.se.simu.domain.dto.ConfigDto; |
| | | import com.se.simu.domain.po.DataPo; |
| | | import com.se.simu.helper.StringHelper; |
| | | import com.se.simu.helper.WebHelper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import com.se.simu.Rainfall; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.*; |
| | | |
| | | /** |
| | | * 内涝求解器服务类 |
| | |
| | | @Resource |
| | | PropertiesConfig config; |
| | | |
| | | public void createConfig() { |
| | | /** |
| | | * 创建降雨文件 |
| | | * <p> |
| | | * https://blog.csdn.net/Dark_Drgon/article/details/139739924 |
| | | * C:\Program Files\matlab\R2020a\runtime\win64 |
| | | */ |
| | | public void createRainFile(DataPo data) throws Exception { |
| | | String filePath = config.getInPath() + File.separator + data.getInPath() + File.separator + config.getRaingage(); |
| | | String startTime = StringHelper.YMDHMS_FORMAT.format(data.getStartTime()); |
| | | |
| | | //MWCharArray file = new MWCharArray(filePath); |
| | | //MWCharArray station = new MWCharArray(config.getRainStation()); |
| | | //MWCharArray time = new MWCharArray(startTime); |
| | | |
| | | Rainfall rainfall = new Rainfall(); |
| | | //rainfall('D:\simu\in\RainGage.dat','Tongzhou','2024-09-29 00:00:00',60,0.5,10) |
| | | Object[] rs = rainfall.rainfall(filePath, config.getRainStation(), startTime, |
| | | Double.valueOf(data.getDuration()), 0.5, config.getRainPeriod()); |
| | | |
| | | // file.dispose(); |
| | | //station.dispose(); |
| | | //time.dispose(); |
| | | } |
| | | |
| | | public void createConfig(DataPo data) throws IOException { |
| | | ConfigDto dto = new ConfigDto(); |
| | | dto.setProperties(data.getInPath(), data.getStartTime(), data.getDuration(), config); |
| | | |
| | | String json = JSONUtil.toJsonPrettyStr(dto); |
| | | String filePath = config.getInPath() + File.separator + data.getInPath() + ".json"; |
| | | |
| | | FileWriter fw = new FileWriter(filePath); |
| | | BufferedWriter bw = new BufferedWriter(fw); |
| | | bw.write(json); |
| | | bw.close(); |
| | | fw.close(); |
| | | } |
| | | |
| | | public String callExe(DataPo data) throws Exception { |
| | | String cmd = String.format("%s %d %s", config.getSolverBat(), WebHelper.getCpuCores(), data.getInPath() + ".json"); |
| | | |
| | | return exec(cmd); |
| | | } |
| | | |
| | | private String exec(String cmd) throws Exception { |
| | | Process process = null; |
| | | BufferedReader nr = null; |
| | | BufferedReader er = null; |
| | | try { |
| | | // new String[] { "/bin/sh", "-c", cmd } |
| | | process = Runtime.getRuntime().exec(cmd); |
| | | nr = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK")); |
| | | er = new BufferedReader(new InputStreamReader(process.getErrorStream(), "GBK")); |
| | | |
| | | String errorLine; |
| | | while ((errorLine = er.readLine()) != null) { |
| | | log.warn(errorLine); |
| | | } |
| | | |
| | | String line; |
| | | StringBuilder sb = new StringBuilder(); |
| | | while ((line = nr.readLine()) != null) { |
| | | sb.append(line); |
| | | } |
| | | |
| | | // 等待程序执行结束并输出状态 |
| | | int exitCode = process.waitFor(); |
| | | |
| | | return sb.toString(); |
| | | } catch (Exception ex) { |
| | | throw ex; |
| | | } finally { |
| | | closeReader(er); |
| | | closeReader(nr); |
| | | closeProcess(process); |
| | | } |
| | | } |
| | | |
| | | private static void closeReader(Reader reader) { |
| | | if (null != reader) { |
| | | try { |
| | | reader.close(); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private static void closeProcess(Process process) { |
| | | if (null != process) { |
| | | try { |
| | | process.destroy(); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public String getKeyFrame(DataPo data) throws Exception { |
| | | String cmd = config.getKeyFrameBat() + " " + config.getInPath() + File.separator + data.getInPath() + File.separator + ".save" + File.separator + data.getInPath() + ".sww"; |
| | | |
| | | String str = exec(cmd); |
| | | if (StringHelper.isEmpty(str) || !str.contains("[")) { |
| | | throw new Exception("生成关键帧出错"); |
| | | } |
| | | |
| | | String rs = str.split("\\[", 2)[1].replace("]", "").replace(" ", ""); |
| | | if (StringHelper.isEmpty(rs)) { |
| | | throw new Exception("关键帧为空"); |
| | | } |
| | | |
| | | return rs; |
| | | } |
| | | |
| | | public void copeWaterFiles() { |
| | | // |
| | | } |
| | | |
| | | public void callExe() { |
| | | public void copeDrainFiles() { |
| | | // |
| | | } |
| | | } |