diff --git a/1.xlsx b/1.xlsx index e6241e7..342189b 100644 Binary files a/1.xlsx and b/1.xlsx differ diff --git a/Fortran/SUB_appcoef.f90 b/Fortran/SUB_appcoef.f90 index b356ccf..aa8d756 100644 --- a/Fortran/SUB_appcoef.f90 +++ b/Fortran/SUB_appcoef.f90 @@ -27,52 +27,60 @@ case('db3') lf = 6 allocate(Lo_D(lf),Hi_D(lf)) - Lo_D = [0.332670552950957,0.806891509313339,0.459877502119331,-0.135011020010391,-0.0854412738822415,0.0352262918821007] - Hi_D = [0.0352262918821007,0.0854412738822415,-0.135011020010391,-0.459877502119331,0.806891509313339,-0.332670552950957] + Lo_D = [0.332670552950957,0.806891509313339,0.459877502119331,& + -0.135011020010391,-0.0854412738822415,0.0352262918821007] + Hi_D = [0.0352262918821007,0.0854412738822415,-0.135011020010391,& + -0.459877502119331,0.806891509313339,-0.332670552950957] case('db4') lf = 8 allocate(Lo_D(lf),Hi_D(lf)) - Lo_D = [0.230377813308855,0.714846570552542,0.630880767929590,-0.0279837694169839,-0.187034811718881,0.0308413818359870,& - 0.0328830116669829,-0.0105974017849973] - Hi_D = [-0.0105974017849973,-0.0328830116669829,0.0308413818359870,0.187034811718881,-0.0279837694169839,-0.630880767929590,& - 0.714846570552542,-0.230377813308855] + Lo_D = [0.230377813308855,0.714846570552542,0.630880767929590,-0.0279837694169839,& + -0.187034811718881,0.0308413818359870,0.0328830116669829,-0.0105974017849973] + Hi_D = [-0.0105974017849973,-0.0328830116669829,0.0308413818359870,0.187034811718881,& + -0.0279837694169839,-0.630880767929590,0.714846570552542,-0.230377813308855] case('db5') lf = 10 allocate(Lo_D(lf),Hi_D(lf)) - Lo_D = [0.160102397974125,0.603829269797473,0.724308528438574,0.138428145901103,-0.242294887066190,-0.0322448695850295,& - 0.0775714938400652,-0.00624149021301171,-0.0125807519990155,0.00333572528500155] - Hi_D = [0.00333572528500155,0.0125807519990155,-0.00624149021301171,-0.0775714938400652,-0.0322448695850295,0.242294887066190,& - 0.138428145901103,-0.724308528438574,0.603829269797473,-0.160102397974125] + Lo_D = [0.160102397974125,0.603829269797473,0.724308528438574,0.138428145901103,& + -0.242294887066190,-0.0322448695850295,0.0775714938400652,& + -0.00624149021301171,-0.0125807519990155,0.00333572528500155] + Hi_D = [0.00333572528500155,0.0125807519990155,-0.00624149021301171,& + -0.0775714938400652,-0.0322448695850295,0.242294887066190,0.138428145901103,& + -0.724308528438574,0.603829269797473,-0.160102397974125] case('db6') lf = 12 allocate(Lo_D(lf),Hi_D(lf)) - Lo_D = [0.111540743350080,0.494623890398385,0.751133908021578,0.315250351709243,-0.226264693965169,-0.129766867567096,& - 0.0975016055870794,0.0275228655300163,-0.0315820393180312,0.000553842200993802,0.00477725751101065,-0.00107730108499558] + Lo_D = [0.111540743350080,0.494623890398385,0.751133908021578,0.315250351709243,& + -0.226264693965169,-0.129766867567096,0.0975016055870794,0.0275228655300163,& + -0.0315820393180312,0.000553842200993802,0.00477725751101065,-0.00107730108499558] Hi_D = [-0.00107730108499558,-0.00477725751101065,0.000553842200993802,0.0315820393180312,0.0275228655300163,-0.0975016055870794,& -0.129766867567096,0.226264693965169,0.315250351709243,-0.751133908021578,0.494623890398385,-0.111540743350080] case('db7') lf = 14 allocate(Lo_D(lf),Hi_D(lf)) - Lo_D = [0.0778520540850624,0.396539319482306,0.729132090846555,0.469782287405359,-0.143906003929106,-0.224036184994166,& - 0.0713092192670500,0.0806126091510659,-0.0380299369350346,-0.0165745416310156,0.0125509985560138,0.000429577973004703,& - -0.00180164070399983,0.000353713800001040] - Hi_D = [0.000353713800001040,0.00180164070399983,0.000429577973004703,-0.0125509985560138,-0.0165745416310156,0.0380299369350346,& - 0.0806126091510659,-0.0713092192670500,-0.224036184994166,0.143906003929106,0.469782287405359,-0.729132090846555,& - 0.396539319482306,-0.0778520540850624] + Lo_D = [0.0778520540850624,0.396539319482306,0.729132090846555,0.469782287405359,-0.143906003929106,& + -0.224036184994166,0.0713092192670500,0.0806126091510659,-0.0380299369350346,& + -0.0165745416310156,0.0125509985560138,0.000429577973004703,-0.00180164070399983,0.000353713800001040] + Hi_D = [0.000353713800001040,0.00180164070399983,0.000429577973004703,-0.0125509985560138,& + -0.0165745416310156,0.0380299369350346,0.0806126091510659,-0.0713092192670500,& + -0.224036184994166,0.143906003929106,0.469782287405359,-0.729132090846555,0.396539319482306,-0.0778520540850624] case('db8') lf = 16 allocate(Lo_D(lf),Hi_D(lf)) - Lo_D = [0.0544158422430816,0.312871590914466,0.675630736298013,0.585354683654869,-0.0158291052560239,-0.284015542962428,& - 0.000472484573997973,0.128747426620186,-0.0173693010020221,-0.0440882539310647,0.0139810279170155,0.00874609404701566,& + Lo_D = [0.0544158422430816,0.312871590914466,0.675630736298013,0.585354683654869,-0.0158291052560239,& + -0.284015542962428,0.000472484573997973,0.128747426620186,-0.0173693010020221,& + -0.0440882539310647,0.0139810279170155,0.00874609404701566,& -0.00487035299301066,-0.000391740372995977,0.000675449405998557,-0.000117476784002282] - Hi_D = [-0.000117476784002282,-0.000675449405998557,-0.000391740372995977,0.00487035299301066,0.00874609404701566,-0.0139810279170155,& - -0.0440882539310647,0.0173693010020221,0.128747426620186,-0.000472484573997973,-0.284015542962428,0.0158291052560239,& - 0.585354683654869,-0.675630736298013,0.312871590914466,-0.0544158422430816] + Hi_D = [-0.000117476784002282,-0.000675449405998557,& + -0.000391740372995977,0.00487035299301066,0.00874609404701566,-0.0139810279170155,& + -0.0440882539310647,0.0173693010020221,0.128747426620186,-0.000472484573997973,& + -0.284015542962428,0.0158291052560239,0.585354683654869,& + -0.675630736298013,0.312871590914466,-0.0544158422430816] case('fk4') lf = 4 @@ -89,10 +97,10 @@ case('fk8') lf = 8 allocate(Lo_D(lf),Hi_D(lf)) - Lo_D = [0.349238111863800,0.782683620384065,0.475265135079471,-0.0996833284505732,-0.159978097434030,0.0431066681065162,& - 0.0425816316775818,-0.0190001788537359] - Hi_D = [-0.0190001788537359,-0.0425816316775818,0.0431066681065162,0.159978097434030,-0.0996833284505732,-0.475265135079471,& - 0.782683620384065,-0.349238111863800] + Lo_D = [0.349238111863800,0.782683620384065,0.475265135079471,-0.0996833284505732,& + -0.159978097434030,0.0431066681065162,0.0425816316775818,-0.0190001788537359] + Hi_D = [-0.0190001788537359,-0.0425816316775818,0.0431066681065162,0.159978097434030,& + -0.0996833284505732,-0.475265135079471,0.782683620384065,-0.349238111863800] end select diff --git a/Fortran/SUB_wavedec.f90 b/Fortran/SUB_wavedec.f90 index 7978b55..b910b2c 100644 --- a/Fortran/SUB_wavedec.f90 +++ b/Fortran/SUB_wavedec.f90 @@ -28,16 +28,16 @@ case('db4') lf = 8 allocate(Lo_D(lf),Hi_D(lf)) - Lo_D = [-0.0105974017849973,0.0328830116669829,0.0308413818359870,-0.187034811718881,-0.0279837694169839,0.630880767929590,& - 0.714846570552542,0.230377813308855] + Lo_D = [-0.0105974017849973,0.0328830116669829,0.0308413818359870,-0.187034811718881,& + -0.0279837694169839,0.630880767929590,0.714846570552542,0.230377813308855] Hi_D = [-0.230377813308855,0.714846570552542,-0.630880767929590,-0.0279837694169839,0.187034811718881,0.0308413818359870,& -0.0328830116669829,-0.0105974017849973] case('db5') lf = 10 allocate(Lo_D(lf),Hi_D(lf)) - Lo_D = [0.00333572528500155,-0.0125807519990155,-0.00624149021301171,0.0775714938400652,-0.0322448695850295,-0.242294887066190,& - 0.138428145901103,0.724308528438574,0.603829269797473,0.160102397974125] + Lo_D = [0.00333572528500155,-0.0125807519990155,-0.00624149021301171,0.0775714938400652,-0.0322448695850295,& + -0.242294887066190,0.138428145901103,0.724308528438574,0.603829269797473,0.160102397974125] Hi_D = [-0.160102397974125,0.603829269797473,-0.724308528438574,0.138428145901103,0.242294887066190,-0.0322448695850295,& -0.0775714938400652,-0.00624149021301171,0.0125807519990155,0.00333572528500155] @@ -52,22 +52,24 @@ case('db7') lf = 14 allocate(Lo_D(lf),Hi_D(lf)) - Lo_D = [0.000353713800001040,-0.00180164070399983,0.000429577973004703,0.0125509985560138,-0.0165745416310156,-0.0380299369350346,& - 0.0806126091510659,0.0713092192670500,-0.224036184994166,-0.143906003929106,0.469782287405359,0.729132090846555,& - 0.396539319482306,0.0778520540850624] - Hi_D = [-0.0778520540850624,0.396539319482306,-0.729132090846555,0.469782287405359,0.143906003929106,-0.224036184994166,& - -0.0713092192670500,0.0806126091510659,0.0380299369350346,-0.0165745416310156,-0.0125509985560138,0.000429577973004703,& - 0.00180164070399983,0.000353713800001040] + Lo_D = [0.000353713800001040,-0.00180164070399983,0.000429577973004703,0.0125509985560138,-0.0165745416310156,& + -0.0380299369350346,0.0806126091510659,0.0713092192670500,-0.224036184994166,& + -0.143906003929106,0.469782287405359,0.729132090846555,0.396539319482306,0.0778520540850624] + Hi_D = [-0.0778520540850624,0.396539319482306,-0.729132090846555,0.469782287405359,0.143906003929106,& + -0.224036184994166,-0.0713092192670500,0.0806126091510659,0.0380299369350346,-0.0165745416310156,& + -0.0125509985560138,0.000429577973004703,0.00180164070399983,0.000353713800001040] case('db8') lf = 16 allocate(Lo_D(lf),Hi_D(lf)) - Lo_D = [-0.000117476784002282,0.000675449405998557,-0.000391740372995977,-0.00487035299301066,0.00874609404701566,0.0139810279170155,& - -0.0440882539310647,-0.0173693010020221,0.128747426620186,0.000472484573997973,-0.284015542962428,-0.0158291052560239,& - 0.585354683654869,0.675630736298013,0.312871590914466,0.0544158422430816] + Lo_D = [-0.000117476784002282,0.000675449405998557,-0.000391740372995977,& + -0.00487035299301066,0.00874609404701566,0.0139810279170155,& + -0.0440882539310647,-0.0173693010020221,0.128747426620186,0.000472484573997973,& + -0.284015542962428,-0.0158291052560239,0.585354683654869,0.675630736298013,0.312871590914466,0.0544158422430816] Hi_D = [-0.0544158422430816,0.312871590914466,-0.675630736298013,0.585354683654869,0.0158291052560239,-0.284015542962428,& - -0.000472484573997973,0.128747426620186,0.0173693010020221,-0.0440882539310647,-0.0139810279170155,0.00874609404701566,& - 0.00487035299301066,-0.000391740372995977,-0.000675449405998557,-0.000117476784002282] + -0.000472484573997973,0.128747426620186,0.0173693010020221,-0.0440882539310647,& + -0.0139810279170155,0.00874609404701566,0.00487035299301066,-0.000391740372995977,& + -0.000675449405998557,-0.000117476784002282] case('fk4') lf = 4 @@ -84,8 +86,8 @@ case('fk8') lf = 8 allocate(Lo_D(lf),Hi_D(lf)) - Lo_D = [-0.0190001788537359,0.0425816316775818,0.0431066681065162,-0.159978097434030,-0.0996833284505732,0.475265135079471,& - 0.782683620384065,0.349238111863800] + Lo_D = [-0.0190001788537359,0.0425816316775818,0.0431066681065162,-0.159978097434030,& + -0.0996833284505732,0.475265135079471,0.782683620384065,0.349238111863800] Hi_D = [-0.349238111863800,0.782683620384065,-0.475265135079471,-0.0996833284505732,0.159978097434030,0.0431066681065162,& -0.0425816316775818,-0.0190001788537359] diff --git a/FortranWebApi/Controllers/FortranController.cs b/FortranWebApi/Controllers/FortranController.cs new file mode 100644 index 0000000..46a63e2 --- /dev/null +++ b/FortranWebApi/Controllers/FortranController.cs @@ -0,0 +1,74 @@ +using Microsoft.AspNetCore.Mvc; +using FortranWebApi.Models; +using FortranWebApi.Services; +using System.Text.Json; + +namespace FortranWebApi.Controllers +{ + [ApiController] + [Route("[controller]")] + public class FortranCalculateController : ControllerBase + { + private readonly FortranInteropService _fortranService; + private readonly ILogger _logger; + + public FortranCalculateController(FortranInteropService fortranService, ILogger logger) + { + _fortranService = fortranService; + _logger = logger; + } + + [HttpPost] + public IActionResult Post([FromBody] FortranRequestWrapper wrapper) + { + try + { + if (string.IsNullOrEmpty(wrapper.Text)) + { + return BadRequest(new + { + message = "请求文本不能为空", + success = false, + data = (object)null + }); + } + + string result = _fortranService.ProcessFortranRequest(wrapper.Text); + + // 使用驼峰命名法解析结果 + var options = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, + PropertyNameCaseInsensitive = true, + WriteIndented = true + }; + + var resultObj = JsonSerializer.Deserialize(result, options); + + // 返回新的格式 + var response = new ApiResponse + { + Message = "Success", + Success = true, + Data = new ApiResponseData + { + Value = resultObj + } + }; + + return Ok(response); + } + catch (Exception ex) + { + _logger.LogError(ex, "处理Fortran请求时发生错误"); + return StatusCode(500, new + { + message = ex.Message, + success = false, + data = (object)null + }); + } + } + } +} \ No newline at end of file diff --git a/FortranWebApi/Dockerfile b/FortranWebApi/Dockerfile new file mode 100644 index 0000000..4074a30 --- /dev/null +++ b/FortranWebApi/Dockerfile @@ -0,0 +1,88 @@ +# ===== 第一阶段:构建阶段 ===== +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build + +# 配置 NuGet 使用国内镜像源 +RUN dotnet nuget add source https://mirrors.cloud.tencent.com/nuget/ \ + && dotnet nuget disable source nuget.org + +# 配置 apt-get 使用 apt-cacher-ng 作为代理 +RUN echo 'Acquire::http::Proxy "http://192.168.1.140:3142";' > /etc/apt/apt.conf.d/01proxy + +# 创建并配置 Debian 镜像源 +RUN echo "deb https://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \ + echo "deb https://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \ + echo "deb https://mirrors.ustc.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list + +# 允许使用不安全的软件源 +RUN echo 'Acquire::AllowInsecureRepositories "true";' > /etc/apt/apt.conf.d/99allow-insecure && \ + echo 'Acquire::AllowDowngradeToInsecureRepositories "true";' >> /etc/apt/apt.conf.d/99allow-insecure + +WORKDIR /src + +# 安装 Fortran 编译器 +RUN apt-get update && apt-get install -y --no-install-recommends \ + gfortran \ + && rm -rf /var/lib/apt/lists/* + +# 复制 Fortran 源文件并编译 +COPY Fortran/*.f90 /src/Fortran/ +WORKDIR /src/Fortran + +# 编译所有 .f90 文件为 .o 文件 +RUN for file in *.f90; do gfortran -fPIC -c "$file" -o "${file%.f90}.o"; done + +# 链接所有 .o 文件为共享库 +RUN gfortran -shared *.o -o libSUB_WAVELET.so + +# 回到主工作目录 +WORKDIR /src + +# 只复制项目文件 +COPY ["FortranWebApi.csproj", "./"] +RUN dotnet restore + +# 复制源代码 +COPY . . + +# 合并 build 和 publish 命令 +RUN dotnet publish -c Release -o /app/publish + +# 复制编译好的 .so 文件到发布目录 +RUN mkdir -p /app/publish && \ + cp /src/Fortran/libSUB_WAVELET.so /app/publish/ + +# ===== 第二阶段:运行阶段 ===== +FROM mcr.microsoft.com/dotnet/aspnet:8.0 + +# 配置 apt-get 使用 apt-cacher-ng 作为代理 +RUN echo 'Acquire::http::Proxy "http://192.168.1.140:3142";' > /etc/apt/apt.conf.d/01proxy + +# 创建并配置 Debian 镜像源 +RUN echo "deb https://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \ + echo "deb https://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \ + echo "deb https://mirrors.ustc.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list + +# 允许使用不安全的软件源 +RUN echo 'Acquire::AllowInsecureRepositories "true";' > /etc/apt/apt.conf.d/99allow-insecure && \ + echo 'Acquire::AllowDowngradeToInsecureRepositories "true";' >> /etc/apt/apt.conf.d/99allow-insecure + +# 安装运行时依赖 +RUN apt-get update && apt-get install -y --no-install-recommends \ + libc6-dev \ + libgfortran5 \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /app +COPY --from=build /app/publish . + +# 确保 .so 文件的权限正确 +RUN chmod 755 /app/libSUB_WAVELET.so + +# 设置 LD_LIBRARY_PATH +ENV LD_LIBRARY_PATH=/app + +# 设置端口和监听地址 +ENV ASPNETCORE_URLS=http://+:5000 +EXPOSE 5000 + +ENTRYPOINT ["dotnet", "FortranWebApi.dll"] \ No newline at end of file diff --git a/FortranWebApi/FortranWebApi.csproj b/FortranWebApi/FortranWebApi.csproj new file mode 100644 index 0000000..6a4d551 --- /dev/null +++ b/FortranWebApi/FortranWebApi.csproj @@ -0,0 +1,27 @@ + + + + net8.0 + enable + enable + true + + + + + + + + + + + + + + + + PreserveNewest + + + + \ No newline at end of file diff --git a/FortranWebApi/HttpRequests/fortranwebapi.http b/FortranWebApi/HttpRequests/fortranwebapi.http new file mode 100644 index 0000000..1c35b49 --- /dev/null +++ b/FortranWebApi/HttpRequests/fortranwebapi.http @@ -0,0 +1,13 @@ +@FortranWebApi_HostAddress = http://localhost:5000 + +### 测试Fortran请求 + +POST {{FortranWebApi_HostAddress}}/FortranCalculate +Content-Type: application/json + +{ + "text": "{\"FuncName\":\"calculate_main\",\"ClassName\":\"\",\"Par\":[ {"Name":"n","DataType":"0","ArrayType":"0","IsOut":"2","Data":0}, + {"Name":"Data_Error","DataType":"1","ArrayType":"1","IsOut":"2","Data":[]}, + {"Name":"Data_Re","DataType":"1","ArrayType":"1","IsOut":"1","Data":[]}, + {"Name":"Error","DataType":"1","ArrayType":"1","IsOut":"1","Data":[]}]}" +} \ No newline at end of file diff --git a/FortranWebApi/Models/ApiResponse.cs b/FortranWebApi/Models/ApiResponse.cs new file mode 100644 index 0000000..3845aa5 --- /dev/null +++ b/FortranWebApi/Models/ApiResponse.cs @@ -0,0 +1,31 @@ +using System.Text.Json.Serialization; + +namespace FortranWebApi.Models +{ + public class ApiResponse + { + [JsonPropertyName("message")] + public string Message { get; set; } = "Success"; + + [JsonPropertyName("success")] + public bool Success { get; set; } = true; + + [JsonPropertyName("data")] + public ApiResponseData? Data { get; set; } + } + + public class ApiResponseData + { + [JsonPropertyName("contentType")] + public string? ContentType { get; set; } + + [JsonPropertyName("serializerSettings")] + public object? SerializerSettings { get; set; } + + [JsonPropertyName("statusCode")] + public int? StatusCode { get; set; } + + [JsonPropertyName("value")] + public T? Value { get; set; } + } +} \ No newline at end of file diff --git a/FortranWebApi/Models/FortranParameter.cs b/FortranWebApi/Models/FortranParameter.cs new file mode 100644 index 0000000..78d68f0 --- /dev/null +++ b/FortranWebApi/Models/FortranParameter.cs @@ -0,0 +1,22 @@ +using System.Text.Json.Serialization; + +namespace FortranWebApi.Models +{ + public class FortranParameter + { + [JsonPropertyName("name")] + public string Name { get; set; } = string.Empty; + + [JsonPropertyName("dataType")] + public string DataType { get; set; } = "0"; + + [JsonPropertyName("arrayType")] + public string ArrayType { get; set; } = "0"; + + [JsonPropertyName("isOut")] + public string IsOut { get; set; } = "2"; + + [JsonPropertyName("data")] + public object? Data { get; set; } + } +} \ No newline at end of file diff --git a/FortranWebApi/Models/FortranRequest.cs b/FortranWebApi/Models/FortranRequest.cs new file mode 100644 index 0000000..e17fc88 --- /dev/null +++ b/FortranWebApi/Models/FortranRequest.cs @@ -0,0 +1,13 @@ +using System.Text.Json.Serialization; + +namespace FortranWebApi.Models +{ + public class FortranRequest + { + public string FuncName { get; set; } = string.Empty; + public string ClassName { get; set; } = string.Empty; + + [JsonPropertyName("par")] + public List Parameters { get; set; } = new List(); + } +} \ No newline at end of file diff --git a/FortranWebApi/Models/FortranRequestWrapper.cs b/FortranWebApi/Models/FortranRequestWrapper.cs new file mode 100644 index 0000000..49c36c2 --- /dev/null +++ b/FortranWebApi/Models/FortranRequestWrapper.cs @@ -0,0 +1,7 @@ +namespace FortranWebApi.Models +{ + public class FortranRequestWrapper + { + public string Text { get; set; } = string.Empty; + } +} \ No newline at end of file diff --git a/FortranWebApi/Program.cs b/FortranWebApi/Program.cs new file mode 100644 index 0000000..ac334f1 --- /dev/null +++ b/FortranWebApi/Program.cs @@ -0,0 +1,80 @@ +using Serilog; +using FortranWebApi.Services; +using System.IO; +using System.Text.Json; +using System.Text.Json.Serialization; + +// 设置库搜索路径 +string currentDir = Directory.GetCurrentDirectory(); +Environment.SetEnvironmentVariable("LD_LIBRARY_PATH", + $"{Environment.GetEnvironmentVariable("LD_LIBRARY_PATH")}:{currentDir}"); + +var builder = WebApplication.CreateBuilder(args); + +// 添加健康检查服务 +builder.Services.AddHealthChecks(); + +// 配置Serilog +Log.Logger = new LoggerConfiguration() + .WriteTo.Console(outputTemplate: + "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}") + .WriteTo.File("logs/log-.txt", + rollingInterval: RollingInterval.Day, + outputTemplate: + "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}") + .CreateLogger(); + +builder.Host.UseSerilog(); // 将Serilog添加到Host + +// 配置JSON序列化选项,使用驼峰命名法 +builder.Services.AddControllers().AddJsonOptions(options => +{ + options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; + options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase; + options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; + options.JsonSerializerOptions.PropertyNameCaseInsensitive = true; +}); + +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.Services.AddSingleton(); + +// 配置CORS +builder.Services.AddCors(options => +{ + options.AddDefaultPolicy(policy => + { + policy.AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader(); + }); +}); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseCors(); +app.UseAuthorization(); +// 映射健康检查端点 +app.MapHealthChecks("/health"); +app.MapControllers(); + +try +{ + Log.Information("启动应用程序..."); + app.Run(); +} +catch (Exception ex) +{ + Log.Fatal(ex, "应用程序启动失败"); +} +finally +{ + Log.CloseAndFlush(); +} \ No newline at end of file diff --git a/FortranWebApi/Properties/launchSettings.json b/FortranWebApi/Properties/launchSettings.json new file mode 100644 index 0000000..295de27 --- /dev/null +++ b/FortranWebApi/Properties/launchSettings.json @@ -0,0 +1,14 @@ +{ + "profiles": { + "FortranWebApi": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": true, + "applicationUrl": "http://0.0.0.0:5000" + } + } +} \ No newline at end of file diff --git a/FortranWebApi/Services/FortranInteropService.cs b/FortranWebApi/Services/FortranInteropService.cs new file mode 100644 index 0000000..4e9dd67 --- /dev/null +++ b/FortranWebApi/Services/FortranInteropService.cs @@ -0,0 +1,519 @@ +using System.Runtime.InteropServices; +using FortranWebApi.Models; +using System.Text.Json; +using System.Runtime.InteropServices.Marshalling; +using System.IO; + +namespace FortranWebApi.Services +{ + public class FortranInteropService + { + // 静态构造函数设置DLL导入解析器 + static FortranInteropService() + { + // 添加当前目录到库搜索路径 + NativeLibrary.SetDllImportResolver(typeof(FortranInteropService).Assembly, (name, assembly, path) => + { + if (name == "libSUB_WAVELET.so") + { + // 尝试从当前目录加载 + string currentDir = Directory.GetCurrentDirectory(); + string libraryPath = Path.Combine(currentDir, "libSUB_WAVELET.so"); + + if (File.Exists(libraryPath)) + { + return NativeLibrary.Load(libraryPath); + } + } + + // 回退到默认加载行为 + return IntPtr.Zero; + }); + } + + // DllImport声明 + [DllImport("libSUB_WAVELET.so", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SUB_WAVELET")] + private static extern void SUB_WAVELET( + ref int n, + double[] Data_Error, + double[] Data_Re, + double[] Error + ); + + public string ProcessFortranRequest(string requestText) + { + try + { + // 解析请求 + var options = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, + PropertyNameCaseInsensitive = true, + WriteIndented = true + }; + + var request = JsonSerializer.Deserialize(requestText, options); + if (request == null) + { + throw new ArgumentException("无效的请求格式"); + } + + // 准备输入参数 + var parameters = request.Parameters; + + // 提取参数 + int n = GetIntParameter(parameters, "n"); + double[] Data_Error = GetDoubleArrayParameter(parameters, "Data_Error"); + + + // 准备数组参数 + double[] Data_Re = new double[10000]; // 输出数组,初始大小为10000 + double[] Error = new double[10000]; // 输出数组,初始大小为10000 + + + // 调用Fortran函数 + SUB_WAVELET( + ref n, + Data_Error, + Data_Re, + Error + ); + + // 更新输出参数 + UpdateDoubleArrayParameter(parameters, "Data_Re", Data_Re); + UpdateDoubleArrayParameter(parameters, "Error", Error); + + + // 处理输出数组 + // 处理输出数组 Data_Re + // 注意:没有找到明确的长度参数,使用非零元素数量 + { + int nonZeroCount = 0; + for (int i = 0; i < Data_Re.Length; i++) + { + if (Data_Re[i] != 0) nonZeroCount = i + 1; + } + if (nonZeroCount > 0) + { + double[] resultArray = new double[nonZeroCount]; + Array.Copy(Data_Re, resultArray, nonZeroCount); + UpdateDoubleArrayParameter(parameters, "Data_Re", resultArray); + } + } + // 处理输出数组 Error + // 注意:没有找到明确的长度参数,使用非零元素数量 + { + int nonZeroCount = 0; + for (int i = 0; i < Error.Length; i++) + { + if (Error[i] != 0) nonZeroCount = i + 1; + } + if (nonZeroCount > 0) + { + double[] resultArray = new double[nonZeroCount]; + Array.Copy(Error, resultArray, nonZeroCount); + UpdateDoubleArrayParameter(parameters, "Error", resultArray); + } + } + + + // 返回结果 - 只返回原始请求的结构,但包含更新后的参数 + var result = new FortranRequest + { + FuncName = request.FuncName, + ClassName = request.ClassName, + Parameters = parameters + }; + + return JsonSerializer.Serialize(result, options); + } + catch (Exception ex) + { + return JsonSerializer.Serialize(new { error = ex.Message }); + } + } + + private float GetFloatParameter(List parameters, string name) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param == null || param.Data == null) + { + return 0.0f; + } + + if (param.Data is JsonElement element) + { + if (element.ValueKind == JsonValueKind.Number) + { + return element.GetSingle(); + } + } + + return Convert.ToSingle(param.Data); + } + private double GetDoubleParameter(List parameters, string name) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param == null || param.Data == null) + { + return 0.0; + } + + if (param.Data is JsonElement element) + { + if (element.ValueKind == JsonValueKind.Number) + { + return element.GetSingle(); + } + } + + return Convert.ToDouble(param.Data); + } + private int GetIntParameter(List parameters, string name) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param == null || param.Data == null) + { + return 0; + } + + if (param.Data is JsonElement element) + { + if (element.ValueKind == JsonValueKind.Number) + { + return element.GetInt32(); + } + } + + return Convert.ToInt32(param.Data); + } + private int[] GetIntArrayParameter(List parameters, string name) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param == null || param.Data == null) + { + return Array.Empty(); + } + + if (param.Data is JsonElement element && element.ValueKind == JsonValueKind.Array) + { + var array = new List(); + foreach (var item in element.EnumerateArray()) + { + if (item.ValueKind == JsonValueKind.Number) + { + array.Add(item.GetInt32()); + } + } + return array.ToArray(); + } + + if (param.Data is System.Collections.IEnumerable enumerable && !(param.Data is string)) + { + var array = new List(); + foreach (var item in enumerable) + { + array.Add(Convert.ToInt32(item)); + } + return array.ToArray(); + } + + return Array.Empty(); + } + private float[] GetFloatArrayParameter(List parameters, string name) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param == null || param.Data == null) + { + return Array.Empty(); + } + + if (param.Data is JsonElement element && element.ValueKind == JsonValueKind.Array) + { + var array = new List(); + foreach (var item in element.EnumerateArray()) + { + if (item.ValueKind == JsonValueKind.Number) + { + array.Add(item.GetSingle()); + } + } + return array.ToArray(); + } + + if (param.Data is System.Collections.IEnumerable enumerable && !(param.Data is string)) + { + var array = new List(); + foreach (var item in enumerable) + { + array.Add(Convert.ToSingle(item)); + } + return array.ToArray(); + } + + return Array.Empty(); + } + private double[] GetDoubleArrayParameter(List parameters, string name) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param == null || param.Data == null) + { + return Array.Empty(); + } + + if (param.Data is JsonElement element && element.ValueKind == JsonValueKind.Array) + { + var array = new List(); + foreach (var item in element.EnumerateArray()) + { + if (item.ValueKind == JsonValueKind.Number) + { + array.Add(item.GetSingle()); + } + } + return array.ToArray(); + } + + if (param.Data is System.Collections.IEnumerable enumerable && !(param.Data is string)) + { + var array = new List(); + foreach (var item in enumerable) + { + array.Add(Convert.ToSingle(item)); + } + return array.ToArray(); + } + + return Array.Empty(); + } + private void UpdateParameter(List parameters, string name, object value) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param != null) + { + param.Data = value; + } + } + + private void UpdateArrayParameter(List parameters, string name, float[] value) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param != null) + { + param.Data = value; + } + } + private void UpdateDoubleArrayParameter(List parameters, string name, double[] value) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param != null) + { + param.Data = value; + } + } + private void UpdateIntArrayParameter(List parameters, string name, int[] value) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param != null) + { + param.Data = value; + } + } + // 添加新的辅助方法来处理二维数组 + private double[,] GetDouble2DArrayParameter(List parameters, string name, int rows, int cols) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param == null || param.Data == null) + { + return new double[rows, cols]; + } + + double[,] result = new double[rows, cols]; + + if (param.Data is JsonElement element && element.ValueKind == JsonValueKind.Array) + { + int index = 0; + foreach (var item in element.EnumerateArray()) + { + if (item.ValueKind == JsonValueKind.Number) + { + int row = index / cols; + int col = index % cols; + if (row < rows && col < cols) + { + result[row, col] = item.GetDouble(); + } + index++; + } + } + } + else if (param.Data is System.Collections.IEnumerable enumerable && !(param.Data is string)) + { + int index = 0; + foreach (var item in enumerable) + { + int row = index / cols; + int col = index % cols; + if (row < rows && col < cols) + { + result[row, col] = Convert.ToDouble(item); + } + index++; + } + } + + return result; + } + + // 添加更新二维数组参数的方法 + private void UpdateDouble2DArrayParameter(List parameters, string name, double[,] value) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param != null) + { + // 将二维数组转换为一维数组以便于JSON序列化 + int rows = value.GetLength(0); + int cols = value.GetLength(1); + double[] flatArray = new double[rows * cols]; + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + flatArray[i * cols + j] = value[i, j]; + } + } + param.Data = flatArray; + } + } + // float版本 + private float[,] GetFloat2DArrayParameter(List parameters, string name, int rows, int cols) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param == null || param.Data == null) + { + return new float[rows, cols]; + } + + float[,] result = new float[rows, cols]; + + if (param.Data is JsonElement element && element.ValueKind == JsonValueKind.Array) + { + int index = 0; + foreach (var item in element.EnumerateArray()) + { + if (item.ValueKind == JsonValueKind.Number) + { + int row = index / cols; + int col = index % cols; + if (row < rows && col < cols) + { + result[row, col] = item.GetSingle(); + } + index++; + } + } + } + else if (param.Data is System.Collections.IEnumerable enumerable && !(param.Data is string)) + { + int index = 0; + foreach (var item in enumerable) + { + int row = index / cols; + int col = index % cols; + if (row < rows && col < cols) + { + result[row, col] = Convert.ToSingle(item); + } + index++; + } + } + + return result; + } + + private void UpdateFloat2DArrayParameter(List parameters, string name, float[,] value) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param != null) + { + int rows = value.GetLength(0); + int cols = value.GetLength(1); + float[] flatArray = new float[rows * cols]; + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + flatArray[i * cols + j] = value[i, j]; + } + } + param.Data = flatArray; + } + } + + // int版本 + private int[,] GetInt2DArrayParameter(List parameters, string name, int rows, int cols) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param == null || param.Data == null) + { + return new int[rows, cols]; + } + + int[,] result = new int[rows, cols]; + + if (param.Data is JsonElement element && element.ValueKind == JsonValueKind.Array) + { + int index = 0; + foreach (var item in element.EnumerateArray()) + { + if (item.ValueKind == JsonValueKind.Number) + { + int row = index / cols; + int col = index % cols; + if (row < rows && col < cols) + { + result[row, col] = item.GetInt32(); + } + index++; + } + } + } + else if (param.Data is System.Collections.IEnumerable enumerable && !(param.Data is string)) + { + int index = 0; + foreach (var item in enumerable) + { + int row = index / cols; + int col = index % cols; + if (row < rows && col < cols) + { + result[row, col] = Convert.ToInt32(item); + } + index++; + } + } + + return result; + } + + private void UpdateInt2DArrayParameter(List parameters, string name, int[,] value) + { + var param = parameters.FirstOrDefault(p => p.Name == name); + if (param != null) + { + int rows = value.GetLength(0); + int cols = value.GetLength(1); + int[] flatArray = new int[rows * cols]; + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + flatArray[i * cols + j] = value[i, j]; + } + } + param.Data = flatArray; + } + } + } +} \ No newline at end of file diff --git a/FortranWebApi/appsettings.json b/FortranWebApi/appsettings.json new file mode 100644 index 0000000..dcb0750 --- /dev/null +++ b/FortranWebApi/appsettings.json @@ -0,0 +1,16 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "Kestrel": { + "Endpoints": { + "Http": { + "Url": "http://0.0.0.0:5000" + } + } + } +} \ No newline at end of file diff --git a/HttpRequests/fortranwebapi.http b/HttpRequests/fortranwebapi.http index 6165a7f..1c35b49 100644 --- a/HttpRequests/fortranwebapi.http +++ b/HttpRequests/fortranwebapi.http @@ -7,6 +7,7 @@ Content-Type: application/json { "text": "{\"FuncName\":\"calculate_main\",\"ClassName\":\"\",\"Par\":[ {"Name":"n","DataType":"0","ArrayType":"0","IsOut":"2","Data":0}, - {"Name":"x","DataType":"1","ArrayType":"1","IsOut":"2","Data":[]}, - {"Name":"xden","DataType":"1","ArrayType":"1","IsOut":"1","Data":[]}]}" + {"Name":"Data_Error","DataType":"1","ArrayType":"1","IsOut":"2","Data":[]}, + {"Name":"Data_Re","DataType":"1","ArrayType":"1","IsOut":"1","Data":[]}, + {"Name":"Error","DataType":"1","ArrayType":"1","IsOut":"1","Data":[]}]}" } \ No newline at end of file diff --git a/Services/FortranInteropService.cs b/Services/FortranInteropService.cs index 99a5632..4e9dd67 100644 --- a/Services/FortranInteropService.cs +++ b/Services/FortranInteropService.cs @@ -35,8 +35,9 @@ namespace FortranWebApi.Services [DllImport("libSUB_WAVELET.so", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SUB_WAVELET")] private static extern void SUB_WAVELET( ref int n, - double[] x, - double[] xden + double[] Data_Error, + double[] Data_Re, + double[] Error ); public string ProcessFortranRequest(string requestText) @@ -63,38 +64,56 @@ namespace FortranWebApi.Services // 提取参数 int n = GetIntParameter(parameters, "n"); - double[] x = GetDoubleArrayParameter(parameters, "x"); + double[] Data_Error = GetDoubleArrayParameter(parameters, "Data_Error"); // 准备数组参数 - double[] xden = new double[10000]; // 输出数组,初始大小为10000 + double[] Data_Re = new double[10000]; // 输出数组,初始大小为10000 + double[] Error = new double[10000]; // 输出数组,初始大小为10000 // 调用Fortran函数 SUB_WAVELET( ref n, - x, - xden + Data_Error, + Data_Re, + Error ); // 更新输出参数 - UpdateDoubleArrayParameter(parameters, "xden", xden); + UpdateDoubleArrayParameter(parameters, "Data_Re", Data_Re); + UpdateDoubleArrayParameter(parameters, "Error", Error); // 处理输出数组 - // 处理输出数组 xden + // 处理输出数组 Data_Re // 注意:没有找到明确的长度参数,使用非零元素数量 { int nonZeroCount = 0; - for (int i = 0; i < xden.Length; i++) + for (int i = 0; i < Data_Re.Length; i++) { - if (xden[i] != 0) nonZeroCount = i + 1; + if (Data_Re[i] != 0) nonZeroCount = i + 1; } if (nonZeroCount > 0) { double[] resultArray = new double[nonZeroCount]; - Array.Copy(xden, resultArray, nonZeroCount); - UpdateDoubleArrayParameter(parameters, "xden", resultArray); + Array.Copy(Data_Re, resultArray, nonZeroCount); + UpdateDoubleArrayParameter(parameters, "Data_Re", resultArray); + } + } + // 处理输出数组 Error + // 注意:没有找到明确的长度参数,使用非零元素数量 + { + int nonZeroCount = 0; + for (int i = 0; i < Error.Length; i++) + { + if (Error[i] != 0) nonZeroCount = i + 1; + } + if (nonZeroCount > 0) + { + double[] resultArray = new double[nonZeroCount]; + Array.Copy(Error, resultArray, nonZeroCount); + UpdateDoubleArrayParameter(parameters, "Error", resultArray); } }