{"id":18595709,"url":"https://github.com/zncl2222/uc_sgsim","last_synced_at":"2026-04-28T18:07:49.253Z","repository":{"id":39635774,"uuid":"429350839","full_name":"Zncl2222/uc_sgsim","owner":"Zncl2222","description":"An uncondition random field generation tools that are easy to use","archived":false,"fork":false,"pushed_at":"2024-04-05T01:48:25.000Z","size":7024,"stargazers_count":0,"open_issues_count":10,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-04-05T02:46:15.895Z","etag":null,"topics":["c","geostatistics","python","simulation","stochastic-simulation"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Zncl2222.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-11-18T08:24:58.000Z","updated_at":"2024-06-08T12:31:36.913Z","dependencies_parsed_at":"2024-02-10T15:27:11.234Z","dependency_job_id":"1f090212-9ac7-49c5-932f-613b0bbf5ad8","html_url":"https://github.com/Zncl2222/uc_sgsim","commit_stats":{"total_commits":267,"total_committers":2,"mean_commits":133.5,"dds":"0.029962546816479363","last_synced_commit":"65b1c6802dc5c63e96f45796826f3924721e6b59"},"previous_names":["zncl2222/stochastic_uc_sgsim"],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zncl2222%2Fuc_sgsim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zncl2222%2Fuc_sgsim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zncl2222%2Fuc_sgsim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zncl2222%2Fuc_sgsim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Zncl2222","download_url":"https://codeload.github.com/Zncl2222/uc_sgsim/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239379323,"owners_count":19628684,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","geostatistics","python","simulation","stochastic-simulation"],"created_at":"2024-11-07T01:20:48.425Z","updated_at":"2026-04-28T18:07:44.230Z","avatar_url":"https://github.com/Zncl2222.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"![licence](https://img.shields.io/github/license/Zncl2222/Stochastic_UC_SGSIM)\n![python](https://img.shields.io/pypi/pyversions/uc-sgsim)\n[![ci](https://img.shields.io/github/actions/workflow/status/Zncl2222/uc_sgsim/github-pre-commit.yml?logo=pre-commit\u0026label=pre-commit)](https://github.com/Zncl2222/Stochastic_UC_SGSIM/actions/workflows/github-pre-commit.yml)\n[![build](https://img.shields.io/github/actions/workflow/status/Zncl2222/uc_sgsim/cmake.yml?logo=cmake\u0026logoColor=red\u0026label=CMake)](https://github.com/Zncl2222/Stochastic_UC_SGSIM/actions/workflows/cmake.yml)\n[![pytest](https://img.shields.io/github/actions/workflow/status/Zncl2222/uc_sgsim/sonarcloud.yml?logo=pytest\u0026label=pytest)](https://github.com/Zncl2222/Stochastic_UC_SGSIM/actions/workflows/sonarcloud.yml)\n[![build](https://github.com/Zncl2222/Stochastic_UC_SGSIM/actions/workflows/codeql.yml/badge.svg)](https://github.com/Zncl2222/Stochastic_UC_SGSIM/actions/workflows/codeql.yml)\n[![codecov](https://codecov.io/gh/Zncl2222/uc_sgsim/branch/main/graph/badge.svg?token=3qZt0OqDNI)](https://codecov.io/gh/Zncl2222/uc_sgsim)\n\n\u003cdiv\u003e\n \u003ctable\u003e\n  \u003cthead\u003e\n    \u003ctr\u003e\n      \u003ctd colspan=\"5\" align=\"center\"\u003e\u003cstrong\u003eSonar Cloud Quality\u003c/strong\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003eMetric\u003c/th\u003e\n      \u003cth\u003ePython\u003c/th\u003e\n      \u003cth\u003eC\u003c/th\u003e\n    \u003c/tr\u003e\n  \u003c/thead\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\u003cstrong\u003eQuality Gate\u003c/strong\u003e\u003c/td\u003e\n      \u003ctd\u003e\u003ca href=\"https://sonarcloud.io/summary/new_code?id=zncl2222_Stochastic_UC_SGSIM_py\"\u003e\u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=zncl2222_Stochastic_UC_SGSIM_py\u0026metric=alert_status\" alt=\"Quality Gate Status\"\u003e\u003c/a\u003e\u003c/td\u003e\n      \u003ctd\u003e\u003ca href=\"https://sonarcloud.io/summary/new_code?id=zncl2222_Stochastic_UC_SGSIM_c\"\u003e\u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=zncl2222_Stochastic_UC_SGSIM_c\u0026metric=alert_status\" alt=\"Quality Gate Status\"\u003e\u003c/a\u003e\u003c/td\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\u003cstrong\u003eReliability Rating\u003c/strong\u003e\u003c/td\u003e\n      \u003ctd\u003e\u003ca href=\"https://sonarcloud.io/summary/new_code?id=zncl2222_Stochastic_UC_SGSIM_py\"\u003e\u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=zncl2222_Stochastic_UC_SGSIM_py\u0026metric=reliability_rating\" alt=\"Reliability Rating\"\u003e\u003c/a\u003e\u003c/td\u003e\n      \u003ctd\u003e\u003ca href=\"https://sonarcloud.io/summary/new_code?id=zncl2222_Stochastic_UC_SGSIM_c\"\u003e\u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=zncl2222_Stochastic_UC_SGSIM_c\u0026metric=reliability_rating\" alt=\"Reliability Rating\"\u003e\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\u003cstrong\u003eSecurity Rating\u003c/strong\u003e\u003c/td\u003e\n      \u003ctd\u003e\u003ca href=\"https://sonarcloud.io/summary/new_code?id=zncl2222_Stochastic_UC_SGSIM_py\"\u003e\u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=zncl2222_Stochastic_UC_SGSIM_py\u0026metric=security_rating\" alt=\"Security Rating\"\u003e\u003c/a\u003e\u003c/td\u003e\n      \u003ctd\u003e\u003ca href=\"https://sonarcloud.io/summary/new_code?id=zncl2222_Stochastic_UC_SGSIM_c\"\u003e\u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=zncl2222_Stochastic_UC_SGSIM_c\u0026metric=security_rating\" alt=\"Security Rating\"\u003e\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\u003cstrong\u003eMaintainability Rating\u003c/strong\u003e\u003c/td\u003e\n      \u003ctd\u003e\u003ca href=\"https://sonarcloud.io/summary/new_code?id=zncl2222_Stochastic_UC_SGSIM_py\"\u003e\u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=zncl2222_Stochastic_UC_SGSIM_py\u0026metric=sqale_rating\" alt=\"Maintainability Rating\"\u003e\u003c/a\u003e\u003c/td\u003e\n      \u003ctd\u003e\u003ca href=\"https://sonarcloud.io/summary/new_code?id=zncl2222_Stochastic_UC_SGSIM_c\"\u003e\u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=zncl2222_Stochastic_UC_SGSIM_c\u0026metric=sqale_rating\" alt=\"Maintainability Rating\"\u003e\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n \u003c/table\u003e\n\u003c/div\u003e\n\n\u003ch3 align=\"center\"\u003e\n\n\u003e __Warning__\n\u003e This project is still in the pre-dev stage, the API usuage may be subject to change\n\n\u003c/h3\u003e\n\n## UnConditional Sequential Gaussian SIMulation (UCSGSIM)\n\n\u003ch3 align=\"center\"\u003eAn unconditional random field generation tools that are easy to use.\u003c/h3\u003e\n\n## Introduction to UCSGSIM\nUnConditional Sequential Gaussian Simulation (UCSGSIM) is a method for generating random fields that is based on the kriging interpolation technique.\n\nUnconditional simulation does not adhere to the patterns observed in the data but instead follows the user's settings, such as mean and variance.\n\n**The core ideas of UCSGSIM are:**\n1. Create the grid (no data values exist at this stage).\n\n$$ \\Omega\\to R $$\n\n2. Select a random point within the model (draw one random value from the x_grid).\n\n$$ X = RandomValue(\\Omega),  X:\\Omega\\to R $$\n\n3. Choose the **theoretical covariance model** to use and set the **sill** and **range** properly.\n\n$$ Gaussian = (C_{0} - s)(1 - e^{-h^{2}/r^{2}a})$$\n\n$$ Spherical = (C_{0} - s)(3h/2r - h^3/2r^3)$$\n\n$$ Exponential = (C_{0} - s)(1 - e^{-h/ra})$$\n\n4. If there are more than one data value close to the visited point (based on the **range** of the covariance model), proceed to the next step. Otherwise, draw a random value from a normal distribution as the simulation result for this iteration.\n\n$$ Z_{k}({X_{simulation}}) = RandomNormal(m = 0 ,\\sigma^2 = Sill)$$\n\n5. Calculate **weights** from the **data covaraince** and **distance coavariance**\n\n$$ \\sum_{j=1}^{n}\\omega_{j} = C(X_{data}^{i},X_{data}^{i})C^{-1}(X_i,X_i), i=1...N $$\n\n6. Calculate the **kriging estimate** from the **weights** and **data value**\n\n$$ Z_{k}(X_{estimate}) = \\sum_{i=1}^{n} \\omega_{i} Z(X_{data}) + (1- \\sum_{i=1}^{n} \\omega_{i} m_{g}) $$\n\n7. Calculate the **kriging error (kriging variance)** from **weights** and **data covariance**\n\n$$ \\sigma_{krige}^{2} = \\sum_{i=1}^{n}\\omega_{i}C(X_{data}^{i},X_{data}^{i}) $$\n\n8. Draw a random value from the normal distribution and add to the **kriging estimate**.\n\n$$ Z(X_{simulation}) = Z(X_{estimate}) + RandomNormal(m = 0, \\sigma^2 = \\sigma_{krige}^{2}) $$\n\n9. Repeat 2 ~ 8 until the entire model is simulated.\n\n10. Repeat 1 ~ 9 with different **randomseed number** to produce mutiple realizations.\n\n## Installation\n```bash\npip install uc-sgsim\n```\n\n## Features\n* One dimensional unconditional randomfield generation with sequential gaussian simulation algorithm\n* Muti-cores simulation (mutiprocessing)\n* Ability to generate random fields in Python using either a C interface via ctype or directly in Python using the NumPy and SciPy libraries.\n\n## Examples\n```py\nimport matplotlib.pyplot as plt\nimport uc_sgsim as uc\nfrom uc_sgsim.cov_model import Gaussian\n\nif __name__ == '__main__':\n    x = 151  # Model grid, only 1D case is support now\n\n    bw_s = 1  # lag step\n    bw_l = 35  # lag range\n    randomseed = 151  # randomseed for simulation\n    k_range = 17.32  # effective range of covariance model\n    sill = 1  # sill of covariance model\n\n    nR = 10  # numbers of realizations in each CPU cores,\n    # if nR = 1 n_process = 8\n    # than you will compute total 8 realizations\n\n    # Create Covariance model first\n    cov_model = Gaussian(bw_l, bw_s, k_range, sill)\n\n    # Create simulation and input the Cov model\n    # You could also set min_value, max_value and max_neighbor for sgsim by key words\n    # sgsim = uc.UCSgsimDLL(x, nR, cov_model, min_value=-6, max_value=6, max_neigh=10)\n    # set min_value, max_value and max_neighbor by directly assign\n    # sgsim.min_value = -6\n    # sgsim.max_value = 6\n    # sgsim.max_neigh = 10\n\n    # Create simulation with default min_value, max_value and max_neigh params\n    sgsim_py = uc.UCSgsim(x, nR, cov_model) # run sgsim with python\n    sgsim_c = uc.UCSgsimDLL(x, nR, cov_model) # run sgsim with c\n\n    # Start compute with n CPUs\n    sgsim_c.compute(n_process=2, randomseed=randomseed)\n    sgsim_py.compute(n_process=2, randomseed=987654)\n\n    sgsim_c.mean_plot('ALL')  # Plot mean\n    sgsim_c.variance_plot()  # Plot variance\n    sgsim_c.cdf_plot(x_location=10)  # CDF\n    sgsim_c.hist_plot(x_location=10)  # Hist\n    sgsim_c.variogram_compute(n_process=2)  # Compute variogram before plotting\n    # Plot variogram and mean variogram for validation\n    sgsim.variogram_plot()\n    # Save random_field and variogram\n    sgsim_c.save_random_field('randomfields.csv', save_single=True)\n    sgsim_c.save_variogram('variograms.csv', save_single=True)\n\n    # show figure\n    plt.show()\n```\n\n\u003cp align=\"center\"\u003e\n   \u003cimg src=\"https://github.com/Zncl2222/Stochastic_SGSIM/blob/main/figure/Realizations.png\"  width=\"40%\"/\u003e\n   \u003cimg src=\"https://github.com/Zncl2222/Stochastic_SGSIM/blob/main/figure/Mean.png\"  width=\"40%\"/\u003e\n   \u003cimg src=\"https://github.com/Zncl2222/Stochastic_SGSIM/blob/main/figure/Variance.png\"  width=\"40%\"/\u003e\n   \u003cimg src=\"https://github.com/Zncl2222/Stochastic_SGSIM/blob/main/figure/Variogram.png\"  width=\"50%\"/\u003e\n   \u003cimg src=\"https://github.com/Zncl2222/Stochastic_SGSIM/blob/main/figure/HIST.png\"  width=\"40%\"/\u003e\n   \u003cimg src=\"https://github.com/Zncl2222/Stochastic_SGSIM/blob/main/figure/CDF.png\"  width=\"50%\"/\u003e\n\u003c/p\u003e\n\nIf you prefer to utilize pure C to execute this code, you can make modifications to the c_example.c file located in the root directory. Once you've made the necessary changes to c_example.c, you can compile and execute the code using the following commands:\n\nOn Linux\n```bash\nsh cmake_build.sh\n```\nOn Windows\n```bat\ncmake_build.bat\n```\n\nC example file\n```c\n// c_example.c\n# include \u003cstdio.h\u003e\n# include \u003cstdlib.h\u003e\n\n# include \"./uc_sgsim/c_core/include/sgsim.h\"\n# include \"./uc_sgsim/c_core/include/cov_model.h\"\n# if defined(__linux__) || defined(__unix__)\n# define PAUSE printf(\"Press Enter key to continue...\"); fgetc(stdin);//NOLINT\n# elif _WIN32\n# define PAUSE system(\"PAUSE\");\n# endif\n\nint main() {\n    // you can also set z_min and z_max at sgsim_t. Default value will depend on\n    // sill value in cov_model_t\n    sgsim_t sgsim_example = {\n        .x_len = 150,\n        .realization_numbers = 5,\n        .randomseed = 12345,\n        .kriging_method = 1,\n        .if_alloc_memory = 1,  // This should be equal to 1 if you want to run by c.\n    };\n\n    // you can also set max_negibor at cov_model_t. Defualt value is 4.\n    cov_model_t cov_example = {\n        .bw_l = 35,\n        .bw_s = 1,\n        .k_range = 17.32,\n        .use_cov_cache = 0,\n        .sill = 1,\n        .nugget = 0,\n    };\n\n    sgsim_run(\u0026sgsim_example, \u0026cov_example, 0);\n    sgsim_t_free(\u0026sgsim_example);\n    PAUSE\n    return 0;\n}\n```\n\n## Future plans\n* 2D unconditional randomfield generation\n* GUI (pyhton)\n* More covariance models\n* More kriging methods (etc. Oridinary Kriging)\n* Performance enhancement\n* Providing more comprehensive documentation and user-friendly design improvements.\n\n## Performance\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://github.com/Zncl2222/Stochastic_SGSIM/blob/main/figure/C_Cpp_py_comparision.png\"  width=\"70%\"/\u003e\n\u003c/p\u003e\n\n```\nParameters:\n\nmodel len = 150\n\nnumber of realizations = 1000\n\nRange scale = 17.32\n\nVariogram model = Gaussian model\n\n---------------------------------------------------------------------------------------\n\nTesting platform:\n\nCPU: AMD Ryzen 9 4900 hs\n\nRAM: DDR4 - 3200 40GB (Dual channel 16GB)\n\nDisk: WD SN530\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzncl2222%2Fuc_sgsim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzncl2222%2Fuc_sgsim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzncl2222%2Fuc_sgsim/lists"}